Note that there are some explanatory texts on larger screens.

plurals
  1. POiTextSharp - Some pages not stamped as expected
    primarykey
    data
    text
    <p>I'm using iTextSharp 5.0.6 to read an existing PDF, iterate each page stamping text on each, and then writing out the newly stamped PDF. The issue I'm faced with is that this isn't working 100% of the time. For some PDFs every page is stamped as expected, for others most pages are stamped while some are not. Seems as if there's potentially an issue where the stamper's GetOverContent() is not returning the top-most layer, but that's just an assumption. Has anyone had a similar issue? </p> <pre><code>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 &lt;= pageCount; i++) { pageSize = reader.GetPageSize(i); pageContents = stamper.GetOverContent(i); //draw a rectangle pageContents.SetColorFill(backgroundColor); pageContents.MoveTo(pageSize.Width - (fontWidth + padding), 0f); pageContents.LineTo(pageSize.Width, 0f); pageContents.LineTo(pageSize.Width, 14f); pageContents.LineTo(pageSize.Width - (fontWidth + padding), 14f); pageContents.Fill(); //drop our watermark on top of the rectangle we just created pageContents.BeginText(); pageContents.SetColorFill(fontColor); pageContents.SetFontAndSize(font, fontSize); pageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, WATERMARK_TEXT, pageSize.Width - fontWidth, 4, textAngle); pageContents.EndText(); } stamper.Close(); reader.Close(); return memoryStream.ToArray(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload