Adding new page in itextsharp will overlap with header

試著忘記壹切 提交于 2019-12-12 06:21:14

问题


I am using iTextSharp version 5.4.5.0.

I have a problem while adding new page using document.NewPage() method.

When I will use above method to add new page, the content of this new page will overlap with my header section. I have created my header table in OnEndPage() method of PdfPageEventHelper class.

Here is the code for my header in OnEndPage event:

PdfPTable table = new PdfPTable(1);
table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
PdfPTable headerTable = new PdfPTable(1) { TotalWidth = document.PageSize.Width };

PdfPCell cImage = new PdfPCell(ImageHeader, true);
cImage.HorizontalAlignment = Element.ALIGN_RIGHT;
cImage.Border = iTextSharp.text.Rectangle.NO_BORDER;
cImage.FixedHeight = 45f;
cImage.PaddingTop = 0f;

headerTable.AddCell(cImage);

PdfPCell cell = new PdfPCell(headerTable) { Border = Rectangle.NO_BORDER };
table.AddCell(cell);

table.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);   // Here the document.Top value is 45

And I have also assign margin top as 45 while creating document object as below:

MemoryStream workStream = new MemoryStream();
Document document = new Document(); 

PdfWriter writer = PdfWriter.GetInstance(document, workStream);
document.SetMargins(30, 30, 45, 30);

Can anyone help me to not overlap document content with header table while adding new page ?

Thanks.


回答1:


When you do this:

table.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);

You claim // Here the document.Top value is 45 but that is not correct!

You create your document like this:

Document document = new Document();

This means that the lower-left coordinate of your document is (0, 0) and the upper-right coordinate is (595, 842).

Then you define margins for the document:

document.SetMargins(30, 30, 45, 30);

This means that:

  • Document.Left = 0 + 30 = 30
  • Document.Right = 595 - 30 = 565
  • Document.Top = 842 - 45 = 797
  • Document.Bottom = 0 + 30 = 30

So when you have this line:

table.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);

You actually have:

table.WriteSelectedRows(0, -1, 30, 797, writer.DirectContent);

This is totally wrong because that makes your table overlap with whatever content you are adding inside the margins.

To solve this, you need to calculate the height of the table. See for instance the answer to the question How to define the page size based on the content?

table.LockedWidth = true;
Float h = table.TotalHeight;

Now you can use h to define the top margin:

document.SetMargins(30, 30, 20 + h, 30);

And you can use Document.Top to define the y position of the table:

table.WriteSelectedRows(0, -1,
    document.LeftMargin,
    document.Top + h + 10,
    writer.DirectContent);

With this code, the table will be added inside the margin, leaving 10 user units of white space under the top of the page and 10 user units of white space above the top margin.



来源:https://stackoverflow.com/questions/35449121/adding-new-page-in-itextsharp-will-overlap-with-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!