C# 在每页的页眉单元格中添加页码
我正在创建一个pdf文档。有一个PdfPTable,其中的一些行将在每页中重复。我想要的是在每页的表格单元格中添加页码 我尝试在PdfPageEventHelper中呈现单元格,并在onEndPage()中写入表,但页码计数并没有增加,在每个页面中总是1 我怎样才能做到这一点 [更新] 我在startpage()上呈现了表,而不是构造函数。我使用writer.PageNumber进行页面计数。页码现在增加了,但表格在每页中都会反复出现C# 在每页的页眉单元格中添加页码,c#,itext,pdfptable,C#,Itext,Pdfptable,我正在创建一个pdf文档。有一个PdfPTable,其中的一些行将在每页中重复。我想要的是在每页的表格单元格中添加页码 我尝试在PdfPageEventHelper中呈现单元格,并在onEndPage()中写入表,但页码计数并没有增加,在每个页面中总是1 我怎样才能做到这一点 [更新] 我在startpage()上呈现了表,而不是构造函数。我使用writer.PageNumber进行页面计数。页码现在增加了,但表格在每页中都会反复出现 public class itsEventsHandler
public class itsEventsHandler : PdfPageEventHelper
{
int pageCount;
protected PdfPTable tblAccInfo = new PdfPTable(3);
public itsEventsHandler()
{
}
public override void OnStartPage(PdfWriter writer, Document document)
{
pageCount = writer.PageNumber;
this.BuildBorderCell(tblAccInfo, "A/C No.", boldFont);
this.BuildBorderCell(tblAccInfo, "External Doc No.", boldFont);
this.BuildBorderCell(tblAccInfo, "PAGE", boldFont);
this.BuildBorderCell(tblAccInfo, accountNo, titleFont);
this.BuildBorderCell(tblAccInfo, docNo, titleFont);
this.BuildBorderCell(tblAccInfo, pageCount.ToString(), titleFont);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
tblAccInfo.WriteSelectedRows(0, -1, document.LeftMargin + 53f,
document.PageSize.Height - 250f, writer.DirectContent);
}
}
我希望页码在每页的表单元格中。您应该在开始页上重写
()
public class itsEventsHandler : PdfPageEventHelper
{
int pageCount;
protected PdfPTable tblAccInfo = new PdfPTable(3);
public itsEventsHandler()
{
}
public override void OnStartPage(PdfWriter writer, Document document)
{
pageCount = writer.PageNumber;
this.BuildBorderCell(tblAccInfo, "A/C No.", boldFont);
this.BuildBorderCell(tblAccInfo, "External Doc No.", boldFont);
this.BuildBorderCell(tblAccInfo, "PAGE", boldFont);
this.BuildBorderCell(tblAccInfo, accountNo, titleFont);
this.BuildBorderCell(tblAccInfo, docNo, titleFont);
this.BuildBorderCell(tblAccInfo, pageCount.ToString(), titleFont);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
tblAccInfo.WriteSelectedRows(0, -1, document.LeftMargin + 53f,
document.PageSize.Height - 250f, writer.DirectContent);
}
}
有一个参数document
,来自具有函数getPageNumber
()
编辑
在阅读了@mkl的链接后,我不得不撤回上述内容。
真正的答案来自@mkl的评论。这是我的解决方案
public class itsEventsHandler : PdfPageEventHelper
{
int pageCount;
protected PdfPTable tblAccInfo = new PdfPTable(3);
public itsEventsHandler()
{
}
public void BuildBorderCell(PdfPTable pdfTable, string strText,
iTextSharp.text.Font font)
{
PdfPCell cell = new PdfPCell(new Phrase(strText, font));
cell.Border = iTextSharp.text.Rectangle.TOP_BORDER;
cell.PaddingTop = 5f;
pdfTable.AddCell(cell);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
pageCount = writer.PageNumber;
//Creating the table
PdfPTable tblAccInfo = new PdfPTable(3);
tblAccInfo.TotalWidth = 450f;
float[] accInfoWidths = new float[] { 50f, 50f, 50f};
tblAccInfo.SetWidths(accInfoWidths);
//Building table cell
BuildBorderCell(tblAccInfo, "A/C No.", boldFont);
BuildBorderCell(tblAccInfo, "External Doc No.", boldFont);
BuildBorderCell(tblAccInfo, "PAGE", boldFont);
BuildBorderCell(tblAccInfo, accountNo, titleFont);
BuildBorderCell(tblAccInfo, docNo, titleFont);
BuildBorderCell(tblAccInfo, pageCount.ToString(), titleFont);
//Writing the table
tblAccInfo.WriteSelectedRows(0, -1, document.LeftMargin + 53f,
document.PageSize.Height - 250f, writer.DirectContent);
//Droping the table
tblAccInfo = null;
}
}
查看您的pageCount++
是否在方法之外。您的代码是否编译过?此外,您还可以使用pageCount.ToString()
在构造函数中构建用作标头的表。因此,构建时的值在这里是固定的……A)无需在起始页上使用,您可以在OneDPage中执行所有操作。B) 我看到您为每个页面向表tblAccInfo
(至少我假设BuildBorderCell
就是这样做的)添加了新的单元格,因此它显然会不断增长。为什么不在onedpage
中实例化一个新的PdfPTable
实例,填充它,绘制它,然后删除它呢?通过将tblAccInfo
设置为null
或一个新的PdfPTable
实例。itext开发者建议使用onStartPage
添加内容,即使您参考的api文档也建议使用onedpage
。并且onedpage
具有相同的document
参数…它在哪里说不添加内容<代码>请注意,即使未编写页面,仍会调用此方法。最好使用onEndPage来避免无限循环。
和注意:不要在页面事件中使用Document.add()。
听起来并不像您可能不使用OP使用的BuildBorderCell操作。另外,OnPageEnd
有document
参数,OP用来使用构造函数而不是OnPageEnd
,我认为它是故意不在那里使用的。我更新了我的代码。我在startpage()上呈现了表,而不是构造函数。pagecount增加了,但每个页面都会创建一个表。@Mokuyobi好的,API文档只给出提示,不太清楚。不过,结合iText开发人员在这里写的关于堆栈溢出的内容,它总结为“不要在onStartPage
中向新文档页面添加任何内容,也不要通过document
或PdfWriter
或其他方式,只在onEndPage
中添加内容。”。您可以使用onStartPage
来准备内容,但请注意,在某些情况下,调用了onStartPage
的页面最终是不需要的,并且不会出现在PDF中。例如,阅读Bruno Lowagie的文章,他在文章中比在ApiDocs或书中更清楚地宣布了这一点:您正在使用OnStartPage()
方法添加内容。这是禁止的!您可能需要添加BuildBorderCell
helper方法。