在每个页面的标题单元格中添加页码

问题描述 投票:1回答:2

我正在创建一个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);
 }     

}

我希望页面编号在每个页面的表格单元格中。

c# itext pdfptable
2个回答
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;
  }     

}

0
投票

你应该覆盖onStartPagehttps://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具有函数getPageNumberhttps://itextsupport.com/apidocs/iText5/5.5.9/com/itextpdf/text/Document.html#getPageNumber--

编辑

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

© www.soinside.com 2019 - 2024. All rights reserved.