How to create Header in PDF generation using C# iText7

随声附和 提交于 2020-01-25 06:56:10

问题


I tried to add Header in all the pages of my PDF using iText package in .NET C#, but it's not working.

Note: I'm using iText version 7

I tried the following Stack Overflow answers, they demonstrated only for Footer (i.e., END_PAGE) not for START_PAGE

  • Add Header and Footer for PDF using iTextsharp

My Code:

class Program {
    static void Main(string[] args) {

        var writer = new PdfWriter("E:/pdfSample/bala.pdf");
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf,PageSize.A4);
        document.SetMargins(120,40,100,40);

        // Create a PdfFont
        var font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);

        List list = new List();

        // Add ListItem objects
        list.Add(new ListItem("Never gonna give you up"))
            .Add(new ListItem("Never gonna let you down"))    
            .Add(new ListItem("Never gonna run around and desert you"))
            .Add(new ListItem("Never gonna make you cry"))
            .Add(new ListItem("Never gonna say goodbye"))
            .Add(new ListItem("Never gonna tell a lie and hurt you"));
        // Add the list
        document.Add(list);

        // Add a Paragraph
        document.Add(new Paragraph("iText is:").SetFont(font));


        document.Add(logo);
        var tree = new Image(ImageDataFactory.CreateJpeg(new Uri("E:/pdfSample/RelatednessTree.jpg")));
        // document.Add(tree);

        pdf.AddEventHandler(PdfDocumentEvent.START_PAGE,new Paragraph("HEADER TEXT"));
        pdf.AddEventHandler(PdfDocumentEvent.END_PAGE,new Paragraph("FOOTER TEXT"));

        document.Close();

    }
}


public class TableFooter : IEventHandler {
    private Paragraph para;

    public TableFooter(Paragraph paraObj) {
        para = paraObj;
    }

    public void HandleEvent(Event @event) {
        PdfDocumentEvent docEvent = @event as PdfDocumentEvent;
        PdfDocument doc = docEvent.GetDocument();
        PdfPage page = docEvent.GetPage();
        PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(),page.GetResources(),doc);

        new Canvas(canvas,doc, new Rectangle(20, -20, page.GetPageSize().GetWidth() - 80,80)).Add(para);
    }
}

Kindly assist me how to add Header (i.e., START_PAGE) and whats the configuration is required to display the Header in all the PDF pages.


回答1:


I have did this in past using PdfEventHandler by overriding methods like this.

class PdfEvents : PdfPageEventHelper
    {

        public override void OnStartPage(PdfWriter writer, Document document)
        {

        }

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

        }
    }

and then you can write your content by getting exact area in pdf using :

table.WriteSelectedRows(2, -1, 0, -1, 36, 806, canvas);



回答2:


I created two classes in my code, one for header and one for footer, so i call the two elements like this:

pdf.AddEventHandler(PdfDocumentEvent.START_PAGE,new HeaderClass("HEADER TEXT"));
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE,new FooterClass("FOOTER TEXT"));

In the HandleEvent for the header class i changed this line:

PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(),page.GetResources(),doc);

with this:

PdfCanvas canvas = new PdfCanvas(page.NewContentStreamAfter(),page.GetResources(),doc);

leaving the structure you have coded, but, obviously I changed the page coordinates of canvas.



来源:https://stackoverflow.com/questions/52855077/how-to-create-header-in-pdf-generation-using-c-sharp-itext7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!