Add transparent image as background to PDF

Questions, comments and suggestions concerning VintaSoft Imaging .NET SDK.

Moderator: Alex

Post Reply
David_karlsson
Posts: 8
Joined: Mon Jan 14, 2019 12:55 pm

Add transparent image as background to PDF

Post 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.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Add transparent image as background to PDF

Post 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
David_karlsson
Posts: 8
Joined: Mon Jan 14, 2019 12:55 pm

Re: Add transparent image as background to PDF

Post 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 ?
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Add transparent image as background to PDF

Post 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
Post Reply