Reputation: 76547
I've been working with ITextSharp and using it to generate PDFs - however I recently encountered an issue where the top margin of all pages beyond the first seem to be ignored and as a result writing occurs over my header.
Current margins:
document.SetMargins(72,72, 36, 36);
It should be noted that the left and right margins work perfectly beyond the first page - however it seems to ignore the top margin and begin the text at the top of the page (where the header occurs).
Does anyone have any insights as to why this might occur? Or is there an area or event that I could use to reinforce the document margins for each page? (Perhaps the OnStartPage
event?)
I would be glad to provide any additional code if needed - as I know that ITextSharp can become very convoluted code-wise.
Upvotes: 3
Views: 2870
Reputation: 2711
If you want different margins on different, I hooked the OnPageStart event and set the margin there depending on the page number.
internal class DocumentEvents : PdfPageEventHelper
{
/// <summary>
/// Called when [start page].
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="document">The document.</param>
public override void OnStartPage(PdfWriter writer, Document document)
{
if (document.PageNumber == 1)
{
document.SetMargins(40f, 40f, 130f, 20f);
}
else
{
document.SetMargins(40f, 40f, 30f, 30f);
}
document.NewPage();
}
}
Don't forget to wire up your event to the document writer:
this.writer.PageEvent = new DocumentEvents();
Upvotes: 3
Reputation: 76547
Resolved the issue as per Alexis Pigeon's instructions of including the Header and Footers into the OnEndPage
event as opposed to each of them being in a separate event. I also adjusted the margins (increasing the bottom margin) to prevent the overwriting.
Upvotes: 2