views:

167

answers:

2

Has anyone used the Winnovative HTMLtoPDF converter to generate a PDF from HTML content? And if so, has anyone successfully put a watermark on any or all pages of that resulting PDF?

A: 

i have the same problem, did you get the solution?

juansalvador_malaga
Not really. However, we have upgraded to the latest version now, and it adds many extra special things. I was personally really looking to add an image to every page that ran from the very very top to the very very bottom, just on the far right side, that was 3 colored vertical lines spanning the whole page. I'm adding an answer right now that might help you out.
Matt Dawdy
A: 

This got interesting. From this page on Winnovative's site I found a code snippet that was useful. Search for 6.4.2.2 and read the code in that section.

Here is the code I used, based on the link I provided. This gets called AFTER I generate the pdf into a PDFDocument object.

public void PostDocProcessing(Winnovative.WnvHtmlConvert.PdfDocument.Document document, string sBackgroundImagePath)
{
    // get the first page the PDF document
    PdfPage firstPage = document.Pages[0];

    System.Drawing.Image logoImg = System.Drawing.Image.FromFile(sBackgroundImagePath);

    // calculate the watermark location
    System.Drawing.SizeF imageSizePx = logoImg.PhysicalDimension;

    // transform from pixels to points
    float imageWidthPoints = UnitsConverter.PixelsToPoints(imageSizePx.Width);
    float imageHeightPoints = UnitsConverter.PixelsToPoints(imageSizePx.Height);
    float watermarkXLocation = (firstPage.ClientRectangle.Width - imageWidthPoints);
    float watermarkYLocation = -50;

    // add a template watermark to the document repeated on each document page
    // the watermark size is equal to image size in points
    Template watermarkTemplate = document.AddTemplate(new System.Drawing.RectangleF(watermarkXLocation, watermarkYLocation, imageWidthPoints, imageHeightPoints));

    // add an image to the watermak
    ImageElement watermarkImageElement = new ImageElement(0, -300, logoImg);
    watermarkImageElement.Transparency = 100;
    watermarkTemplate.AddElement(watermarkImageElement);

    // dispose the image
    logoImg.Dispose();
}
Matt Dawdy