How to add different header for first page in a PDF document
By creating different PdfSection objects for the first page and other pages, different headers can be added for the first page in a PDF document.
Assemblies Required
1) Syncfusion.Compression.Base.dll
2) Syncfusion.Pdf.Base.dll
The below code snippet illustrates how to add different header for the first page.
Code Snippet:
// Create a new instance of PdfDocument class. PdfDocument document = new PdfDocument(); PdfSection section1 = document.Sections.Add(); // Add the first page using first section section1.Pages.Add(); // Add the section PdfSection section2 = document.Sections.Add(); section2.Pages.Add(); section2.Pages.Add(); RectangleF rect = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 140); // Create the template PdfPageTemplateElement header = new PdfPageTemplateElement(rect); header.Graphics.DrawRectangle(PdfPens.Red, rect); header.Graphics.DrawString("First Page Header", new PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Red, new PointF(10, 10)); section1.Template.Top = header; // Create the template PdfPageTemplateElement header1 = new PdfPageTemplateElement(rect); header1.Graphics.DrawRectangle(PdfPens.Red, rect); header1.Graphics.DrawString("Header Template", new PdfStandardFont(PdfFontFamily.TimesRoman, 12), PdfBrushes.Red, new PointF(10, 10)); section2.Template.Top = header1; // Save and close the document. document.Save("Sample.pdf"); document.Close(true);
The working sample can be downloaded from the below link:
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about How to add different header for first page in a PDF document.
You can refer to our PDF feature tour page to learn about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!