2016-12-11 6 views
0

私はバーコード付きのpdfファイルを生成するためにC#でアプリケーションを作成しています。ITextSharp直接コンテンツバーコードを追加する段落のフォントの色を変更する

私はメモリストリームを使用して、最後にPDFファイルに書き込むテーブル(1列)にデータを配置しています。 バーコードを追加する必要があるまで、すべてが正常に機能しています。そのために私は直接のコンテンツを使用します。

 
pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream); 
PdfContentByte cb = new PdfContentByte(pdfWriter); 

iTextSharp.text.Image bc128 = code128.CreateImageWithBarcode(cb, null, null); 

私はバーコードを作成した後、私が使用してテーブルに追加します。

tableout.AddCell(image128); 

をそして、私は問題があります。前に作成したテーブルの行が表示されますが、テキストは表示されません。私がバーコードを追加して行をコメントしてもOKです。

うん:

私は、フォーラムに1件のコメントを見つけました。フォアグラウンドテキストの色は、ハイレベルの テキストレイアウトコードのフォントによって定義されます。 PdfContentByteを直接使用すると、 のフォントと色が分離されますが、それまでは分離されません。

どうすればこの問題を解決できますか?

私はシステムドローイング方法を使用できることを知っていますが、バーコードのイメージはあまり明確ではありません。

+0

_フォーラムで1つのコメントを見つけました:_ - あなたのフォーラムへのリンクを追加してください。 –

+0

また、デッドラインはスタックオーバーフローでカウントされません。友好的なアドバイスの言葉:あなたは、締め切りに言及すれば、人々は実際にはQ&Aの無料サイトであなたを手伝う可能性は低いです。あなたを助けるために、私はそれを編集しました。 –

+0

http://stackoverflow.com/questions/5806242/define-foreground-color-of-a-itextsharp-table-cellご連絡ありがとうございます。私は数日間このことに取り組んでおり、アイデアが不足しています。 – brane

答えて

0

PdfContentByteは、pdfの絶対位置に直接描画するためのものです。しかし、あなたが必要とするのは、 "文書スタック"に追加できるIElementです。

(ただ明確なクラスを識別するために、完全な名前空間を使用して)、これを試してみてください:

Barcode128 barcode = new Barcode128(); 
    System.Drawing.Image img = barcode.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White); 
    iTextSharp.text.Image itextImg = iTextSharp.text.Image.GetInstance(img, System.Drawing.Imaging.ImageFormat.Bmp); 
    table.AddCell(new PdfPCell(itextImg)); 
+0

ありがとうございます。 私はSystem.Drawing.Imageを使用することができますが、イメージはあまり明確ではないことを知っています。 – brane

+0

「あまり明確ではない」とはどういう意味ですか?画質は? – COeDev

+0

はい。画質は素晴らしいです。 – brane

0

私はバーコードとシステムdrawning方法が、画像を利用することができます知っているがそれほど明確ではありません。

ここに私が使用するコードがあります。ドキュメントの行数に応じて異なるサイズのドキュメントを取得しようとしています。だから私はメモリストリームを使用しています(ここには示されていません)。私のアイデアは、メモリとカウントの行のテーブルにデータを書き込むことです。その後、私は新しいpdfドキュメントを作成し、そのドキュメントにテーブルを追加します。

table.AddCell(image128); 

両方の文書が同じである:私は行をコメントした場合

。しかし、その行をコードに残すと、最終的なドキュメントはすべての行を表示しますが、行のテキストは表示されません。 firefoxで2番目のdocu、entを開くと、バーコードが表示されることもあります。

どのような提案も素晴らしいでしょう。または、コンテンツの量によって異なるサイズのドキュメントを取得する別の方法があります。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 
using System.IO; 
using System.Drawing; 

