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);
 }

Set up page borders in word in WPF

2016-03-18 01:14:32 Written by Koohji

A page border is a border that appears outside the margins on each page. Page borders are primarily for decoration so you can use any style, color, and line thickness you want. A good page border can make your page more appealing. In this section, we will demonstrate how to set up page borders in word using Spire.Doc for WPF.

Step 1: Initialize a new instance of Document class. Load the word document from the file.

Document document = new Document();
document.LoadFromFile("Emily.docx");

Step 2: Add page borders and set up the format of borders. In this instance, we set the style of border to ThinThickLargeGap and set the color to yellow green.

Section section = document.Sections[0];
section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap;
section.PageSetup.Borders.Color = Color.YellowGreen;

Step 3: Set up the space between the borders and the text.

section.PageSetup.Borders.Left.Space = 50;
section.PageSetup.Borders.Right.Space = 50;
section.PageSetup.Borders.Top.Space = 50;
section.PageSetup.Borders.Bottom.Space = 50;

Step 4: Save the document and launch the file.

document.SaveToFile("Dickinson.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Dickinson.docx");

Effective Screenshots:

Set up page borders in word in WPF

Full codes:

[C#]
using System.Drawing;
using System.Windows;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Emily
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile("Emily.docx");
            Section section = document.Sections[0];

            //Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap;
            section.PageSetup.Borders.Color = Color.YellowGreen;
            //Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50;
            section.PageSetup.Borders.Right.Space = 50;
            section.PageSetup.Borders.Top.Space = 50;
            section.PageSetup.Borders.Bottom.Space = 50;
            //Save and Launch
            document.SaveToFile("Dickinson.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Dickinson.docx");
        }
    }
        
}
[VB.NET]
Imports System.Drawing
Imports System.Windows
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace Emily
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			'Load Document
			Dim document As New Document()
			document.LoadFromFile("Emily.docx")
			Dim section As Section = document.Sections(0)

			'Add Page Borders with Special Style and Color
			section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap
			section.PageSetup.Borders.Color = Color.YellowGreen
			'Space between Border and Text
			section.PageSetup.Borders.Left.Space = 50
			section.PageSetup.Borders.Right.Space = 50
			section.PageSetup.Borders.Top.Space = 50
			section.PageSetup.Borders.Bottom.Space = 50
			'Save and Launch
			document.SaveToFile("Dickinson.docx", FileFormat.Docx)
			System.Diagnostics.Process.Start("Dickinson.docx")
		End Sub
	End Class

Header or footer are pieces of text or images that appear at the top or bottom of every document page. You can add any information you want to the header and footer areas such as page numbers, author's name, current date and company logo.

This article will demonstrate how to insert text header and footer for word document in WPF by using Spire.Doc for WPF.

Below are the effective screenshots after inserting header and footer:

Insert Header and Footer for Word Document in WPF

Insert Header and Footer for Word Document in WPF

At first, please download Spire.Doc and install it correctly, then add Spire.Doc.Wpf.dll and Spire.License.dll from the installation folder to your project as reference.

Then follow the detail steps below:

Use namespace:

using System.Windows;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

Insert Header

Step 1: Initialize a new instance of Document class and load the word document from file.

Document doc = new Document();
doc.LoadFromFile("Eiffel Tower.docx");

Step 2: Get its first section, then add a header paragraph for section one.

Section section = doc.Sections[0];
HeaderFooter header = section.HeadersFooters.Header;
Paragraph paragraph = header.AddParagraph();

Step 3: Append text for header paragraph and set text format.

TextRange text = paragraph.AppendText("Eiffel Tower introduction");
text.CharacterFormat.FontName = "Cambria";
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.TextColor = Color.DarkGreen;

Step 4: Set Header Paragraph Format.

paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
paragraph.Format.Borders.Bottom.BorderType = BorderStyle.DashSmallGap;
paragraph.Format.Borders.Bottom.Space = 0.05f;
paragraph.Format.Borders.Bottom.Color = Color.DarkGray;

