我正在创建pdf文档。有一个PdfPTable,其中某些行将在每个页面中重复。我要在每页的该表格单元格中添加页码。
我尝试通过在PdfPageEventHelper中呈现单元格并在onEndPage()中写入表,但是页面数没有增加,并且在页面中始终为1。
我该如何实现?
[已更新]
我渲染了表OnStartPage()而不是构造函数。我使用writer.PageNumber作为pageCount。现在页数增加了,但是表在每个页面中一次又一次地出现。
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);
}
}
我希望页码在每页的表格单元格中。
答案 0 :(得分:1)
这是我的解决方法。
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;
}
}
答案 1 :(得分:0)
文档类型中有一个参数document
,具有函数getPageNumber
(https://itextsupport.com/apidocs/iText5/5.5.9/com/itextpdf/text/Document.html#getPageNumber--)
修改
阅读@mkl的链接后,我必须撤消上述内容。 真正的答案来自@mkl。