PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Watermark has with white patches https://forum.pdfsharp.net/viewtopic.php?f=2&t=4092 |
Page 1 of 1 |
Author: | IRlyDunno [ Wed Feb 12, 2020 12:46 pm ] |
Post subject: | Watermark has with white patches |
I'm using pdfsharp v1.51.5189-beta Image with the result of what is happening will be attached at the end. And when I add the watermark (as per the pdfsharp sample) it works most of the time, at least it was working up until today, when I needed to add a watermark on top of a pdf with acroform. And here's the problem, the watermark comes with white patches where some of the fields are. I have identified that the problem is that some fields have a white background, and, for some reason, when the file is saved, the fields are drawn on top of the watermark. Attempts I have tried to fix this: Changing the BackColor property to 'XColors.Transparent' of the PdfTextField in run time, if the wartermark is to be inserted. Changing the ForeColor property to 'XColors.Transparent' of the PdfTextField in run time, if the wartermark is to be inserted. Code: Code: ((PdfTextField )acroCollection[field]).BackColor = PdfSharp.Drawing.XColors.Transparent; ((PdfTextField )acroCollection[field]).ForeColor = PdfSharp.Drawing.XColors.Transparent; For dome reason, the formfield just ignores setting the backcolor I am setting here Adding the watermark after flattening the form. Adding watermark before flattening the form. Flatening the form, saving the pdf, opening the pdf again with a new reader and adding the watermark Code: Code: using (var fs = new FileStream(ReportPath + newfile, FileMode.Create)) { // Get the template and copy it to another document PdfSharp.Pdf.PdfDocument myTemplate = PdfSharp.Pdf.IO.PdfReader.Open(pdfTemplateMod22, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify); //Ensure the new values are displayed if (myTemplate.AcroForm.Elements.ContainsKey("/NeedAppearances")) myTemplate.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true); else myTemplate.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true)); var acroFields = myTemplate.AcroForm.Fields; FuncoesAuxiliaresPdf.SetVarsModelo22(acroFields); PdfHelper.AddWaterMarkToDocument(ref myTemplate); if (myTemplate.AcroForm != null) myTemplate.Flatten(); // NOTE: doc.AcroForm is null after flattening ! and the pdf is not editable myTemplate.Save(fs); // Insiro o pdf da Modelo 22 como o primeiro para ser mostrado primeiro url = "../relatorios/temporarios/" + newfile; if (modeloEAnexosURL != "") // Só necessito de enviar os url dos anexos caso tenham sido feitos url += ";" + modeloEAnexosURL; } // Or opening again and saving after the file is created PdfSharp.Pdf.PdfDocument myTemplate2 = PdfSharp.Pdf.IO.PdfReader.Open((ReportPath + newfile), PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify); PdfHelper.AddWaterMarkToDocument(ref myTemplate2 ); So now, I don't know what to do, and I would appretiate guidance on how I could fix this issue. Attachment: WhitePatchedWatermark.png [ 103.15 KiB | Viewed 8641 times ] |
Author: | Thomas Hoevel [ Wed Feb 12, 2020 1:10 pm ] |
Post subject: | Re: Watermark has with white patches |
Hi! You don't show the routine that draws the watermark. If the watermark is drawn below the pre-existing contents than it is clear that table cells with white background will hide the watermark. Solution: draw the watermark above the pre-existing contents. Use Code: var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append); and not .Prepend.
|
Author: | IRlyDunno [ Wed Feb 12, 2020 2:08 pm ] |
Post subject: | Re: Watermark has with white patches |
Thanks for the reply, Sorry, I thought I posted the code, it's basically the same code as the pdfsharp sample (I tried all 3 of them), but I'll post it now. Code: Code: public static void AddWaterMarkToDocument(ref PdfSharp.Pdf.PdfDocument doc) { string watermark = "Demonstração - DEFIR"; var font = new PdfSharp.Drawing.XFont(PdfFontNames.Arial, 90); PdfSharp.Drawing.XGraphics gfx = null; for (int i = 0; i < doc.Pages.Count; ++i) { // Variation 1: Draw a watermark as a text string. var page = doc.Pages[i]; // Get an XGraphics object for drawing beneath the existing content. gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page, PdfSharp.Drawing.XGraphicsPdfPageOptions.Append); // Get the size (in points) of the text. var size = gfx.MeasureString(watermark, font); // Define a rotation transformation at the center of the page. gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // Create a string format. var format = new PdfSharp.Drawing.XStringFormat(); format.Alignment = PdfSharp.Drawing.XStringAlignment.Near; format.LineAlignment = PdfSharp.Drawing.XLineAlignment.Near; // Create a dimmed red brush. var brush = new PdfSharp.Drawing.XSolidBrush(PdfSharp.Drawing.XColor.FromArgb(50, 255, 0, 0)); // Draw the string. gfx.DrawString(watermark, font, brush, new PdfSharp.Drawing.XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2), format); } } As you can see I was already using the property you specified, but it doesn't work Code: gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page, PdfSharp.Drawing.XGraphicsPdfPageOptions.Append); Since XGraphicsPdfPageOptions.Append draws the watermark behing the content, the acrofields that have a solid white color leave white patches. Thats why I wanted to change the BackColor property of the PdfTextField to XColors.Transparent so that it is seethrough, but the end result ignores my instruction. |
Author: | IRlyDunno [ Fri Feb 14, 2020 6:24 pm ] |
Post subject: | Re: Watermark has with white patches |
So, isn't there any way to fix this? |
Author: | (void) [ Sun Feb 16, 2020 1:49 pm ] |
Post subject: | Re: Watermark has with white patches |
It looks to me as your problem is the "flattening" process not really flattening the form-fields. (IIRC PdfSharp simply sets the fields to "read-only", but otherwise leaving them as they are) It seems, a Pdf-Viewer will put the AcroFields (if they are present) on top of everything else to ensure, the user can interact with them. So no matter where you place your watermark in the z-order, when opening the document the Pdf-Viewer will always place AcroFields on top of that. (otherwise you would be able to completely disable AcroFields just by placing an opaque rectangular watermark on top of the page) The only way out i can currently think of is to get completely rid of all the fields, by "really" flattening them. Maybe this helps: viewtopic.php?f=3&t=3778#p11641 |
Author: | IRlyDunno [ Mon Feb 17, 2020 5:55 pm ] |
Post subject: | Re: Watermark has with white patches |
Thank you for your answer and time! So, you're saying that, there's no current solution for this problem? That's unfortunate. |
Author: | (void) [ Mon Feb 17, 2020 9:37 pm ] |
Post subject: | Re: Watermark has with white patches |
Pretending my assumptions about the AcroFields not being flattened is correct, then i would say, it's not possible with the current "official" release (alone). You could try out my forked version (linked in the post i mentioned) and see, whether it works for your use-case. (I would not recommend to use it in production, though) Or you could combine PdfSharp with other libraries or tools, that are able to flatten a form. e.g. process your Pdf as always, save it, run it through "some other tool" to flatten it and then load the flattened version again with PdfSharp to add the Watermark. Sorry, i cannot give any recommendations for the "other" tool, but i'm sure, there's plenty of choices out there. Good luck ! |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |