Search code examples
c#selectpdf

Is there a way to remove the top margin on the first page only, in PDFs generated by SelectPDF?


I'm trying to generate a PDF report using SelectPDF. The report is multi-page long, and as such I need pagebreaks. Whenever these pagebreaks show up, I need top and bottom margins so the report won't look ugly, but I can only find the option to add margins for every page or no pages, but not "every page except the first one".

The source of the report is HTML/CSS that I have set up. The contents are dynamic, so I cannot manually control the text... The greatest granularity I have is controlling the CSS.

HtmlToPdf converter = new HtmlToPdf();
converter.Options.PdfPageSize = PdfPageSize.A4;
converter.Options.PageBreaksEnhancedAlgorithm = true;
converter.Options.MarginTop = 20;
converter.Options.MarginBottom = 20;

The "MarginTop" option adds the top margin to every single page. It works excellently for every page except for the first one, where I need it to be 0, but I can't find any options to do that. Does it exist?


Solution

  • It's not possible to do that using margins. You can however workaround this using headers/footers:

    HtmlToPdf converter = new HtmlToPdf();
    converter.Options.PdfPageSize = PdfPageSize.A4;
    converter.Options.PageBreaksEnhancedAlgorithm = true;
    
    converter.Options.DisplayHeader = true;
    converter.Header.Height = 20;
    converter.Header.DisplayOnFirstPage = false;
    
    converter.Options.DisplayFooter = true;
    converter.Footer.Height = 20;