如何在Itextsharp中以PDF格式在页脚中添加页码

时间:2012-05-09 11:53:28

标签: itextsharp itext

我在ASP.NET代码中使用iTextSharp DLL。我正在将数据提取到数据集中并将数据集添加到PDF表中。

如果我的数据集有100行,那么将在PDF表格中添加100行,并在PDF文件中创建3或4页。

如何在每个页面中添加页脚?

4 个答案:

答案 0 :(得分:18)

您需要从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();
        }
    }

答案 1 :(得分:1)

您必须继承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();
    }
}

答案 2 :(得分:0)

这是另一种方法,在每个页面上制作模板然后在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();
        }

    } 

答案 3 :(得分:0)

如果您只需要一个简单的页码,那么就可以完成此工作:

public class PageHeaderFooter : PdfPageEventHelper
{
    private readonly Font _pageNumberFont = new Font(Font.HELVETICA, 8f, Font.NORMAL, Color.BLACK);

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        AddPageNumber(writer, document);
    }

    private void AddPageNumber(PdfWriter writer, Document document)
    {
        var text = writer.PageNumber.ToString();

        var numberTable = new PdfPTable(1);
        var numberCell = new BorderlessPdfPCell(new Phrase(text, _pageNumberFont)) {HorizontalAlignment = Element.ALIGN_RIGHT};

        numberTable.AddCell(numberCell);
        numberTable.TotalWidth = 50;
        numberTable.WriteSelectedRows(0, -1, document.Right - 80, document.Bottom + 20, writer.DirectContent);
    }
}

您以这种方式使用它:

var stream = new MemoryStream();
var document = new Document(PageSize.A4, 20, 20, 30, 30);

var pdfWriter = PdfWriter.GetInstance(document, stream);
pdfWriter.PageEvent = new PageHeaderFooter();
// rest of PDF generation