Chandan
Chandan

Reputation: 235

Why these borders are showing when generating pdf using iTextsharp?

I am trying to generate multiple pdfs into a single pdf, which I have achieved by using itextSharp , but while generating them few thing I came across,which are pointed below:

How to solve these errors? Below is my code:

public byte[] GetPDF(string pHTML)
    {
        byte[] bPDF = null;
        MemoryStream ms = new MemoryStream();
        TextReader txtReader = new StringReader(pHTML);
        //Rectangle pagesize = new Rectangle(864.0f, 1152.0f);
        Document doc = new Document(PageSize.NOTE);
        string path = Server.MapPath("PDFs");
        PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
        HTMLWorker htmlWorker = new HTMLWorker(doc);
        doc.Open();

            for (int i = 1; i <= 5; i++)
            {

                doc.NewPage();
                PdfPTable table= new PdfPTable(1);
                table.TotalWidth = 500f;
                table.LockedWidth = true;
                table.HorizontalAlignment = 0;
                table.DefaultCell.Border = Rectangle.NO_BORDER;

                Image imageTopURL = Image.GetInstance("Top.PNG");
                       PdfPCell imgTopCell = new PdfPCell(imageTopURL);
                Paragraph p = new Paragraph("XYZ", new Font(Font.FontFamily.COURIER, 32f, Font.UNDERLINE));
                p.Alignment = Element.ALIGN_CENTER;
                table.AddCell(imgTopCell);
                table.AddCell(p);
                Image imageMidURL = Image.GetInstance("Mid.PNG");                    
                PdfPCell imgMidCell = new PdfPCell(imageMidURL);
                Paragraph p1 = new Paragraph("ABC", new Font(Font.FontFamily.HELVETICA, 29f, Font.ITALIC));
                p1.Alignment = Element.ALIGN_CENTER;
                table.AddCell(imgMidCell);
                imgMidCell.Border = 0;                 
                table.AddCell(p1);
                Image imageBotURL = Image.GetInstance("Bottom.PNG");
                PdfPCell imgBotCell = new PdfPCell(imageBotURL);
                table.AddCell(imgBotCell);
                imageTopURL.ScaleAbsolute(505f, 270f);
                imageMidURL.ScaleAbsolute(590f, 100f);
                imageBotURL.ScaleAbsolute(505f, 170f);
                 doc.Open();
                doc.Add(table);
                  htmlWorker.StartDocument();
                htmlWorker.Parse(txtReader);
                htmlWorker.EndDocument();
            }
              htmlWorker.Close();
               doc.Close();
        doc.Close();
        bPDF = ms.ToArray();
        return bPDF; 
}

Upvotes: 0

Views: 777

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You are telling the table that default cells shouldn't have a border:

table.DefaultCell.Border = Rectangle.NO_BORDER;

This means that PdfPCell instances that are created implicitly won't get a border. For instance: if you do:

table.AddCell("Implicit cell creation");

Then that cell won't get a border.

However: you are creating a cell explicitly:

PdfPCell imgTopCell = new PdfPCell(imageTopURL);

In this case, the DefaultCell is never used. It is very normal that imgTopCell has a border. If you don't want a border for imgTopCell, you need to define the Border of imgTopCell like this:

imgTopCell.Border = Rectangle.NO_BORDER;

Regarding the alignment: it seems that you didn't read about the difference between text mode and composite mode. Please read the documentation, for instance:

You are making a number of newbie mistakes that can all be fixed by reading the documentation. You have too many questions in one post. Please create new questions if my answer didn't solve every single of your problems. I see at least two more questions in your post (your question should actually be closed with as reason "Too broad").

Update:

In your comment, you added the following code snippet:

table.AddCell(new Paragraph(data.EmpName, new Font(Font.FontFamily.COURIER, 32f, Font.BOLD)));

You want to center this text.

First, let me explain that you are using the AddCell() method with a Paragraph as parameter. This doesn't really make sense as the Paragraph will be treated as a Phrase. You can as well write:

table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER ;
table.AddCell(new Phrase(data.EmpName, new Font(Font.FontFamily.COURIER, 32f, Font.BOLD)));

When you are passing a Phrase to the AddCell() method, you are

  1. using text mode (the properties of the cell prevail over the properties of its elements), and
  2. you are asking iTextSharp to create a PdfPCell.

In this case, iTextSharp will look at the DefaultCell and use the properties of that cell to create a new cell. If you want to center the content of that new cell, you need to define this at the level of the DefaultCell. All of this is explained in my answer to the following questions:

Upvotes: 1

Related Questions