0
votes

I'm creating an application in c# for generation of some pdf files with bar code in it.

I'm using the memory stream and placing data to the table (one column) which I'll write to the pdf file at the end. Everything is working fine till I need to add bar code. For that I use direct content.

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

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

After I create bar code I add it to the table using:

tableout.AddCell(image128);

And then I have a problem. Rows from the table that I've created before are shown, but I don't see a text. If I comment line with bar code addition everything is OK.

I've found one comment on forum:

Yep. Foreground text color is defined by the Font for the high-level text layout code. When you start using a PdfContentByte directly, the font and color become separated, but not until then.

How can I solve this?

I know that I can use system drawning method but image with bar code is not so clear.

2
I've found one comment on forum: - please add a link to your forum.Amedee Van Gasse
Also, deadlines don't count on Stack Overflow. A word of friendly advice: people are actually less likely to help you on a free-for-all Q&A website if you mention deadlines. So to help you, I edited that away.Amedee Van Gasse
stackoverflow.com/questions/5806242/… you for your advice. I'm working on this for few days and I'm running out of ideas.brane
Please share a sscce which allows us to reproduce the issue.mkl

2 Answers

0
votes

PdfContentByte is for direct rendering to absolute positions in the pdf. But what you need is an IElement you can add to the "document stack".

Try this (using full namespaces just to clearly identify the classes):

  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
votes

I know that I can use system drawning method but image with bar code is not so clear.

Here is a code that I use. I'm trying to get documents whit different sizes depending on the number of lines in the document. So I'm using memory stream (not shown here). My idea is to write data to the table in memory and count lines. After that I will create new pdf document and add table to that document.

If I comment a line:

table.AddCell(image128);

both documents are the same. But if I leave that line in code final document shows all rows but text in rows is not visible. If I open second docu,ent in firefox I can even see a bar codes.

Any suggestion will be great. Or is there a different way to get documents with different sizes depending on amount of content.

    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;

        }
    }
}

My other ideai is to use data from memory stream. I'm able to create result.pdf document with size that I want but after I write Data from memory size changes.

    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;
        }
    }
}