Add Headers To All Pages in ItextSharp
09-05-2018Add Headers To All Pages
public void WritePageNumber(string path,string outputPath) { PdfReader reader = new PdfReader(path); // Create a stamper PdfStamper stamper = new PdfStamper(reader,new FileStream(outputPath,FileMode.Create)); // Loop over the pages and add a header to each page int n = reader.NumberOfPages; for (int i = 1; i <= n; i++) { GetHeaderTable(i, n).WriteSelectedRows(0, -1, 34, 803, stamper.GetOverContent(i)); } // Close the stamper stamper.Close(); reader.Close(); } /** * Create a header table with page X of Y * @param x the page number * @param y the total number of pages * @return a table that can be used as header */ public static PdfPTable GetHeaderTable(int x, int y) { PdfPTable table = new PdfPTable(2); table.TotalWidth=527; table.LockedWidth = true; table.DefaultCell.FixedHeight = 20; table.DefaultCell.Border = Element.ALIGN_BOTTOM; table.AddCell("FOOBAR FILMFESTIVAL"); table.DefaultCell.HorizontalAlignment=(Element.ALIGN_RIGHT); table.AddCell($"Page {x} / {y}"); return table; } }