Hello, we are using EO-PDF 23.1.45 with an Angular application UI with C# web-API. I have defined an AfterRenderPage method to apply a watermark to each page in the PDF - the code is below
Code: C#
...
HtmlToPdf.Options.AfterRenderPage = AfterRenderPage;
...
private void AfterRenderPage(object sender, PdfPageEventArgs e)
{
HtmlToPdf.ConvertHtml($"<div style='position: absolute; top:50%; left:50%; transform: translateX(-50%) translateY(-50%) rotate(-45deg); opacity:0.2; font-size:110px; width:100%; text-align:center;'>My watermark</div>", e.Page);
}
This renders the pages in the PDF as expected when I run the application locally, on my workstation (i.e. each page is rendered in the PDF with the watermark on all pages). However when I deploy to the server (hosted in AppServices) I get a different result for larger requests - i.e. for smaller requests (PDF's with say 5-10 pages), each page is rendered with the watermark. However for larger requests (especially PDF's with say 20 or more pages), it renders the pages in the report correctly however the watermark often (most of the time) only appears on say the first 10 pages - there is no real pattern - sometimes it generates the watermark on the first say 5,6,7,etc pages, and sometimes for all pages. It may have something to do with when the EO-PDF engine determines when the page is fully rendered
I have also tried this using version 23.1.52 (which was provided to fix a different issue) and on version 22.1.94 and both have the same problem. The above AfterRenderPage code has been in place for a number of years and as far as I am aware this use to work (i.e. regardless of the request size, all pages were rendering with the watermark) - unfortunately I don't know what version this became a problem
I now have some 'workaround' code that runs after the PDF generation which puts the watermark text over the top of each page generated, however it's not as good as the original watermark solution (which uses the AfterRenderPage strategy) because the letters of the watermark aren't able to be made transparent
Code: C#
...
PdfDocument doc = null;
using (MemoryStream stream = new MemoryStream(pdf))
{
doc = new PdfDocument(stream);
float pageHeight = doc.Pages[0].Size.Height;
for (int i = 0; i < doc.Pages.Count; i++)
{
EO.Pdf.Contents.PdfTextLayer textLayer = new EO.Pdf.Contents.PdfTextLayer();
//Use a big font, light text color and also
//rotate the text 45 degrees
textLayer.Font = new EO.Pdf.Drawing.PdfFont("Arial", 50);
textLayer.NonStrokingColor = Color.LightGray;
//textLayer.Flatness = 99;
textLayer.GfxMatrix.Rotate(45);
//Create the text object
EO.Pdf.Contents.PdfTextContent textContent = new EO.Pdf.Contents.PdfTextContent("My watermark");
textContent.PositionMode = EO.Pdf.Contents.PdfTextPositionMode.Offset;
textContent.Offset = new EO.Pdf.Drawing.PdfPoint(300, -100);
//Add the text object into the text layer object
textLayer.Contents.Add(textContent);
//Add the text layer into the page
doc.Pages[i].Contents.Add(textLayer);
}
}
using (MemoryStream stream = new MemoryStream())
{
doc.Save(stream);
}
...
Because I can't recreate this issue locally I'm not able to send you a project that shows the issue. Is it possible to either
- fix the AfterRenderPage issue to make sure the method AfterRenderMethod is always fully run (for all pages) before the eventual PDF is generated
- or, is there a way to render the watermark using the workaround EO.Pdf.Contents technique above (or the ACM library) to allow transparent text to sit on top of the already generated PDF
Many thanks