Add Header Footer to an existing PDF file in C# with Aspose.PDF for .NET

Hi Support,

I have a PDF which contains many pages, Now i want to insert Navigate to Table of Contents (TOC) link in Header and Footer of All pages.

Note : The PDF is already avai;able and may or may not be generated using "Aspose"

Is there any way in Aspose by which we can insert Header and Footer to an existing PDF?

Please find the attached sample PDF.

Thanks in advance.

Hi,

Let me share something regarding Aspose.Pdf. This component is used to generate the PDF files from scratch. You cannot use this component to modify existing PDF files.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Beside this, we have another component named Aspose.Pdf, which offers the capability to manipulate/edit existing PDF files. I am pleased to inform you that, using that component you can add Header/Footer to your existing PDF file.

You can use PdfFileStamp to add Header/Footer section in PDF file. But currently PdfFileStamp does not offer the capability to add the hyperlink into Header/Footer section. So as a workaround, we would be using PdfContentEditor class to add the LocalLinks to the TOC page. So the workflow would be like

1 - Add the Header/Footer to the existing PDF file using PdfFileStamp class.

2 - Add the Hyperlinks to all the pages of PDF file, pointing to TOC page, at the same point where header/footer is placed.

Please try using the following code snippet to accomplish your requirement. The resultant PDF file that I've generated based on your requirements, is in attachment, please take a look.

[C#]

// Open the PDF file in which we need to add Header/Footer
FileStream inStream1 = new FileStream(@"d:/pdftest/2061_11_20090406_233245.pdf", FileMode.Open);
// Create an instance of MemoryStream class
MemoryStream ms = new MemoryStream();
PdfFileStamp fileStamp = null;

fileStamp = new PdfFileStamp(inStream1, ms);
// create a FormattedTExt object which will be added in Header/Footer section
FormattedText ft = new FormattedText("Navigate to Table of Contents (TOC)", System.Drawing.Color.FromArgb(0, 0, 0), Aspose.Pdf.Kit.FontStyle.TimesRoman, EncodingType.Winansi, false, 12);
// Add Footer section at specified location in PDF
fileStamp.AddFooter(ft,15,5,0);
// Add header section at specified locaiton in PDF
fileStamp.AddHeader(ft, 15, 5, 0);
// close the FileStamp object which adds the watermark to the PDF file
fileStamp.Close();
// close the inputFileStream
inStream1.Close();

// create an onject of PDFFileInfo class. We will use it to get the total number of pages in PDF
PdfFileInfo info = new PdfFileInfo(@"d:/pdftest/2061_11_20090406_233245.pdf");

// Create an instance of PdfContentEditor class
PdfContentEditor editor = new PdfContentEditor();
// Create an object of rectangle that will specify the hyperlink to be added in Footer section.
System.Drawing.Rectangle Footer_rect = new System.Drawing.Rectangle(5, 12, 180, 15);
// Create an object of rectangle that will specify the hyperlink to be added in Header section.
System.Drawing.Rectangle Header_rect = new System.Drawing.Rectangle(5, 760, 180, 15);
editor.BindPdf(ms);
// set the color value for rectangle to white, that whill be displayed around the link
System.Drawing.Color c = System.Drawing.Color.FromArgb(255, 255, 255);
// Iterate through all the pages in the PDF file individually

for (int i = 1; i <= info.NumberofPages; i++)
{
// Add the local link in FooterSection of the document.
editor.CreateLocalLink(Footer_rect, 2, i, c);
// Add the local link in HeaderSection of the document.
editor.CreateLocalLink(Header_rect, 2, i, c);
}

// Save the final PDF with hyperlink & Stamp information
editor.Save(@"d:/pdftest/outfile.pdf");
// close the memory stream object
ms.Close();

If it does not satisfy your requirement, please feel free to share.