Is it possible to set a background image for all pages using PdfSharp/Migradoc?
Asked Answered
E

1

6

I'm using PDFSharp/Migradoc to generate PDFs from my web application, and I've managed to get a background image working for the first page of the document.

I start by creating a single section in the document, then rendering an image to this. I then write the document content to paragraph objects inside the same section object.

However, I need to show a different background image for all subsequent pages in the document.

Is this possible? If so, how can I do it?

Answer: In the interest of providing a complete answer, here is the basic code that will get this working:

Section section = this.document.AddSection();

section.PageSetup.DifferentFirstPageHeaderFooter = true;
section.PageSetup.OddAndEvenPagesHeaderFooter = false;

Image firstPageImage = section.Headers.FirstPage.AddImage("firstPage.jpg");
// ...configure image...
Image otherPageImage = section.Headers.Primary.AddImage("everyOtherPage.jpg");
// ...configure image...
Erythro answered 23/1, 2011 at 20:18 Comment(2)
thanks for posting the code :)Ima
Thanks, I was trying to use the FirstPage stuff and couldn't work out why it wasn't working. Hadn't added the DifferentFirstPageHeaderFooter = true.Roe
P
1

If you draw the background image as part of the Header or Footer, then you can use the default Header with the "normal" picture and a different first page header for the first page.

Propositus answered 24/1, 2011 at 9:41 Comment(2)
Hi; thanks for the answer. Can you confirm that if I render the image to the header, then it will take up the entire page?Erythro
Nevermind - I managed to get it working. I posted the code so others might learn from it.Erythro

© 2022 - 2024 — McMap. All rights reserved.