sazr
sazr

Reputation: 25928

Adding Header and Footer Images to PDF: Header image doesn't show, Footer is scaled up

I am attempting to create a simple .pdf using the libary ITextSharp. I am making a .pdf where the header & footer have images in them and the header margin is 300px & the footer margin is 664px.

My Problem: My code doesn't insert the header image for some reason and the footer image is blown up/scaled up in size for some reason.

Can you tell me whats wrong with my code. The header image should extend the whole width of an A4 page & be 300px in height. The footer image should extend the whole width of the page & be 664px in height. Both images dont need to be resized they are already the whole width of the page & the correct heights.

public class itsEventsHandler : PdfPageEventHelper
{
    PdfTemplate total;
    BaseFont helv;

    // I am following a tutorial & they said that if I want to create headers/footers when each page is created 
    // that I should override the OnEndPage() not the OnStartPage() is that correct?
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        // Post: When each new page is created, add a header & footer image to the page. And set the top margin to 370px
        //       and the bottom margin to 664px.
        // Result: The function executes but the pdf's header image isn't visible & the footer looks resized(scaled up in size).

        //Footer Image 
        iTextSharp.text.Image imgfoot = iTextSharp.text.Image.GetInstance(resolvePath("~/images/pdf/bottomBorder.jpg"));
        //Header Image 
        iTextSharp.text.Image imghead = iTextSharp.text.Image.GetInstance(resolvePath("~/images/pdf/topBorder.jpg"));

        imgfoot.SetAbsolutePosition(0, 0);
        imghead.SetAbsolutePosition(0, 0);

        PdfContentByte cbhead = writer.DirectContent;
        PdfTemplate tp = cbhead.CreateTemplate(2480, 370); // units are in pixels but I'm not sure if thats the correct units
        tp.AddImage(imghead);

        PdfContentByte cbfoot = writer.DirectContent;
        PdfTemplate tpl = cbfoot.CreateTemplate(2480, 664); 
        tpl.AddImage(imgfoot);

        cbhead.AddTemplate(tp, 0, 0);
        cbfoot.AddTemplate(tpl, 0, 0);

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

        /*PdfContentByte cb = writer.DirectContent;
        cbfoot.SaveState();
        document.SetMargins(35, 35, 100, 82);
        cb.RestoreState();*/

        //document.NewPage(); 
        base.OnStartPage(writer, document);
    }

    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        total = writer.DirectContent.CreateTemplate(100, 100);
        total.BoundingBox = new iTextSharp.text.Rectangle(-20, -20, 100, 100);
        helv = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    }
}

// My code to create the pdf
 // Create a Document object
var document = new Document(PageSize.A4, 50, 50, 370, 664);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
writer.PageEvent = new itsEventsHandler();
// Open the Document for writing
document.Open();
// add some paragrahs
document.Close();

Upvotes: 4

Views: 10092

Answers (1)

ionden
ionden

Reputation: 12776

When you are adding the templates check their location. For example:

cbhead.AddTemplate(tp, 0, 715);
cbfoot.AddTemplate(tpl, 0, 0);

Hope this helps!

Upvotes: 4

Related Questions