iTextSharp 创建页脚页 # of #

Posted

技术标签:

【中文标题】iTextSharp 创建页脚页 # of #【英文标题】:iTextSharp Creating a Footer Page # of # 【发布时间】:2010-11-05 04:18:59 【问题描述】:

我正在尝试使用 iTextSharp 在 PDF 文档的每个页面上创建一个页脚,格式为 Page # of # 按照 iText 页面和书中的教程。虽然我一直在 cb.SetFontAndSize(helv, 12); - 对象引用未设置为对象。任何人都可以看到这个问题吗?代码如下。

谢谢, 抢

public class MyPdfPageEventHelpPageNo : iTextSharp.text.pdf.PdfPageEventHelper

    protected PdfTemplate total;
    protected BaseFont helv;
    private bool settingFont = false;

    public override void OnOpenDocument(PdfWriter writer, Document document)
    
        total = writer.DirectContent.CreateTemplate(100, 100);
        total.BoundingBox = new Rectangle(-20, -20, 100, 100);

        helv = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    

    public override void OnEndPage(PdfWriter writer, Document document)
    
        PdfContentByte cb = writer.DirectContent;
        cb.SaveState();
        string text = "Page " + writer.PageNumber + " of ";
        float textBase = document.Bottom - 20;
        float textSize = 12; //helv.GetWidthPoint(text, 12);
        cb.BeginText();
        cb.SetFontAndSize(helv, 12);
        if ((writer.PageNumber % 2) == 1)
        
            cb.SetTextMatrix(document.Left, textBase);
            cb.ShowText(text);
            cb.EndText();
            cb.AddTemplate(total, document.Left + textSize, textBase);
        
        else
        
            float adjust = helv.GetWidthPoint("0", 12);
            cb.SetTextMatrix(document.Right - textSize - adjust, textBase);
            cb.ShowText(text);
            cb.EndText();
            cb.AddTemplate(total, document.Right - adjust, textBase);
        
        cb.RestoreState();
    

    public override void OnCloseDocument(PdfWriter writer, Document document)
    
        total.BeginText();
        total.SetFontAndSize(helv, 12);
        total.SetTextMatrix(0, 0);
        int pageNumber = writer.PageNumber - 1;
        total.ShowText(Convert.ToString(pageNumber));
        total.EndText();
    


【问题讨论】:

OnDocumentLoad 是否真的在 OnPageEnd 之前被调用? 单步执行代码时,是否正确定义了 helv? 我看得太深了,错过了显而易见的事情,你们都是对的,OnOpenDocument 没有被调用,所以在 OnEndPage helv 什么都不是。我将其追溯到一个愚蠢的错误,因为我在主流程中构建页面事件太晚了。感谢您的帮助。 【参考方案1】:

这是一个good example,用于将总页码添加到每个页面。

编辑:这是该网站的代码,以防它越来越消失:

