How to skip header/footer for HTML page to WinForms PDF?
The Syncfusion® HTML to PDF converter is a .NET PDF library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, texts, fonts, and the layout of the original HTML document or webpage. Using this library, you can convert an HTML to PDF in C# and VB.NET.
Using this library, you can skip the Header and Footer for the first page of the PDF document during the HTML to PDF conversion using C#.
Steps to skip header and footer for the first page of the PDF document during the HTML conversion programmatically
- Create a new C# console application project.
- Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.HtmlConverter; using Syncfusion.Pdf.Graphics; using System.Drawing;
- Use the following code sample to convert HTML to PDF without the Header and Footer.
C#
//Initialize HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); //Convert URL to PDF PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com"); MemoryStream stream = new MemoryStream(); document.Save(stream); //Add the header and footer PdfDocument document1 = HeaderFooter(stream); //Save the document document1.Save("NoHeaderFooter.pdf"); //Close the document document1.Close(); //This will open the PDF file so, the result will be seen in the default PDF viewer Process.Start("NoHeaderFooter.pdf");
- Use the following code sample to add the header and footer from the second page of the PDF document.
C#
private static PdfDocument HeaderFooter(MemoryStream stream) { //Load the existing PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); //Load the page PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage; // Get the first page of the converter PDF as a template PdfTemplate template = loadedPage.CreateTemplate(); //Create a new PDF document PdfDocument document = new PdfDocument(); //Add the page PdfPage page = document.Pages.Add(); //Create the graphics PdfGraphics graphics = page.Graphics; //Draw the template graphics.DrawPdfTemplate(template, PointF.Empty, new SizeF(page.Size.Width, page.Size.Height)); //Add a section- Header and footer can be added to this section PdfSection section = document.Sections.Add(); for (int i = 1; i < loadedDocument.Pages.Count; i++) { //Add pages to section section.Pages.Add(); } //Add the header template at the top of the section section.Template.Top = AddHeader(document); //Add the footer template at the bottom of the section section.Template.Bottom = AddFooter(document); //Draw the remaining pages of the converted PDF document as a template. for (int i = 1; i < loadedDocument.Pages.Count; i++) { PdfLoadedPage loadedPage1 = loadedDocument.Pages[i] as PdfLoadedPage; //Create the template from the page. PdfTemplate template1 = loadedPage1.CreateTemplate(); document.Pages[i].Graphics.DrawPdfTemplate(template1, PointF.Empty, new SizeF(page.Size.Width, page.Size.Height)); } return document; }
- Add the following code in the AddHeader and AddFooter method to add the header and footer to the section.
C#
private static PdfPageTemplateElement AddHeader(PdfDocument doc) { RectangleF bounds = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template for header PdfPageTemplateElement header = new PdfPageTemplateElement(bounds); //Draw the rectangle in header header.Graphics.DrawRectangle(PdfPens.DarkBlue, bounds); //Draw the image in header PdfImage image = new PdfBitmap(@"Logo.png"); SizeF imageSize = new SizeF(110f, 35f); PointF imageLocation = new PointF(doc.Pages[0].GetClientSize().Width - imageSize.Width - 20, 5); header.Graphics.DrawImage(image, imageLocation, imageSize); return header; } private static PdfPageTemplateElement AddFooter(PdfDocument doc) { RectangleF bounds = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template that can be used as footer PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7); PdfBrush brush = new PdfSolidBrush(Color.Black); //Create the page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create the page count field PdfPageCountField count = new PdfPageCountField(font, brush); //Add the fields in composite fields PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0}", pageNumber); compositeField.Bounds = footer.Bounds; //Draw the composite field in footer compositeField.Draw(footer.Graphics, new PointF(470, 40)); return footer; }
A complete working sample can be downloaded from HtmlToPDF_NoHeaderFirstPage.zip.
Take a moment to peruse the documentation, where you can find how to add the header and footer in a PDF document with code example.
An online sample link for Converting HTML to PDF.
Refer here to explore a rich set of Syncfusion Essential® PDF features.
Conclusion