Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the margin for the second page in a PDF using iTextsharp?

Tags:

itextsharp

Is there a way to change the page margins for the second page in a PDF using iTextSharp?

I now have:

Document document = new Document(PageSize.A4, 144f, 72f, 144f, 90f);

PdfWriter.GetInstance(document, ms);

/* first page content */

document.NewPage();
document.SetMargins(72f, 72f, 72f, 100f);

/* second page content */

However, the margins on the second page are the ones set for the first page.

like image 780
jao Avatar asked Dec 08 '22 21:12

jao


1 Answers

Switch the two lines:

document.SetMargins(72f, 72f, 72f, 100f);
document.NewPage();

As documented, the NewPage() function performs a lot of initialisations, among others, setting the margins. So you need to change the margins BEFORE triggering a new page, not after.

like image 184
Bruno Lowagie Avatar answered Jan 05 '23 00:01

Bruno Lowagie