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

时间:2017-06-20 04:14:16

标签: c# asp.net itext

我想将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));
      }
   }

1 个答案:

答案 0 :(得分:2)

解决这个问题的方法不是创建一个继承自 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();