ImageGear v26.0 - Updated
Add Image to PDF
Developer Guide > How to Work with ... > PDF > How to... > Add Image to PDF

To add an image to a page in a PDF, you can use the AddImage method. This method offers control over the area and opacity of the image being added.

The following snippet demonstrates how to add a watermark to each page in a document:

PDF support needs to be initialized first for this snippet to work. To get familiar with initializing IGNET, initializing PDF support, loading a PDF, saving a PDF, and terminating PDF support, try any one of the tutorials.

C#

using System.IO;
using ImageGear.Core;
using ImageGear.Formats;
using ImageGear.Formats.PDF;

public static void addImageWatermarkToDocument(ImGearPDFDocument igDocument, String watermarkImagePath, int watermarkImagePageIndex)
{
    // Set the image as half transparent.
    double watermarkOpacity = 0.50;
   
    // Open a watermark image.
    using (FileStream imageFileStream = new FileStream(watermarkImagePath, FileMode.Open, FileAccess.Read))
    {
        ImGearPage loadedIgRasterPage = ImGearFileFormats.LoadPage(imageFileStream, watermarkImagePageIndex);
        // Add watermark image to each page in the document.
        foreach (ImGearPDFPage pdfPage in igDocument.Pages)
        {
            pdfPage.AddImage(loadedIgRasterPage, watermarkOpacity);
        }
    }
}

The following snippet demonstrates how to specify the size and location of an image placed on each page in a document:

PDF support needs to be initialized first for this snippet to work. To get familiar with initializing IGNET, initializing PDF support, loading a PDF, saving a PDF, and terminating PDF support, try any one of the tutorials.

C#

using System.IO;
using ImageGear.Core;
using ImageGear.Formats;
using ImageGear.Formats.PDF;

public static void addImageToDocument(ImGearPDFDocument igDocument, String imagePath, int imagePageIndex)
{
    // Set image as opaque.
    double opacity = 1.0;
   
    // Set area to top left corner.
    ImGearDoubleRectangle areaOnPage = new ImGearDoubleRectangle(1.0, 1.0, 3.0, 3.5);
   
    // Open a watermark image.
    using (FileStream imageFileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
    {
        ImGearPage loadedIgRasterPage = ImGearFileFormats.LoadPage(imageFileStream, imagePageIndex);
        // Add watermark image to each page in the document.
        foreach (ImGearPDFPage pdfPage in igDocument.Pages)
        {
            pdfPage.AddImage(loadedIgRasterPage, opacity, areaOnPage);
        }
    }
}
Parameter Description
opacity The opacity parameter allows you to control the transparency of the image being added to the page, where 0.0 is transparent and 1.0 is opaque. The default value is opaque.
areaOnPage The areaOnPage parameter allows you to control the area on the page that the image should be placed. When either width or height equals 0.0, the entire page area is used. The default value is the entire page area.
Is this page helpful?
Yes No
Thanks for your feedback.