How to add headers to specific sections of a PDF using C#
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. This guide demonstrates how to add headers selectively to specific sections of a PDF document, allowing for greater layout control and flexibility in document design.
Steps to add headers to specific sections of a PDF programmatically
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference in your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
- Use the following code sample in Program.cs to add headers to specific sections of a PDF document.
C#
// Initialize the PDF document
PdfDocument document = new PdfDocument();
// Define the font for the header
PdfFont headerfont = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Create the first section with headers
PdfSection sectionWithHeaders = document.Sections.Add();
// Apply header to this section
sectionWithHeaders.Template.Top = CreateHeaderTemplate(headerfont);
// Add 5 pages in the first section
for (int i = 0; i < 5; i++)
{
PdfPage page = sectionWithHeaders.Pages.Add();
DrawContentOnPage(page, $"Content for page {i + 1} in section with headers");
}
// Create the second section without headers
PdfSection sectionWithoutHeaders = document.Sections.Add();
// Add 5 pages in the second section
for (int i = 0; i < 5; i++)
{
PdfPage page = sectionWithoutHeaders.Pages.Add();
DrawContentOnPage(page, $"Content for page {i + 6} in section without headers");
}
// Save the PDF document
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputFileStream);
}
document.Close(true);
- Create the header template in the
CreateHeaderTemplate
method: A PdfPageTemplateElement is created with this rectangle, and header text is drawn using the specified font and a black brush. This template can then be applied to the top of a PDF section to display a consistent header across its pages.
// Creates and returns a header template to be applied to a PDF section
private static PdfPageTemplateElement CreateHeaderTemplate(PdfFont headerfont)
{
// Define the size and position of the header area (X=0, Y=0, Width=500, Height=50)
RectangleF rect = new RectangleF(0, 0, 500, 50);
// Create a page template element using the defined rectangle
PdfPageTemplateElement header = new PdfPageTemplateElement(rect);
// Draw header text at the top-left corner of the header area using the specified font
header.Graphics.DrawString("Header Text", headerfont, PdfBrushes.Black, new PointF(0, 0));
// Return the constructed header template
return header;
}
- Draw the page content in the
DrawContentOnPage
method: where text is rendered onto the PDF page at a fixed vertical position using a standard Helvetica font and black color.
// Draws specified content text onto a PDF page at a fixed position
private static void DrawContentOnPage(PdfPage page, string content)
{
// Get the graphics object for the given PDF page
PdfGraphics graphics = page.Graphics;
// Draw the provided content string using Helvetica font (10pt) at position (X=0, Y=100)
graphics.DrawString(content, new PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.Black, new PointF(0, 100));
}
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation to learn how to add headers and footers in a PDF document.
Conclusion
I hope you enjoyed learning on how to add headers to specific sections of a PDF document.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example 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, Direct-Trac, or feedback portal. We are always happy to assist you!