PDF加入頁碼有查到兩種方式
一種是繼承PdfPageEventHelper,然後再override OnEndPage 加入頁碼
另一種則是類似用加入浮水印方式用PdfStamper來加入頁碼
個人採用後者,主要用在已經產生好的PDF上
public static void AddPageNumber(string fileIn, string fileOut) { byte[] bytes = File.ReadAllBytes(fileIn); Font blackFont = FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK); using (MemoryStream stream = new MemoryStream()) { PdfReader reader = new PdfReader(bytes); using (PdfStamper stamper = new PdfStamper(reader, stream)) { int pages = reader.NumberOfPages; for (int i = 1; i <= pages; i++) { ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0); } } bytes = stream.ToArray(); } File.WriteAllBytes(fileOut, bytes); }
最後附上查詢頁碼Code
//查詢PDF頁碼 string ppath = rootpath + "XXX.pdf"; PdfReader pdfReader = new PdfReader(ppath); int numberOfPages = pdfReader.NumberOfPages; Console.WriteLine(numberOfPages); Console.ReadLine();
沒有留言:
張貼留言