Hi,
Learn about Aspose.Pdf, a component for generating PDF files from scratch. Note that you cannot use this component to modify existing PDF files.
We also have Aspose.Pdf, which allows you to manipulate/edit existing PDF files. You can add headers and footers to your existing PDF file using this component.
You can use PdfFileStamp to add headers and footers to a PDF file. However, PdfFileStamp currently does not support adding hyperlinks to headers and footers. As a workaround, use the PdfContentEditor class to add LocalLinks to the TOC page. The workflow is as follows:
- Add the header/footer to the existing PDF file using the PdfFileStamp class.
- Add hyperlinks to all pages of the PDF file, pointing to the TOC page, at the same point where the header/footer is placed.
Please try the following code snippet to accomplish this requirement. You can find the resultant PDF file generated based on your requirements in the attachment.
// Open the PDF file in which we need to add a 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 to the 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 the Footer section at the specified location in the PDF
fileStamp.AddFooter(ft, 15, 5, 0);
// Add Header section at the specified location in the PDF
fileStamp.AddHeader(ft, 15, 5, 0);
// Close the FileStamp object which adds the watermark to the PDF file
fileStamp.Close();
// Close the input FileStream
inStream1.Close();
// create an object 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 the rectangle that will specify the hyperlink to be added in the Footer section.
System.Drawing.Rectangle Footer_rect = new System.Drawing.Rectangle(5, 12, 180, 15);
// Create an object of the rectangle that will specify the hyperlink to be added in the Header section.
System.Drawing.Rectangle Header_rect = new System.Drawing.Rectangle(5, 760, 180, 15);
editor.BindPdf(ms);
// Set the color value for the rectangle to white that will 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 the FooterSection of the document.
editor.CreateLocalLink(Footer_rect, 2, i, c);
// Add the local link in the 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.