6

Header Content and Pdftable is overlapping

How can I place PdfPTable at any position in the pdf page using (x, y) positioning like (100, 200) or (15, 100) at any place on the pdf page?

Header Table using PdfEventHelper

public override void OnEndPage(PdfWriter writer, Document document)
{
    AddHeader(writer, document);
}

public void AddHeader(PdfWriter writer, Document document)
{
    // set no of rows
    PdfPTable headerTable = new PdfPTable(1);
    // set the width
    headerTable.TotalWidth = document.PageSize.Width;
    headerTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;

    PdfPCell company = new PdfPCell(new Phrase(new Chunk("Name", fontArial)));
    company.HorizontalAlignment = Element.ALIGN_CENTER;
    company.BorderWidth = 0;
    headerTable.AddCell(company);

    PdfPCell report = new PdfPCell(new Phrase(new Chunk("PrintedDate", fontArial)));
    report.HorizontalAlignment = Element.ALIGN_CENTER;
    report.BorderWidth = 0;
    headerTable.AddCell(report);

    headerTable.TotalWidth = document.PageSize.Width - 20;

    // write rows to the pdf output stream
    Rectangle pageSize = document.PageSize;
    headerTable.WriteSelectedRows(0, -1, 0, (document.PageSize.Height - 10), writer.DirectContent);
}

In My Main Class I am doing like this

PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(pdfFile, FileMode.Create));

pdfWriter.PageEvent = page;

document.Open()

Next Here I am adding a pdftable

PdfPTable HeaderTable = new PdfPTable(2);
HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
HeaderTable.TotalWidth = pageSize.Width - 80;
HeaderTable.SetWidthPercentage(new float[] {45, 45}, pageSize);

PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
HeaderLeftCell.Padding = 5;
HeaderLeftCell.PaddingBottom = 8;
HeaderLeftCell.BorderWidthRight = 0;
HeaderTable.AddCell(HeaderLeftCell);

PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
HeaderRightCell.HorizontalAlignment = Element.ALIGN_RIGHT;
HeaderRightCell.Padding = 5;
HeaderRightCell.PaddingBottom = 8;
HeaderRightCell.BorderWidthLeft = 0;
HeaderTable.AddCell(HeaderRightCell);

HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(40), pageSize.GetTop(50), cb);

The result is overlap of Header Content and pdftable

bPratik
  • 6,894
  • 4
  • 36
  • 67
Brad
  • 229
  • 2
  • 4
  • 12
  • 1
    You **are** placing the table at a *specific* location when you call the five parameter overload [PdfPTable.WriteSelectedRows()] ( http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPTable.html#writeSelectedRows(int, int, float, float, com.itextpdf.text.pdf.PdfContentByte)) (adding long links in SO comment is really a PITA, SO is choking on the link, sorry). The header is probably overlapping your content because of the calculated top margin. – kuujinbo Feb 21 '12 at 16:49
  • 1
    @kuujinbo add it as an answer then... – bPratik Aug 03 '12 at 11:11

2 Answers2

0

As mentioned in the itext docs-

To avoid having the cell border and the content overlap, if you are having thick cell borders, call the setUserBorderPadding(true), like this:

cell.setUserBorderPadding(true);
sjain
  • 23,126
  • 28
  • 107
  • 185
0

you have calculated the y position for your header table in onload() like document.PageSize.Height-10.

please set some calculated value instead of pageSize.GetTop(50) in the second table also.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
DonaJose
  • 144
  • 5
  • Actually using something like `pageSize.GetTop(50)` (which es equivalent to `pageSize.Top - 50`) *is more correct than* `pageSize.Height - 10` as the latter only works for documents with the coordinate system origin in the lower left corner which is merely often, not always, the case. – mkl Feb 10 '17 at 09:28