iTextSharp的创建#的尾页# [英] iTextSharp Creating a Footer Page # of #

查看:869
本文介绍了iTextSharp的创建#的尾页#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建每个使用iTextSharp的在#格式页#以下的iText的页面和书教程PDF文档的页面的页脚。虽然我不断收到对cb.SetFontAndSize异常(阖闾,12); - 对象引用未设置为一个对象。任何人都可以看到的问题? code是如下。

谢谢, 罗布

 公共类MyPdfPageEventHelpPageNo:iTextSharp.text.pdf.PdfPageEventHelper
{
    保护PdfTemplate总和;
    保护BASEFONT阖闾;
    私人布尔settingFont = FALSE;

    公众覆盖无效OnOpenDocument(PdfWriter作家,文档文件)
    {
        总= writer.DirectContent.CreateTemplate(100,100);
        total.BoundingBox =新的Rectangle(-20,-20,100,100);

        阖闾= BaseFont.CreateFont(BaseFont.HELVETICA,BaseFont.WINANSI,BaseFont.NOT_EMBEDDED);
    }

    公众覆盖无效的OnEndPage(PdfWriter作家,文档文件)
    {
        PdfContentByte CB = writer.DirectContent;
        cb.SaveState();
        字符串文本=页面+ writer.PageNumber +的;
        浮textBase = document.Bottom  -  20;
        浮TEXTSIZE = 12; //helv.GetWidthPoint(text,12);
        cb.BeginText();
        cb.SetFontAndSize(阖闾,12);
        如果((writer.PageNumber%2)== 1)
        {
            cb.SetTextMatrix(document.Left,textBase);
            cb.ShowText(文本);
            cb.EndText();
            cb.AddTemplate(总,document.Left + TEXTSIZE,textBase);
        }
        其他
        {
            浮动调整= helv.GetWidthPoint(0,12);
            cb.SetTextMatrix(document.Right  -  TEXTSIZE  - 调整,textBase);
            cb.ShowText(文本);
            cb.EndText();
            cb.AddTemplate(总,document.Right  - 调整,textBase);
        }
        cb.RestoreState();
    }

    公众覆盖无效OnCloseDocument(PdfWriter作家,文档文件)
    {
        total.BeginText();
        total.SetFontAndSize(阖闾,12);
        total.SetTextMatrix(0,0);
        INT的PageNumber = writer.PageNumber  -  1;
        total.ShowText(Convert.ToString(页面编号));
        total.EndText();
    }

}
 

解决方案

下面是一个<一href="http://www.mazsoft.com/blog/post/2008/04/30/$c$c-sample-for-using-iTextSharp-PDF-library.aspx">good例如中添加总页数为每一页。

修改:这是从该网站的情况下,code以往任何时候都消失了:

 使用系统;