namespace testtables1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(300, 720); 
      Document pdfToCreate = new Document(pageSize, 0, 0, 0, 0); 
      PdfWriter writer = PdfWriter.GetInstance(pdfToCreate, new FileStream(@"D:\\testfile.pdf", FileMode.Create)); 
      pdfToCreate.Open(); 

      PdfPTable table = new PdfPTable(1); 
      PdfPTable tableout = new PdfPTable(1); 

      WriteLineToPdf("This is first row. Below is image generated using SystemDrawing.", table, out tableout); 
      table = tableout; 

      // Create bar code 
      Barcode128 code128 = new Barcode128(); 
      code128.CodeType = Barcode.CODE128; 
      code128.ChecksumText = true; 
      code128.GenerateChecksum = true; 
      code128.Code = "00100370006756555316"; 

      // Create a new PdfWrite object, writing the output to a MemoryStream 
      var outputStream = new MemoryStream(); 
      var pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream); 
      PdfContentByte cb = new PdfContentByte(writer); 

      // Image generated using System.Drawing and rendering 
      System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White)); 
      iTextSharp.text.Image bmCoverted = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp); 
      table.AddCell(bmCoverted); 

      pdfToCreate.Open(); 

      // Image generated using iTextSharp.text.Image 
      WriteLineToPdf("This is third row. Below is image generated using code128.CreateImageWithBarcode.", table, out tableout); 
      table = tableout; 
      iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null); 
      table.AddCell(image128); 
      table = tableout; 

      WriteLineToPdf("This is fifth row.", table, out tableout); 
      pdfToCreate.Add(tableout); 

      // Create new document with height that depends on number of lines in document. 
      iTextSharp.text.Rectangle psFinal = new iTextSharp.text.Rectangle(200, 300); 
      Document pdfFinal = new Document(psFinal, 0, 0, 0, 0); 
      PdfWriter writer1 = PdfWriter.GetInstance(pdfFinal, new FileStream(@"D:\\finalfile.pdf", FileMode.Create)); 
      pdfFinal.Open(); 
      pdfFinal.Add(tableout); 

      cb = null; 

      pdfToCreate.Close(); 
      pdfFinal.Close(); 
     } 

     // Write a single line to the PDF. 
     private static void WriteLineToPdf(string tLine, PdfPTable ticTable0In, out PdfPTable ticTableIN) 
     { 
      // Define fonts. 
      BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); // Base font. 
      iTextSharp.text.Font timesN = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); 

      Paragraph par = new Paragraph(tLine, timesN); 
      par.Alignment = Element.ALIGN_CENTER; 
      PdfPCell cell; 

      cell = new PdfPCell(); 
      //cell.FixedHeight = 12f; 
      //cell.Border = Rectangle.NO_BORDER; 


      cell.AddElement(par); 
      ticTable0In.AddCell(cell); 
      ticTableIN = ticTable0In; 
      par = null; 

     } 
    } 
} 

私の他の理想は、メモリストリームからのデータを使用することです。私は、私が欲しいサイズを持つresult.pdfドキュメントを作成することができますが、メモリサイズの変更からデータを書き込んだ後です。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace TableWithBC2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (MemoryStream myMemoryStream = new MemoryStream()) 
      { 
       PdfPTable tableIn; 
       Document pdfDocument = new Document(); 
       PdfWriter writer = PdfWriter.GetInstance(pdfDocument, myMemoryStream); 
       PdfPTable tableOut = new PdfPTable(new float[] { 200 }); 
       tableIn = new PdfPTable(new float[] { 200 }); 
       pdfDocument.Open(); 

       for (int i = 0; i < 5; i++) 
       { 
        WriteLineToPdf("This is row no. " + i.ToString(), tableIn, out tableOut); 
        tableIn = tableOut; 
       } 

       // Create bar code 
       PdfContentByte cb = writer.DirectContent; 
       Barcode128 code128 = new Barcode128(); 
       code128.CodeType = Barcode.CODE128; 
       code128.ChecksumText = true; 
       code128.GenerateChecksum = true; 
       code128.Code = "00100370006756555316"; 
       Image barCodeImage = code128.CreateImageWithBarcode(cb, null, null); 
       PdfPCell cell1 = new PdfPCell(barCodeImage); 
       tableOut.AddCell(cell1); 
       tableIn = tableOut; 
       WriteLineToPdf("This is a last row.", tableIn, out tableOut); 

       pdfDocument.Add(tableOut); 
       pdfDocument.Close(); 

       // Create final PDF document. 
       Document pdfDocument1 = new Document(new Rectangle(200, 250)); 
       PdfWriter writer1 = PdfWriter.GetInstance(pdfDocument1, new FileStream(@"D:\\result.pdf", FileMode.Create)); 
       pdfDocument1.Open(); 
       pdfDocument1.Add(new Chunk()); 
       pdfDocument1.Close(); 

       byte[] content = myMemoryStream.ToArray(); 

       // Write out PDF from memory stream. 
       using (FileStream fs = File.OpenWrite(@"D:\\nikola bd - final\\out\\dummy22.pdf")) 
       { 
        fs.Write(content, 0, (int)content.Length); 
       } 
      } 
     } 
     // Write a single line to the PDF. 
     private static void WriteLineToPdf(string tLine, PdfPTable tTableIn, out PdfPTable tTableOut) 
     { 
      BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); 
      iTextSharp.text.Font timesN = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.NORMAL, BaseColor.RED); 

      Paragraph par = new Paragraph(tLine, timesN); 
      par.Alignment = Element.ALIGN_CENTER; 
      PdfPCell cell = new PdfPCell(); 
      cell.AddElement(par); 
      tTableIn.AddCell(cell); 
      tTableOut = tTableIn; 
     } 
    } 
} 
関連する問題