我使用的是iTextSharp版本5.4.5.0。
我在使用document.NewPage()方法添加新页面时遇到了问题。
当我使用上述方法添加新页面时,这个新页面的内容将与我的标题部分重叠。我已经在OnEndPage()类的PdfPageEventHelper方法中创建了标题表。
以下是OnEndPage事件中我的标题的代码:
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,如下所示:
MemoryStream workStream = new MemoryStream();
Document document = new Document(); 
PdfWriter writer = PdfWriter.GetInstance(document, workStream);
document.SetMargins(30, 30, 45, 30);有人能帮助我在添加新页面时不要将文档内容与标题表重叠吗?
谢谢。
发布于 2016-02-17 08:24:27
当你这样做时:
table.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);您声称是// Here the document.Top value is 45,但这是不正确的!
您可以这样创建文档:
Document document = new Document();这意味着文档的左下角坐标是(0, 0),右上坐标是(595, 842)。
然后定义文档的页边距:
document.SetMargins(30, 30, 45, 30);这意味着:
Document.Left =0+ 30 = 30Document.Right =595-30= 565Document.Top =842-45= 797Document.Bottom =0+ 30 = 30所以当你有了这句话:
table.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);你实际上有:
table.WriteSelectedRows(0, -1, 30, 797, writer.DirectContent);这是完全错误的,因为这会使您的表与在边距中添加的任何内容重叠。
要解决这个问题,需要计算表的高度。例如,请看问题如何根据内容定义页面大小?的答案
table.LockedWidth = true;
Float h = table.TotalHeight;现在,您可以使用h来定义上边距:
document.SetMargins(30, 30, 20 + h, 30);您可以使用Document.Top定义表的y位置:
table.WriteSelectedRows(0, -1,
    document.LeftMargin,
    document.Top + h + 10,
    writer.DirectContent);使用此代码,将在页边距中添加该表,在页面顶部留下10个用户单位的空白,在上边距上留下10个用户单位的空白。
https://stackoverflow.com/questions/35449121
复制相似问题