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 8682 times ]