使用System.Collections.Generic;
使用System.Text;
使用iTextSharp.text.pdf;
使用iTextSharp.text;
命名空间PDF_Tests
{
    公共类TwoColumnHeaderFooter:PdfPageEventHelper
    {
        //这是作家的contentbyte对象
        PdfContentByte CB;
        //我们就会把页面的最终数量在模板
        PdfTemplate模板;
        //这是BASEFONT我们将使用页眉/页脚
        BASEFONT BF = NULL;
        //这个跟踪的创建时间
        日期时间PrintTime = DateTime.Now;
        #区域属性
        私人字符串_title;
        公共字符串标题
        {
            {返回_title; }
            集合{_title =价值; }
        }

        私人字符串_HeaderLeft;
        公共字符串HeaderLeft
        {
            {返回_HeaderLeft; }
            集合{_HeaderLeft =价值; }
        }
        私人字符串_HeaderRight;
        公共字符串HeaderRight
        {
            {返回_HeaderRight; }
            集合{_HeaderRight =价值; }
        }
        私人字体_HeaderFont;
        公共字体HeaderFont
        {
            {返回_HeaderFont; }
            集合{_HeaderFont =价值; }
        }
        私人字体_FooterFont;
        公共字体FooterFont
        {
            {返回_FooterFont; }
            集合{_FooterFont =价值; }
        }
        #endregion
        //我们覆盖了onOpenDocument方法
        公众覆盖无效OnOpenDocument(PdfWriter作家,文档文件)
        {
            尝试
            {
                PrintTime = DateTime.Now;
                BF = BaseFont.CreateFont(BaseFont.HELVETICA,BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
                CB = writer.DirectContent;
                模板= cb.CreateTemplate(50,50);
            }
            赶上(DocumentException日)
            {
            }
            赶上(System.IO.IOException IOE)
            {
            }
        }

        公众覆盖无效的OnStartPage(PdfWriter作家,文档文件)
        {
            base.OnStartPage(作家,文件);
            矩形的pageSize = document.PageSize;
            如果(标题!=的String.Empty)
            {
                cb.BeginText();
                cb.SetFontAndSize(BF,15);
                cb.SetRGBColorFill(50,50,200);
                cb.SetTextMatrix(pageSize.GetLeft(40),pageSize.GetTop(40));
                cb.ShowText(标题);
                cb.EndText();
            }
            如果(HeaderLeft + HeaderRight!=的String.Empty)
            {
                PdfPTable HeaderTable =新PdfPTable(2);
                HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
                HeaderTable.TotalWidth = pageSize.Width  -  80;
                HeaderTable.SetWidthPercentage(新浮法[] {45,45},pageSize的);

                PdfPCell HeaderLeftCell =新PdfPCell(新词组(8 HeaderLeft,HeaderFont));
                HeaderLeftCell.Padding = 5;
                HeaderLeftCell.PaddingBottom = 8;
                HeaderLeftCell.BorderWidthRight = 0;
                HeaderTable.AddCell(HeaderLeftCell);
                PdfPCell HeaderRightCell =新PdfPCell(新词组(8 HeaderRight,HeaderFont));
                HeaderRightCell.Horizo​​ntalAlignment = 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);
            }
        }
        公众覆盖无效的OnEndPage(PdfWriter作家,文档文件)
        {
            base.OnEndPage(作家,文件);
            INT PAGEN = writer.PageNumber;
            字符串文本=页面+ PAGEN +的;
            浮动的len = bf.GetWidthPoint(文字,8);
            矩形的pageSize = document.PageSize;
            cb.SetRGBColorFill(100,100,100);
            cb.BeginText();
            cb.SetFontAndSize(BF,8);
            cb.SetTextMatrix(pageSize.GetLeft(40),pageSize.GetBottom(30));
            cb.ShowText(文本);
            cb.EndText();
            cb.AddTemplate(模板,pageSize.GetLeft(40)+ len位置,p​​ageSize.GetBottom(30));

            cb.BeginText();
            cb.SetFontAndSize(BF,8);
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
            印上+ PrintTime.ToString()
            pageSize.GetRight(40),
            pageSize.GetBottom(30),0);
            cb.EndText();
        }
        公众覆盖无效OnCloseDocument(PdfWriter作家,文档文件)
        {
            base.OnCloseDocument(作家,文件);
            template.BeginText();
            template.SetFontAndSize(BF,8);
            template.SetTextMatrix(0,0);
            template.ShowText(+(writer.PageNumber  -  1));
            template.EndText();
        }
    }
}
 

和适用的code进行安装:

  //我们的自定义页眉和页脚使用事件处理程序完成
TwoColumnHeaderFooter PageEventHandler =新TwoColumnHeaderFooter();
PDFWriter.PageEvent = PageEventHandler;
//定义页头
PageEventHandler.Title =称号;
PageEventHandler.HeaderFont = FontFactory.GetFont(BaseFont.COURIER_BOLD,10,Font.BOLD);
PageEventHandler.HeaderLeft =集团;
PageEventHandler.HeaderRight =1;
 

I'm trying to create a footer on each of the pages in a PDF document using iTextSharp in the format Page # of # following the tutorial on the iText pages and the book. Though I keep getting an exception on cb.SetFontAndSize(helv, 12); - object reference not set to an object. Can anyone see the issue? Code is below.

Thanks, Rob

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();
    }

}

解决方案

Here's a good example for adding total page number to every page.

EDIT: Here is the code from that site in case it ever goes away:

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();
        }
    }
}

And the applicable code to attach it:

// 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";

这篇关于iTextSharp的创建#的尾页#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