Welcome Guest Search | Active Topics | Sign In | Register

AfterRenderPage not running for all pages before PDF is generated Options
Phil
Posted: Monday, April 3, 2023 4:52:56 AM
Rank: Advanced Member
Groups: Member

Joined: 11/8/2017
Posts: 64
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
eo_support
Posted: Monday, April 3, 2023 1:44:10 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,088
Hi,

Without a way to reproduce the problem we are unable to tell exactly why AfterRenderPage is not firing correctly (we don't even know whether it's because the event is not fired properly, or the event is fired properly but RenderHtml did not work properly).

There are a few things you can try:

1. Make sure you enclose your HTML in a fixed size DIV. For example, instead of calling ConvertHtml with:

Code: HTML/ASPX
<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>


You can call:
Code: HTML/ASPX
<div style='width:600px;height:800px;'>
    <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>
</div>


2. Try to use HtmlToPdfOptions.HeaderHtmlFormat using the same HTML instead of AfterRenderPage. There is no limit on how big your header can be. So it can be rendered over your main content;

3. You can still use ACM interface but it only support transparency on images. So you could pre-render your text into an image with alpha channel and then use ACM interface to stamp that image onto each page. Obviously this would be the last resort because images are always less effcient than text;

Hope this helps. In the mean time if you found out a way to reproduce it please let us know.

Thanks!
Phil
Posted: Tuesday, April 4, 2023 11:51:31 PM
Rank: Advanced Member
Groups: Member

Joined: 11/8/2017
Posts: 64
Many thanks for these suggestions - I went with option 2 (with the option 1 markup)

Code: C#
HtmlToPdf.Options.HeaderHtmlFormat = $"&lt;div style='width:{sizeF.Width}in;height:{sizeF.Height}in;'&gt;&lt;div style='transform: translateX(0%) translateY(200%) rotate(-45deg); opacity:0.2; font-size:100px; width:100%; text-align:center;'&gt;{_watermarkText}&lt;/div&gt;&lt;/div&gt;";

eo_support
Posted: Wednesday, April 5, 2023 9:30:27 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,088
Great. Please feel free to let us know if you run into anything else.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.