Some times we need to add header/Footer section to the Pdf file being generated. With Aspsoe.pdf it’s very easy to add Header/Footer section to the Pdf being generated, using HeaderFooter class.
Adding Header/Footer becomes little tricky during conversion feature, i.e. conversion of HTML to Pdf. In order to accomplish this we need to add the Header/Footer to every section of the Pdf file being generated. Please use the following code snippet for this purpose.
[C#.NET]
Pdf pdf = new Pdf();
//Bind the html stream to pdf object
pdf.BindHTML("C:\\pdftest\\Memory.html");
//add customed header to every section of the pdf object
foreach (Aspose.Pdf.Section sec1 in pdf.Sections)
{
Aspose.Pdf.HeaderFooter hf1 = new Aspose.Pdf.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
text.TextInfo.Alignment = Aspose.Pdf.AlignmentType.Center;
hf1.Paragraphs.Add(text);
}
//Save the pdf
pdf.Save(@"c:/pdftest/HTMLwithHeaderFooter.pdf");
[VB.NET]
Dim pdf As Pdf = New Pdf()
'Bind the html stream to pdf object
pdf.BindHTML("C:\pdftest\Memory.html")
'//add customed header to every section of the pdf object
For Each sec1 As Aspose.Pdf.Section In pdf.Sections
Dim hf1 As Aspose.Pdf.HeaderFooter = New Aspose.Pdf.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, "My Header")
'//Add the text object to the Paragraphs collection of HeaderFooter object to
'//display header on the pages of PDF document
text.TextInfo.Alignment = Aspose.Pdf.AlignmentType.Center
hf1.Paragraphs.Add(text)
Next
'//Save the pdf
pdf.Save("c:/pdftest/HTMLwithHeaderFooter.pdf")