I use iTextSharp 5.0.6 to read an existing PDF file, repeat each page, typing on each, and then writing a new PDF stamp. The problem I am facing is that this does not work 100% of the time. For some PDF files, each page is marked as expected, for other pages most of the stamps, and some not. There seems to be a potential problem where the GetOverContent () template does not return the topmost layer, but this is just an assumption. Has anyone had a similar problem?
using iTextSharp.text; using iTextSharp.text.pdf; const string WATERMARK_TEXT = "John Doe"; static void Main(string[] args) { string masterPdf = "master.pdf"; string pdfToCreate = "watermark.pdf"; byte[] bytes = StampPDF(masterPdf); using (FileStream stream = new FileStream(pdfToCreate, FileMode.Create)) { stream.Write(bytes, 0, bytes.Length); } } static byte[] StampPDF(string PdfPath) { using (MemoryStream memoryStream = new MemoryStream()) { PdfReader reader = new PdfReader(PdfPath); int pageCount = reader.NumberOfPages; PdfStamper stamper = new PdfStamper(reader, memoryStream); float fontSize = 9; float textAngle = 0f; BaseFont font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); BaseColor backgroundColor = new BaseColor(0, 0, 0); BaseColor fontColor = new BaseColor(255, 255, 255); float padding = 2f; float fontWidth = font.GetWidthPoint(WATERMARK_TEXT, fontSize); iTextSharp.text.Rectangle pageSize; PdfContentByte pageContents; for (int i = 1; i <= pageCount; i++) { pageSize = reader.GetPageSize(i); pageContents = stamper.GetOverContent(i);
source share