Page 1 of 1

Add transparent image as background to PDF

Posted: Mon Jan 14, 2019 1:03 pm
by David_karlsson
Hi !
I'm evaluating Vintasoft.Imaging.Pdf. So far Vintasoft.Imaging.Pdf is very good. Unfortunately I do not find sample code to be able to add background image. I need Water Stamp/Watermark PDF . Therefore, I need to add a transparent image as a backround on pdf.

Re: Add transparent image as background to PDF

Posted: Mon Jan 14, 2019 2:33 pm
by Alex
Hi David,

Thank you for your interest to our product.

For solving your task you need to create a PDF graphics for PDF page and specify that graphics must add new content before existing content:
https://www.vintasoft.com/docs/vsimagin ... ode).html

Here is an example that shows how to draw a watermark on the first page of PDF document:

Code: Select all

// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument = new Vintasoft.Imaging.Pdf.PdfDocument("document.pdf"))
{
    // create PDF graphics for first PDF page and specify that graphics must add new content before existing content
    using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics pdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(
        pdfDocument.Pages[0],
        Vintasoft.Imaging.Pdf.Drawing.PdfGraphicsCreationMode.Prepend))
    {
        // create "watermark" image
        using (Vintasoft.Imaging.VintasoftImage image = new Vintasoft.Imaging.VintasoftImage("watermark.jpg"))
        {
            // draw "watermark" image on first PDF page
            pdfGraphics.DrawImage(image, new System.Drawing.RectangleF(0, 0, 600, 200));
        }
    }

    // save PDF document to a new file
    pdfDocument.Save("documentWithWatermark.pdf");
}

Please read how to draw graphics on PDF page here: https://www.vintasoft.com/docs/vsimagin ... _Page.html

Best regards, Alexander

Re: Add transparent image as background to PDF

Posted: Mon Jan 14, 2019 3:18 pm
by David_karlsson
Thank you Alex.
I tested the code you posted but I do get an exception "{"Index was outside the bounds of the array."} System.IndexOutOfRangeException.

Is it possible to change opacity of the background image ?

Re: Add transparent image as background to PDF

Posted: Mon Jan 14, 2019 3:51 pm
by Alex
David_karlsson wrote: Mon Jan 14, 2019 3:18 pm I tested the code you posted but I do get an exception "{"Index was outside the bounds of the array."} System.IndexOutOfRangeException.

Is it possible to change opacity of the background image ?
Could you send us (to support@vintasoft.com) a small working project, which demonstrates the problem? We need to reproduce the problem on our side.

Best regards, Alexander