Watermarking each page in a loaded PDF document sometimes blanks pages in the document (Except the watermark)

0
I am iterating over each page in a loaded PDF document using the latest version of the PDF library. foreach (PdfPageBase lPage in lDoc.Pages) { try { PdfGraphics g = lPage.Graphics; PdfGraphicsState state = g.Save(); var pw = lPage.Graphics.Size.Width; var ph = lPage.Graphics.Size.Height; var ss = font.MeasureString(stampText); var dx = pw - ss.Height - 5; var dy = (ph - ss.Width 5); g.SetTransparency(0.25f); g.RotateTransform(-90); g.DrawString(stampText, font, PdfPens.Red, PdfBrushes.Red, new PointF(-(ss.Width ss.Height), ss.Height / 2)); g.Restore(state); cpage = 1; } catch (Exception ex) { //Do Something here } } On some document however The contents of the page get wiped and all I am left with is the watermark... I have tried all manner of things in this code to glean the differences between pages that wipe and the pages that do not wipe... and have not been able to find the reason for this. Has anyone else run into this issue. BTW with an earlier version of this library I would get NULL REFERENCE EXCEPTIONS on some of the pages in some documents. With the current library the Catch never gets entered but some pages still exhibit this blanking behavior
pdf
edited 2/27/2020 5:55:31 PM
asked 2/27/2020 1:54:58 PM
add a comment

1 Answers

0
Not that anyone had a real answer for this problem so I managed to come up with a technique to solve this piece of stupidity... Essentially, rather than attempting to LOAD the supplied PDF and watermark each of its pages, then save the result back out as a separate document. (This resulted in the squirrely behavior noted above) I have instead Loaded the document into a PDFLoadedDocument object, then Created a new PDFDocument object, then iterated over the pages in the loaded document add each page to the newly opened document. At the end of that saved the newly opened document. Then Loaded the newly create document into a PDFLoadedDocument object and watermaked each of its pages. Why this works with over a dozen problem clild document I have for testing is unknown to me but it does seem to have solved this stupid problem... Sample snippet shown below.... using (PdfImportedDocument ldoc = new PdfImportedDocument(fi1.FullName)) { using (PdfDocument odoc = new PdfDocument(PdfDocumentConformanceLevel.Pdf_A1B)) { pBar.Visible = true; pBar.Maximum = ldoc.Pages.Count; pBar.Value = 0; foreach(PdfImportedPage p in ldoc.Pages) { odoc.ImportPage(ldoc, p); pBar.Value = 1; System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(30); } pBar.Visible = false; odoc.Save(Path.Combine(System.IO.Path.GetTempPath(), "Before_WM_" this.hashedFileName)); } }
edited 3/4/2020 7:08:41 PM
answered 3/4/2020 7:05:09 PM
add a comment

Your Answer

Not the answer you're looking for? Browse other questions tagged pdf or ask your own question.