【问题标题】:Adding page number in header cell in every page在每页的标题单元格中添加页码
【发布时间】:2019-08-31 10:41:17
【问题描述】:

我正在创建一个 pdf 文档。有一个 PdfPTable,其中某些行将在每一页中重复。我想要的是在每一页的表格单元格中添加页码。

我尝试在 PdfPageEventHelper 中渲染单元格并在 onEndPage() 中写入表,但页码计数没有增加,并且在非常 page 中始终为 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);
 }     

}

我希望页码在每一页的表格单元格中。

【问题讨论】:

  • 您的pageCount ++ 在方法之外。你的代码能编译吗?
  • 此外,您使用pageCount.ToString() 构建表以用作构造函数中的标题。因此,施工时的价值在这里是固定的......
  • A) 无需使用OnStartPage,您可以在OnEndPage 中进行所有操作。 B) 我看到您为每个页面在表格tblAccInfo 中添加新单元格(至少我认为BuildBorderCell 是这样做的),所以显然它会不断增长。为什么不简单地在OnEndPage 中实例化一个新的PdfPTable 实例,填充、绘制和删除它?
  • 通过将tblAccInfo 设置为null 或新的PdfPTable 实例。

标签: c# itext pdfptable


【解决方案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;
  }     

}

【讨论】:

  • 您可能想要添加您的 BuildBorderCell 辅助方法。
【解决方案2】:

您应该重写onStartPage (https://itextsupport.com/apidocs/iText5/5.5.9/com/itextpdf/text/pdf/PdfPageEventHelper.html#onStartPage-com.itextpdf.text.pdf.PdfWriter-com.itextpdf.text.Document-),而不是使用构造函数

有一个来自 Document 类型的参数 document 和一个函数 getPageNumber (https://itextsupport.com/apidocs/iText5/5.5.9/com/itextpdf/text/Document.html#getPageNumber--)

编辑

阅读@mkl 的链接后,我不得不撤回上述内容。 真正的答案来自评论中的@mkl。

【讨论】:

  • itext 开发人员不建议使用onStartPage 添加内容,即使您参考的api 文档也建议使用OnEndPage。并且OnEndPage 具有相同的document 参数...
  • 哪里说不添加内容? Note that if even if a page is not written this method is still called. It is preferable to use onEndPage to avoid infinite loops.Note: do not use Document.add() inside a page event. 听起来不像您可能不使用 OP 使用的 BuildBorderCell 操作。 OnPageEnd 也有 document 参数,OP 曾经使用构造函数而不是 OnPageEnd,尽管我故意不在那里使用它。
  • 我更新了我的代码。我渲染了表 OnStartPage() 而不是构造函数。页数增加了,但在每一页中都创建了一个表。
  • @Mokuyobi 好的,API 文档只给出了提示,不是很清楚。不过,结合 iText 开发人员在此处写的关于堆栈溢出的内容,它总结为 “不要通过 DocumentPdfWriter 或其他任何方式向 onStartPage 中的新文档页面添加任何内容,仅在onEndPage 中添加内容。您可以使用onStartPage 准备内容,但要注意,在某些情况下,最终调用onStartPage 的页面不需要并且不会出现在PDF 中。
  • 阅读例如 Bruno Lowagie 的 this answer,他在其中比在 ApiDocs 或书籍中更清楚地宣布了这一点:您在 OnStartPage() 方法中添加内容。这是禁止的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 2019-05-09
相关资源
最近更新 更多