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

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

我想将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));
      }
   }
c# asp.net itext
1个回答
2
投票

解决这个问题的方法不是创建一个继承自PdfPageEventHelper并重写其方法的类,而是通过创建PdfPTable对象并直接使用WriteSelectedRows方法直接在文档关闭之前将内容写入PDF文档。没有必要将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();
© www.soinside.com 2019 - 2024. All rights reserved.