添加新的一页iTextSharp的将头重叠 [英] Adding new page in itextsharp will overlap with header
问题描述
我使用iTextSharp的版本5.4.5.0。
I am using iTextSharp version 5.4.5.0.
我有一个问题,同时增加使用document.NewPage()方法的新的一页。
I have a problem while adding new page using document.NewPage() method.
当我将用上面的方法来添加新的页面,在此新页面的内容与我的头部分重叠。我创建了我的头表 PdfPageEventHelper
类的<code>的OnEndPage()方法。
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.
下面是的OnEndPage
的code为我的头事件:
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
和我也指派距顶部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 ?
感谢。
推荐答案
当你这样做:
table.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);
您声称 //这里document.Top值为45
但就是不正确!
您创建文档这样的:
Document document = new Document();
这意味着,左下文档的坐标(0,0)
和右上角坐标(595,842 )
。
This means that the lower-left coordinate of your document is (0, 0)
and the upper-right coordinate is (595, 842)
.
然后定义利润率文件:
document.SetMargins(30, 30, 45, 30);
这意味着:
-
Document.Left
= 0 + 30 = 30 -
Document.Right
= 595 - 30 = 565 -
Document.Top
= 842 - 45 = 797 -
Document.Bottom
= 0 + 30 = 30
Document.Left
= 0 + 30 = 30Document.Right
= 595 - 30 = 565Document.Top
= 842 - 45 = 797Document.Bottom
= 0 + 30 = 30
所以,当你有这样的一行:
So when you have this line:
table.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);
您实际上有:
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;
现在你可以使用 ^ h
来定义上边距:
Now you can use h
to define the top margin:
document.SetMargins(30, 30, 20 + h, 30);
您还可以使用 Document.Top
来定义是
表中的位置是:
table.WriteSelectedRows(0, -1,
document.LeftMargin,
document.Top + h + 10,
writer.DirectContent);
通过这个code,该表将被添加的在的保证金,留下的空白10个用户单位在页面顶部的白色空间上边距上述10个用户单位
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.
这篇关于添加新的一页iTextSharp的将头重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!