如何在页脚中的PDF由iTextSharp的添加页码(How to Add Page number

2019-06-24 07:16发布

我在ASP.NET代码中使用iTextSharp的DLL。 我获取数据到数据集和数据集添加到PDF表格。

如果我的数据集有更多的100行,然后100行会被添加到PDF表和3或4页将在PDF文件中创建。

如何添加页脚在每一页?

Answer 1:

你需要从PdfPageEventHelper类继承,并覆盖在下面的代码片段显示的方法:

Document doc = new Document(PageSize.A4.Rotate());        

using (MemoryStream ms = new MemoryStream())
{
  PdfWriter writer = PdfWriter.GetInstance(doc, ms);
  PageEventHelper pageEventHelper = new PageEventHelper();
  writer.PageEvent = pageEventHelper;
}

 public class PageEventHelper : PdfPageEventHelper
    {
        PdfContentByte cb;
        PdfTemplate template;


        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            cb = writer.DirectContent;
            template = cb.CreateTemplate(50, 50);
        }

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

            int pageN = writer.PageNumber;
            String text = "Page " + pageN.ToString() + " of ";
            float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size);

            iTextSharp.text.Rectangle pageSize = document.PageSize;

            cb.SetRGBColorFill(100, 100, 100);

            cb.BeginText();
            cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
            cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
            cb.ShowText(text);

            cb.EndText();

            cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin));
        }

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

            template.BeginText();
            template.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
            template.SetTextMatrix(0, 0);
            template.ShowText("" + (writer.PageNumber - 1));
            template.EndText();
        }
    }


Answer 2:

你必须继承pageeventhepler添加到动态生成的页面数量的PDF页面。

public void printPdf()
{
    Document doc = new Document(PageSize.A4.Rotate());
    using (MemoryStream ms = new MemoryStream())
    {
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        PageEventHelper pageEventHelper = new PageEventHelper();
        writer.PageEvent = pageEventHelper;
    }
}

public class PageEventHelper : PdfPageEventHelper
{
    PdfContentByte cb;
    PdfTemplate template;


    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        cb = writer.DirectContent;
        template = cb.CreateTemplate(50, 50);
    }

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

        int pageN = writer.PageNumber;
        String text = "Page " + pageN.ToString() + " of ";
        float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size);

        iTextSharp.text.Rectangle pageSize = document.PageSize;

        cb.SetRGBColorFill(100, 100, 100);

        cb.BeginText();
        cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
        cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
        cb.ShowText(text);

        cb.EndText();

        cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin));
    }

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

        template.BeginText();
        template.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
        template.SetTextMatrix(0, 0);
        template.ShowText("" + (writer.PageNumber - 1));
        template.EndText();
    }
}


Answer 3:

这是另一种方法,使每个网页上的模板,然后在onCloseDocument更新这些模板,看看下面的代码,我使用iTextSharp的7

 class PDFBackgroundHelper : PdfPageEventHelper
{

    private PdfContentByte cb;
    private List<PdfTemplate> templates;
    //constructor
    public PDFBackgroundHelper()
    {
        this.templates = new List<PdfTemplate>();
    }

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

        cb = writer.DirectContentUnder;
        PdfTemplate templateM = cb.CreateTemplate(50, 50);
        templates.Add(templateM);

        int pageN = writer.CurrentPageNumber;
        String pageText = "Page " + pageN.ToString() +" of ";
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        float len = bf.GetWidthPoint(pageText, 10);
        cb.BeginText();
        cb.SetFontAndSize(bf, 10);
        cb.SetTextMatrix(document.LeftMargin, document.PageSize.GetBottom(document.BottomMargin));
        cb.ShowText(pageText);
        cb.EndText();
        cb.AddTemplate(templateM, document.LeftMargin + len, document.PageSize.GetBottom(document.BottomMargin));
    }

    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
        base.OnCloseDocument(writer, document);
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        foreach (PdfTemplate item in templates)
        {
            item.BeginText();
            item.SetFontAndSize(bf, 10);
            item.SetTextMatrix(0, 0);
            item.ShowText("" + (writer.PageNumber));
            item.EndText();
        }

    } 


文章来源: How to Add Page number in Footer in PDF by Itextsharp