ImageGear .NET v25.1 - Updated
Add Image to PDF
User 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#
Copy Code
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);
        }
    }
}
VB.NET
Copy Code
Imports System.IO
Imports ImageGear.Core
Imports ImageGear.Formats
Imports ImageGear.Formats.PDF
 
Public Sub addImageWatermarkToDocument(igDocument As ImGearPDFDocument, watermarkImagePath As String, watermarkImagePageIndex As Integer)
    ' Set the image as half transparent.
    Dim watermarkOpacity As Double = 0.5
 
    ' Open a watermark image.
    Using imageFileStream As FileStream = New FileStream(watermarkImagePath, FileMode.Open, FileAccess.Read)
        Dim loadedIgRasterPage As ImGearPage = ImGearFileFormats.LoadPage(imageFileStream, watermarkImagePageIndex)
        ' Add watermark image to each page in the document.
        For Each pdfPage As ImGearPDFPage In igDocument.Pages
            pdfPage.AddImage(loadedIgRasterPage, watermarkOpacity)
        Next
    End Using
End Sub

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#
Copy Code
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);
        }
    }
}
VB.NET
Copy Code
Imports System.IO
Imports ImageGear.Core
Imports ImageGear.Formats
Imports ImageGear.Formats.PDF
 
Public Sub addImageToDocument(igDocument As ImGearPDFDocument, imagePath As String, imagePageIndex As Integer)
    ' Set image as opaque.
    Dim opacity As Double = 1.0
 
    ' Set area to top left corner.
    Dim areaOnPage As ImGearDoubleRectangle = New ImGearDoubleRectangle(1.0, 1.0, 3.0, 3.5)
 
    ' Open a watermark image.
    Using imageFileStream As FileStream = New FileStream(imagePath, FileMode.Open, FileAccess.Read)
        Dim loadedIgRasterPage As ImGearPage = ImGearFileFormats.LoadPage(imageFileStream, imagePageIndex)
        ' Add watermark image to each page in the document.
        For Each pdfPage As ImGearPDFPage In igDocument.Pages
            pdfPage.AddImage(loadedIgRasterPage, opacity, areaOnPage)
        Next
    End Using
End Sub
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.