Aspose.Pdf

Set Page Header and Footer

Header is a special area on the top of the page. The header of a page uses the top margin area of the page, which means the height of the header can't be larger than the top margin of the page.

 

Similarly, Footer is a special area on the bottom of the page and uses the bottom margin area of the page.

Important Note: In Aspose.Pdf , if the Header or Footer is larger than the top or bottom margin of the page respectively, the top or bottom margin will be enlarged automatically.

 

The following figure shows the complete picture of Header and Footer in a page and its corresponding supported properties:

 

Simple Header & Footer

 

Aspose.Pdf provides the facility to add header and footer on Odd or Even or Both pages of the PDF document.

 

The four properties of Section class that help to add header or footer on the pages are given below:

 

 

Please follow the steps below to set header or footer in the pages:

 

  1. Instantiate HeaderFooter object by calling one of its overloaded constructors that takes a Section reference as argument to inherit the common Section properties
  2. Assign the HeaderFooter object created in above step to OddHeader or EvenHeader or Both properties of Section
  3. Create a Text paragraph that would be used as a header and pass HeaderFooter object and header text to its constructor as references
  4. Finally, Add the Text paragraph object to Paragraphs collection of the HeaderFooter instance

 

Code Snippet

 

[C#]

 

//Instantiate HeaderFooter object and pass the Section reference in which

//the header or footer is to be added

HeaderFooter hf1 = new HeaderFooter(sec1);

 

//Set the header of odd pages of the PDF document

sec1.OddHeader = hf1;

 

//Set the header of even pages of the PDF document

sec1.EvenHeader = hf1;

 

//Instantiate a Text paragraph that will store the content to show as header

Text text = new Text(hf1,"header");

 

//Add the text object to the Paragraphs collection of HeaderFooter object to

//display header on the pages of PDF document

hf1.Paragraphs.Add(text);

 

[VB.NET]

 

'Instantiate HeaderFooter object and pass the Section reference in which

'the header or footer is to be added

Dim hf1 As HeaderFooter = New HeaderFooter(sec1)

 

'Set the header of odd pages of the PDF document

sec1.OddHeader = hf1

 

'Set the header of even pages of the PDF document

sec1.EvenHeader = hf1

 

Instantiate a Text paragraph that will store the content to show as header

Dim text As Text = New Text(hf1, "header")

 

'Add the text object to the Paragraphs collection of HeaderFooter object to

'display header on the pages of PDF document

hf1.Paragraphs.Add(text)

 

[JAVA]

 

//Instantiate HeaderFooter object and pass the Section reference in which

//the header or footer is to be added

HeaderFooter hf1 = new HeaderFooter(sec1);

 

//Set the header of odd pages of the PDF document

sec1.setOddHeader(hf1);

 

//Set the header of even pages of the PDF document                     

sec1.setEvenHeader(hf1);

 

//Instantiate a Text paragraph that will store the content to show as header

                      Text text = new Text(hf1,"header");

 

//Add the text object to the Paragraphs collection of HeaderFooter object to

//display header on the pages of PDF document

hf1.getParagraphs().add(headText);

 

[XML]

 

<Header>

     <Text>

         <Segment>Header</Segment>

     </Text>

</Header>

 

Advanced Header & Footer

 

Simple Header and Footer described above are good when a Header or Footer with the fixed content is to add on the top or bottom of all the pages of a PDF document.

 

But, what if the content of Header or Footer on one page is different from that of the subsequent pages in the same PDF document?

 

To answer this question, Aspose.Pdf has added the following properties to the Section class:

 

 

We can use these above mentioned properties to set header and footer for the subsequent pages in the same PDF document.

 

HeaderFooter class has some properties like IsFirstPageOnly , IsSubsequentPagesOnly and IsLastPageOnly . Using these properties, we can control the display of header or footer on first, last or subsequent pages of the PDF document. With the release of Aspose.Pdf version 3.4.5.0, it introduces another property DistanceFromEdge to set the Page Header or Footer distance from Page Edge. Therefore, the starting position of Rendering Header in the Pdf will be DistanceFromEdge + Header.Margin.Top . Similarly, same is the case with Footer, by setting the Distance and Margin's bottom property. Please study the above picture to understand the complete structure of these properties.

Below is an example that demonstrates the use of these properties to add different headers on first, odd and even pages of the same PDF document.

 

Three different HeaderFooter objects are created in the below example to be used for different purposes.

 

  1. First HeaderFooter instance (hf1) is instantiated to show a header on First Page Only
  2. Second HeaderFooter instance (hf2) is instantiated to show a different header on Odd Subsequent Pages Only
  3. Third HeaderFooter instance (hf3) is instantiated to show a distinct header on Even Subsequent Pages Only

 

Code Snippet

 

[C#]

 

/*

  *First Header "hf1" for first page only

  */

 

//Create a Section object by calling Add method of Sections collection of Pdf class

Section sec1 = pdf1.Sections.Add();

 

//Instantiate First HeaderFooter object and pass the Section reference in which

//the header or footer is to be added

HeaderFooter hf1 = new HeaderFooter(sec1);

 

//Set the header of odd pages of the PDF document

sec1.OddHeader = hf1;

 

//Set the header of even pages of the PDF document

sec1.EvenHeader = hf1;

 

//Enable this header for first page only

hf1.IsFirstPageOnly = true;

 

//Add Distance From Edge Property to 80 unit Points

hf1.DistanceFromEdge = 80;

 

//Set the First HeaderFooter, top and bottom property respectively

hf1.Margin.Bottom = 50;

hf1.Margin.Top = 100;

 

//Instantiate a Text paragraph that will store the content to show as header

Text text = new Text(hf1,"header for first page");

 

//Add the text object to the Paragraphs collection of HeaderFooter object to

//display header on the pages of PDF document

hf1.Paragraphs.Add(text);

 

/*

  *Second Header "hf2" for odd subsequent pages only

  */

 

//Instantiate Second HeaderFooter object and pass the Section reference in which

//the header or footer is to be added

HeaderFooter hf2 = new HeaderFooter(sec1);

 

//Set the additional header of odd pages of the PDF document

sec1.AdditionalOddHeader = hf2;

 

//Enable this header for subsequent page only

hf2.IsSubsequentPagesOnly = true;

 

//Add Distance From Edge Property of header to 150 unit Points

hf2.DistanceFromEdge = 150;

hf2.Margin.Bottom = 70;

 

//Instantiate a Text paragraph that will store the content to show as header

text = new Text(hf2,"odd header for subsequent pages");

 

//Add the text object to the Paragraphs collection of HeaderFooter object to

//display header on the pages of PDF document

hf2.Paragraphs.Add(text);

 

/*

  *Third Header "hf3" for even subsequent pages only

  */

 

//Instantiate Third HeaderFooter object and pass the Section reference in which

//the header or footer is to be added

HeaderFooter hf3 = new HeaderFooter(sec1);

 

//Set the additional header of even pages of the PDF document

sec1.AdditionalEvenHeader = hf3;

 

//Enable this header for subsequent page only

hf3.IsSubsequentPagesOnly = true;

 

//Add the Distance from Edge for the third Header

hf3.DistanceFromEdge = 180;

hf3.Margin.Top = 90;

 

//Instantiate a Text paragraph that will store the content to show as header

text = new Text(hf3,"even header for subsequent pages");

 

//Add the text object to the Paragraphs collection of HeaderFooter object to

//display header on the pages of PDF document

hf3.Paragraphs.Add(text);

 

[VB.NET]

 

'First Header "hf1" for first page only

 

'Create a Section object by calling Add method of Sections collection of Pdf class

Dim sec1 As Section = pdf1.Sections.Add()

 

'Instantiate First HeaderFooter object and pass the Section reference in which

'the header or footer is to be added

Dim hf1 As HeaderFooter = New HeaderFooter(sec1)

 

'Set the header of odd pages of the PDF document

sec1.OddHeader = hf1

 

'Set the header of even pages of the PDF document

sec1.EvenHeader = hf1

 

'Enable this header for first page only

hf1.IsFirstPageOnly = True

 

'Add Distance From Edge Property to 80 unit Points

hf1.DistanceFromEdge = 80;

 

'Set the First HeaderFooter, top and bottom property respectively

hf1.Margin.Bottom = 50;

hf1.Margin.Top = 100;

 

'Instantiate a Text paragraph that will store the content to show as header

Dim text As Text = New Text(hf1, "header for first page")

 

'Add the text object to the Paragraphs collection of HeaderFooter object to

'display header on the pages of PDF document

hf1.Paragraphs.Add(text)

 

'Second Header "hf2" for odd subsequent pages only

 

'Instantiate Second HeaderFooter object and pass the Section reference in which

'the header or footer is to be added

Dim hf2 As HeaderFooter = New HeaderFooter(sec1)

 

'Set the additional header of odd pages of the PDF document

sec1.AdditionalOddHeader = hf2

 

'Enable this header for subsequent page only

hf2.IsSubsequentPagesOnly = True

 

'Add Distance From Edge Property of header to 150 unit Points

hf2.DistanceFromEdge = 150;

hf2.Margin.Bottom = 70;

 

'Instantiate a Text paragraph that will store the content to show as header

text = New Text(hf2, "odd header for subsequent pages")

 

'Add the text object to the Paragraphs collection of HeaderFooter object to

'display header on the pages of PDF document

hf2.Paragraphs.Add(text)

 

'Third Header "hf3" for even subsequent pages only

 

'Instantiate Third HeaderFooter object and pass the Section reference in which

'the header or footer is to be added

Dim hf3 As HeaderFooter = New HeaderFooter(sec1)

 

'Set the additional header of even pages of the PDF document

sec1.AdditionalEvenHeader = hf3

 

'Enable this header for subsequent page only

hf3.IsSubsequentPagesOnly = True

 

'Add the Distance from Edge for the third Header

hf3.DistanceFromEdge = 180;

hf3.Margin.Top = 90;

 

'Instantiate a Text paragraph that will store the content to show as header

text = New Text(hf3, "even header for subsequent pages")

 

'Add the text object to the Paragraphs collection of HeaderFooter object to

'display header on the pages of PDF document

hf3.Paragraphs.Add(text)

 

[XML]

 

<Header IsFirstPageOnly="true" DistanceFromEdge="80" MarginTop="50" MarginBottom="100">

<Text>

  <Segment FontSize="50">header for first page</Segment>

</Text>

</Header>

<Footer Type="Odd"  IsSubsequentPagesOnly="true" DistanceFromEdge="140" MarginTop="50">

<Text>

  <Segment FontSize="30">odd footer for subsequent pages</Segment>

</Text>

</Footer>

<Header Type="Even" IsSubsequentPagesOnly="true" DistanceFromEdge="30" MarginBottom="20">

<Text>

  <Segment FontSize="25">even header for subsequent pages</Segment>

</Text>

</Header>