Hi Tilal, thanks for your reply. If Generator is now obsolete, is there a new way with Document to add headers, footers and sections to the generated PDF?
Hi,
In order to add Header/Footer while creating PDF file using Document class, please try using following code snippet.
[C#]
Document doc = new Document();
// add a page to pages collection of PDF file
doc.Pages.Add();
//Instantiate HeaderFooter object and pass the Section reference in which
//the header or footer is to be added
Aspose.Pdf.HeaderFooter hf1 = new Aspose.Pdf.HeaderFooter();
//Set the header of odd pages of the PDF document
doc.Pages[1].Header = hf1;
//Instantiate a Text paragraph that will store the content to show as header
Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("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);
doc.Save("c:/pdftest/PDFwithHeader.pdf");
In case you need to add Header/Footer in existing PDF file, please try following the instructions specified over
- Manage Header and Footer in PDF Files
- Add Header in a PDF File (Facades)
- Add Footer in a PDF File (Facades)
Hi Nayyer,
How would I use this method to add a header and footer to each page, with a page number (i.e. page 1 of 4)? I know the Pdf.Generator.Text class had the special formats $p and $P for this. Is there an equivalent for the Document style?
I'm currently attempting to iterate (using foreach) over doc.Pages to add a separate header/footer to each page, using the page number of each page. However, the enumerator for PageCollection seems to return an object type which cannot be cast to Page. Is there a better way?
Hi there,