How to add header to PDF on WPF applications

We often use header to the PDF file to give additional information to the PDF file. With Spire.PDF for WPF, developers can easily use the method SetDocumentTemplate() to create a PDF template that only contains header text and header image. By invoking this method, the template will be applied to all pages in your PDF document. This article will demonstrate how to add a header to PDF in C# on WPF applications.

Note: Before Start, please download the latest version of Spire.PDF and add Spire.Pdf.Wpf.dll in the bin folder as the reference of Visual Studio.

Step 1: Create a new PDF document, set its margin and load from file.

PdfDocument doc = new PdfDocument();
doc.PageSettings.Margins.All = 0;
PdfPageBase page = null;
PdfDocument original = new PdfDocument();
original.LoadFromFile("Test.pdf");

Step 2: Create a SetDocumentTemplate() method to add header text.

SetDocumentTemplate(doc, PdfPageSize.A4, original.PageSettings.Margins);

Step 3: Traverse every page of the original PDF document and create a new page with the original contents.

foreach (PdfPageBase origianlPage in original.Pages)
{
    page = doc.Pages.Add(new SizeF(origianlPage.Size.Width, origianlPage.Size.Height));
    origianlPage.CreateTemplate().Draw(page, 0, -(original.PageSettings.Margins.Top));
}

Step 4: Save the document to file and launch to preview it.

doc.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");

Step 5: Details of how to add the header text to the PDF.

private static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
    PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
    topSpace.Foreground = true;
    doc.Template.Top = topSpace;
    PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, System.Drawing.FontStyle.Italic));
    String label = "This is a PDF text header";
    SizeF size = font1.MeasureString(label);
    float y = 0;
    float x = 0;
    topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x, y);
}

Effective screenshot:

How to add header to PDF on WPF applications

Full codes:

private void button1_Click(object sender, RoutedEventArgs e)
 {
     // Create a pdf document.
     PdfDocument doc = new PdfDocument();
     doc.PageSettings.Margins.All = 0;
     PdfPageBase page = null;
     PdfDocument original = new PdfDocument();

     original.LoadFromFile("Test.pdf");

     SetDocumentTemplate(doc, PdfPageSize.A4, original.PageSettings.Margins);
     foreach (PdfPageBase origianlPage in original.Pages)
     {
         page = doc.Pages.Add(new SizeF(origianlPage.Size.Width, origianlPage.Size.Height));
         origianlPage.CreateTemplate().Draw(page, 0, -(original.PageSettings.Margins.Top));

     }

     doc.SaveToFile("output.pdf");
     System.Diagnostics.Process.Start("output.pdf");

 }
 private static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
 {
     PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
     topSpace.Foreground = true;
     doc.Template.Top = topSpace;
     PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, System.Drawing.FontStyle.Italic));
     String label = "This is a PDF text header";
     SizeF size = font1.MeasureString(label);
     float y = 0;
     float x = 0;
     topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x, y);
 }