Insert Footer

Step 5: Add a Footer paragraph.

HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph sparagraph = footer.AddParagraph();

Step 6: Append text for footer paragraph and set text format.

TextRange stext = sparagraph.AppendText("the tallest structure in Paris");
stext.CharacterFormat.FontName = "Calibri";
stext.CharacterFormat.FontSize = 12;
stext.CharacterFormat.TextColor = Color.DarkGreen;

Step 7: Set Footer Paragrah Format.

sparagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
sparagraph.Format.Borders.Top.BorderType = BorderStyle.Hairline;
sparagraph.Format.Borders.Top.Space = 0.15f;
sparagraph.Format.Borders.Color = Color.DarkGray;

Step 8: Save and launch the file.

doc.SaveToFile("HeaderFooter.docx");
System.Diagnostics.Process.Start("HeaderFooter.docx");

Full codes:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document();
            doc.LoadFromFile("Eiffel Tower.docx");

            Section section = doc.Sections[0];
            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph paragraph = header.AddParagraph();

            TextRange text = paragraph.AppendText("Eiffel Tower introduction");
            text.CharacterFormat.FontName = "Cambria";
            text.CharacterFormat.FontSize = 12;
            text.CharacterFormat.TextColor = Color.DarkGreen;

            paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
            paragraph.Format.Borders.Bottom.BorderType = BorderStyle.DashSmallGap;
            paragraph.Format.Borders.Bottom.Space = 0.05f;
            paragraph.Format.Borders.Bottom.Color = Color.DarkGray;

            HeaderFooter footer = section.HeadersFooters.Footer;
            Paragraph sparagraph = footer.AddParagraph();

            TextRange stext = sparagraph.AppendText("the tallest structure in Paris");
            stext.CharacterFormat.FontName = "Calibri";
            stext.CharacterFormat.FontSize = 12;
            stext.CharacterFormat.TextColor = Color.DarkGreen;

            sparagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
            sparagraph.Format.Borders.Top.BorderType = BorderStyle.Hairline;
            sparagraph.Format.Borders.Top.Space = 0.15f;
            sparagraph.Format.Borders.Color = Color.DarkGray;

            doc.SaveToFile("HeaderFooter.docx");
            System.Diagnostics.Process.Start("HeaderFooter.docx");
        }



    }
}
[VB.NET]
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
	Dim doc As New Document()
	doc.LoadFromFile("Eiffel Tower.docx")

	Dim section As Section = doc.Sections(0)
	Dim header As HeaderFooter = section.HeadersFooters.Header
	Dim paragraph As Paragraph = header.AddParagraph()

	Dim text As TextRange = paragraph.AppendText("Eiffel Tower introduction")
	text.CharacterFormat.FontName = "Cambria"
	text.CharacterFormat.FontSize = 12
	text.CharacterFormat.TextColor = Color.DarkGreen

	paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right
	paragraph.Format.Borders.Bottom.BorderType = BorderStyle.DashSmallGap
	paragraph.Format.Borders.Bottom.Space = 0.05F
	paragraph.Format.Borders.Bottom.Color = Color.DarkGray

	Dim footer As HeaderFooter = section.HeadersFooters.Footer
	Dim sparagraph As Paragraph = footer.AddParagraph()

	Dim stext As TextRange = sparagraph.AppendText("the tallest structure in Paris")
	stext.CharacterFormat.FontName = "Calibri"
	stext.CharacterFormat.FontSize = 12
	stext.CharacterFormat.TextColor = Color.DarkGreen

	sparagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right
	sparagraph.Format.Borders.Top.BorderType = BorderStyle.Hairline
	sparagraph.Format.Borders.Top.Space = 0.15F
	sparagraph.Format.Borders.Color = Color.DarkGray

	doc.SaveToFile("HeaderFooter.docx")
	System.Diagnostics.Process.Start("HeaderFooter.docx")
End Sub
page 234