using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;
namespace PDF_Tests

    public class TwoColumnHeaderFooter :PdfPageEventHelper
    
        // This is the contentbyte object of the writer
        PdfContentByte cb;
        // we will put the final number of pages in a template
        PdfTemplate template;
        // this is the BaseFont we are going to use for the header / footer
        BaseFont bf = null;
        // This keeps track of the creation time
        DateTime PrintTime = DateTime.Now;
        #region Properties
        private string _Title;
        public string Title
        
            get  return _Title; 
            set  _Title = value; 
        

        private string _HeaderLeft;
        public string HeaderLeft
        
            get  return _HeaderLeft; 
            set  _HeaderLeft = value; 
        
        private string _HeaderRight;
        public string HeaderRight
        
            get  return _HeaderRight; 
            set  _HeaderRight = value; 
        
        private Font _HeaderFont;
        public Font HeaderFont
        
            get  return _HeaderFont; 
            set  _HeaderFont = value; 
        
        private Font _FooterFont;
        public Font FooterFont
        
            get  return _FooterFont; 
            set  _FooterFont = value; 
        
        #endregion
        // we override the onOpenDocument method
        public override void OnOpenDocument(PdfWriter writer, Document document)
        
            try
            
                PrintTime = DateTime.Now;
                bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                cb = writer.DirectContent;
                template = cb.CreateTemplate(50, 50);
            
            catch (DocumentException de)
            
            
            catch (System.IO.IOException ioe)
            
            
        

        public override void OnStartPage(PdfWriter writer, Document document)
        
            base.OnStartPage(writer, document);
            Rectangle pageSize = document.PageSize;
            if (Title != string.Empty)
            
                cb.BeginText();
                cb.SetFontAndSize(bf, 15);
                cb.SetRGBColorFill(50, 50, 200);
                cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
                cb.ShowText(Title);
                cb.EndText();
            
            if (HeaderLeft + HeaderRight != string.Empty)
            
                PdfPTable HeaderTable = new PdfPTable(2);
                HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
                HeaderTable.TotalWidth = pageSize.Width - 80;
                HeaderTable.SetWidthPercentage(new float[]  45, 45 , pageSize);

                PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
                HeaderLeftCell.Padding = 5;
                HeaderLeftCell.PaddingBottom = 8;
                HeaderLeftCell.BorderWidthRight = 0;
                HeaderTable.AddCell(HeaderLeftCell);
                PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
                HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                HeaderRightCell.Padding = 5;
                HeaderRightCell.PaddingBottom = 8;
                HeaderRightCell.BorderWidthLeft = 0;
                HeaderTable.AddCell(HeaderRightCell);
                cb.SetRGBColorFill(0, 0, 0);
                HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(40), pageSize.GetTop(50), cb);
            
        
        public override void OnEndPage(PdfWriter writer, Document document)
        
            base.OnEndPage(writer, document);
            int pageN = writer.PageNumber;
            String text = "Page " + pageN + " of ";
            float len = bf.GetWidthPoint(text, 8);
            Rectangle pageSize = document.PageSize;
            cb.SetRGBColorFill(100, 100, 100);
            cb.BeginText();
            cb.SetFontAndSize(bf, 8);
            cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
            cb.ShowText(text);
            cb.EndText();
            cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));

            cb.BeginText();
            cb.SetFontAndSize(bf, 8);
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, 
            "Printed On " + PrintTime.ToString(), 
            pageSize.GetRight(40), 
            pageSize.GetBottom(30), 0);
            cb.EndText();
        
        public override void OnCloseDocument(PdfWriter writer, Document document)
        
            base.OnCloseDocument(writer, document);
            template.BeginText();
            template.SetFontAndSize(bf, 8);
            template.SetTextMatrix(0, 0);
            template.ShowText("" + (writer.PageNumber - 1));
            template.EndText();
        
    

以及附加它的适用代码:

// Our custom Header and Footer is done using Event Handler
TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter();
PDFWriter.PageEvent = PageEventHandler;
// Define the page header
PageEventHandler.Title = Title;
PageEventHandler.HeaderFont = FontFactory.GetFont(BaseFont.COURIER_BOLD, 10, Font.BOLD);
PageEventHandler.HeaderLeft = "Group";
PageEventHandler.HeaderRight = "1";

【讨论】:

你可以评论你为什么从pagenumber中减去一个吗?我曾经这样做过,但最近它会给出不正确的结果,并且我已经删除了减法。 iTextSharp 有变化吗?【参考方案2】:

上面的代码是正确的,唯一的问题是,它没有被执行。为此,您必须在为下载 PDf 编写代码的地方创建一个实例。 示例:

MyPdfPageEventHelpPageNo pageeventhandler = new MyPdfPageEventHelpPageNo();
writer.PageEvent = pageeventhandler;

writer 表示 PDF writer(一个实例)。

【讨论】:

【参考方案3】:

在您覆盖的每个方法中,您都可以添加对其基方法的调用:

base.OnStartPage(writer, document);
base.OnEndPage(writer, document);
base.OnCloseDocument(writer, document);

【讨论】:

以上是关于iTextSharp 创建页脚页 # of #的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iTextSharp 字符串中使用 HTML 标签

如何从pdf文件中查找页眉页脚

Word2007 页眉加页码 格式为page of ,注page后数随页数改变,谢谢!

Word2007 页眉加页码 格式为page of ,注page后数随页数改变,谢谢!

iTextSharp:创建 PDF 文件时显示“打开/保存”对话框

在 laravel 中没有得到布局页面的输出