C# 如何在最后一页中添加PdfPTable作为页脚?

C# 如何在最后一页中添加PdfPTable作为页脚?,c#,asp.net,itext,C#,Asp.net,Itext,我只想将PdfPTable作为页脚添加到生成的PDF文档的最后一页。该表将有4列宽度相同的列。我已经尝试了我在这里和其他网站上找到的其他解决方案,但都无济于事。我已将底部边距设置为不同的值,以便为页脚留出空间,并尝试了几种添加PDF页脚的技巧,但仍然没有显示任何内容。 感谢您的帮助 这是我的密码: // The document creation Document prDoc = new Document(PageSize.A4, 15f, 15f, 10f, 50f); P

我只想将PdfPTable作为页脚添加到生成的PDF文档的最后一页。该表将有4列宽度相同的列。我已经尝试了我在这里和其他网站上找到的其他解决方案,但都无济于事。我已将底部边距设置为不同的值,以便为页脚留出空间,并尝试了几种添加PDF页脚的技巧,但仍然没有显示任何内容。 感谢您的帮助

这是我的密码:

   // The document creation

   Document prDoc = new Document(PageSize.A4, 15f, 15f, 10f, 50f);
   PdfWriter pdfWriter = PdfWriter.GetInstance(prDoc, Response.OutputStream);

   // The event manager
   prPageEvent pdfEvent = new prPageEvent();
   pdfWriter.PageEvent = pdfEvent;
   prDoc.Open();
   .
   .
   .
   public class prPageEvent : PdfPageEventHelper {
      public PdfTemplate template;
      public PdfContentByte cb;
      BaseFont bf = null;

      public override void OnOpenDocument(PdfWriter writer, Document document) {
         try {
            bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb = writer.DirectContent;
            template = cb.CreateTemplate(PageSize.A4.Width, 50);
         }
         catch (DocumentException de) {
         }
         catch (System.IO.IOException ioe) {
         }
      }

      public override void OnCloseDocument(PdfWriter writer, Document document) {
         base.OnCloseDocument(writer, document);

         PdfPTable f = new PdfPTable(4);
         f.TotalWidth = document.PageSize.Width - document.LeftMargin;

         f.AddCell(new PdfPCell(new Phrase("first")));
         f.AddCell(new PdfPCell(new Phrase("second")));
         f.AddCell(new PdfPCell(new Phrase("third")));
         f.AddCell(new PdfPCell(new Phrase("fourth")));
         f.WriteSelectedRows(0, -1, document.LeftMargin+5, document.PageSize.Height-35, writer.DirectContent);

         // Only the following simple text has worked so far, but I cannot set 
         // the exact width to each column with this approach.

         //template.BeginText();
         // template.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false), 8);
         // template.SetTextMatrix(0, 40);
         // template.ShowText("first   second   third   fourth");
         //template.EndText();
      }

      public override void OnEndPage(PdfWriter writer, Document document) {
         base.OnEndPage(writer, document);
         BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
         System.String text = "OnEndPage";
         float len = bf.GetWidthPoint(text, 8);
         iTextSharp.text.Rectangle pageSize = document.PageSize;
         cb.BeginText();
         cb.SetFontAndSize(bf, 8);
         cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
         cb.EndText();
         cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
      }
   }

解决方法不是创建一个继承自PdfPageEventHelper的类并重写其方法,而是在文档关闭之前直接在PDF文档中写入内容,方法是创建一个PdfPTable对象,并使用WriteSelectedRows方法直接写入。 对于只需要在文档末尾出现一次的内容,无需使用OnCloseDocument方法

我的最终代码:

Immediately after printing the header and the content...

// Table for final footer.
float[] cols = new float[] { 137, 10, 137, 10, 137, 10, 127 };
PdfPTable f = new PdfPTable(7);
f.SetWidthPercentage(cols, PageSize.A4);

// Populate the table
f.AddCell(pdfUtils.newCell("Lorem", detail_font_size, PdfPCell.ALIGN_CENTER, 1) );
f.AddCell(pdfUtils.newCell(" ", detail_font_size, PdfPCell.ALIGN_CENTER, 0));
f.AddCell(pdfUtils.newCell("Ipsum", detail_font_size, PdfPCell.ALIGN_CENTER, 1));
f.AddCell(pdfUtils.newCell(" ", detail_font_size, PdfPCell.ALIGN_CENTER, 0));
f.AddCell(pdfUtils.newCell("exquisitaque", detail_font_size, PdfPCell.ALIGN_CENTER, 1));
f.AddCell(pdfUtils.newCell(" ", detail_font_size, PdfPCell.ALIGN_CENTER, 0));
f.AddCell(pdfUtils.newCell("responsum", detail_font_size, PdfPCell.ALIGN_CENTER, 1));

// Write the footer using absolute positioning.
f.WriteSelectedRows(0, -1, prDoc.LeftMargin, 75, pdfWriter.DirectContent);

// Close the document
pdfWriter.CloseStream = false;
prDoc.Close();
我也有类似的例子