Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在每页的页眉单元格中添加页码_C#_Itext_Pdfptable - Fatal编程技术网

C# 在每页的页眉单元格中添加页码

C# 在每页的页眉单元格中添加页码,c#,itext,pdfptable,C#,Itext,Pdfptable,我正在创建一个pdf文档。有一个PdfPTable,其中的一些行将在每页中重复。我想要的是在每页的表格单元格中添加页码 我尝试在PdfPageEventHelper中呈现单元格,并在onEndPage()中写入表,但页码计数并没有增加,在每个页面中总是1 我怎样才能做到这一点 [更新] 我在startpage()上呈现了表,而不是构造函数。我使用writer.PageNumber进行页面计数。页码现在增加了,但表格在每页中都会反复出现 public class itsEventsHandler

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

我尝试在PdfPageEventHelper中呈现单元格,并在onEndPage()中写入表,但页码计数并没有增加,在每个页面中总是1

我怎样才能做到这一点

[更新]

我在startpage()上呈现了表,而不是构造函数。我使用writer.PageNumber进行页面计数。页码现在增加了,但表格在每页中都会反复出现

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方法。