Itextsharp place Pdfptable in the desired place

Header content and pdftable overlap

How to place PdfPTable at any position on a pdf page using (x, y) positioning (100, 200) or (15, 100) anywhere on a 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 do it like

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

Next I add 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 a match between the contents of the header and pdftable

+5
source share
2 answers

As mentioned in itext docs -

To avoid cell borders and overlapping contents, if you have thick cell borders, call setUserBorderPadding (true), for example this:

 cell.setUserBorderPadding(true); 
0
source

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

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

0
source

Source: https://habr.com/ru/post/1396957/


All Articles