Welcome Guest Search | Active Topics | Sign In | Register

MVCToPDF RenderAsPDF Attribute with Watermark Options
ECE Support
Posted: Wednesday, May 30, 2018 11:42:51 AM
Rank: Newbie
Groups: Member

Joined: 7/21/2017
Posts: 2
Is it possible to set a watermark on a generated PDF when using the MVC [RenderAsPDF] attribute? Here is a example of my current code:

Code: C#
[RenderAsPDF]
public ActionResult Print(int id)
{
            // Make DB calls to populate viewmodel

            MVCToPDF.ResultFileName = "customname.pdf";

            EO.Pdf.HtmlToPdf.Options.PageSize = EO.Pdf.PdfPageSizes.Letter;
            EO.Pdf.HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 10f);
            EO.Pdf.HtmlToPdf.Options.BeforeRenderPage += delegate(object source, PdfPageEventArgs e)
            {               
                // Create a new text layer.
                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.Gray;
                textLayer.GfxMatrix.Rotate(45);

                // Create the text object
                EO.Pdf.Contents.PdfTextContent textContent = new EO.Pdf.Contents.PdfTextContent("DRAFT");
                textContent.PositionMode = EO.Pdf.Contents.PdfTextPositionMode.Offset;
                textContent.Offset = new EO.Pdf.Drawing.PdfPoint(350, 150);

                // Add the text object into the text layer object
                textLayer.Contents.Add(textContent);

                // Add the text layer into the page
                e.Page.Contents.Add(textLayer);
                
            };

            return View(viewModel);
}


The PDF is successfully created but the watermark never appears. If I place a breakpoint inside the delegate, it is never hit.

My version of EO.Pdf.MVC is EO.Total.2017 17.2.14.0.
eo_support
Posted: Wednesday, May 30, 2018 5:39:16 PM
Rank: Administration
Groups: Administration

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

You won't be able to create watermark below the main contents with the HTML to PDF converter this way. This is because most of the time a white solid background will be rendered for the main HTML output. This white background will completely block whatever you have rendered into the page in BeforeRenderPage event.

There are two alternatives:

1. Rely on the afterConvertHandler argument of the RenderAsPDF method:

https://www.essentialobjects.com/doc/eo.pdf.mvc.mvctopdf.renderaspdf_overload_2.aspx

However this will render additional output above (not below) the main contents. This is the closest to a watermark.

2. If you must have watermark below the main contents, then you can consider modifying your CSS to apply additional style to your body element through this property:

https://www.essentialobjects.com/doc/eo.pdf.htmltopdfoptions.userstylesheet.aspx

The disadvantage of this option is that it's hard to precisely control the position of the watermark since the CSS layout depends on the document layout. For example, if your HTML is only half a page, then it's impossible to create a full page watermark.

Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!
ECE Support
Posted: Wednesday, May 30, 2018 8:36:23 PM
Rank: Newbie
Groups: Member

Joined: 7/21/2017
Posts: 2
Thank you! I was able to get it working.
eo_support
Posted: Thursday, May 31, 2018 8:41:15 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,258
Great. Glad to hear that! Please feel free to let us know if there is 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.