Export and import images to PDF file in C# and VB.NET

Export images

GemBox.Pdf supports exporting images from the PDF file in JPEG, BMP, PNG or TIFF image formats.

The following example shows how you can export single image from the PDF file.

Image exported with GemBox.Pdf
Screenshot of image exported with GemBox.Pdf
Upload your file (Drag file here)
using GemBox.Pdf;
using GemBox.Pdf.Content;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("%InputFileName%"))
            // Iterate through PDF pages and through each page's content elements.
            foreach (var page in document.Pages)
                foreach (var contentElement in page.Content.Elements.All())
                    if (contentElement.ElementType == PdfContentElementType.Image)
                    {
                        // Export an image content element to selected image format.
                        var imageContent = (PdfImageContent)contentElement;
                        imageContent.Save("Export Images.%OutputFileType%");
                        return;
                    }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = PdfDocument.Load("%InputFileName%")
            ' Iterate through PDF pages and through each page's content elements.
            For Each page In document.Pages
                For Each contentElement In page.Content.Elements.All()
                    If contentElement.ElementType = PdfContentElementType.Image Then
                        ' Export an image content element to selected image format.
                        Dim imageContent = CType(contentElement, PdfImageContent)
                        imageContent.Save("Export Images.%OutputFileType%")
                        Return
                    End If
                Next
            Next
        End Using

    End Sub
End Module

Import images

GemBox.Pdf supports importing images in a PDF file from your C# or VB.NET application.

The following example shows how you can easily create a PDF document and import an image to its first page.

PDF file that contains imported image created with GemBox.Pdf library
Screenshot of PDF file that contains imported image created with GemBox.Pdf library
using GemBox.Pdf;
using GemBox.Pdf.Content;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = new PdfDocument())
        {
            // Add a page.
            var page = document.Pages.Add();

            // Load the image from a file.
            var image = PdfImage.Load("%#FragonardReader.jpg%");

            // Set the location of the bottom-left corner of the image.
            // We want the top-left corner of the image to be at location (50, 50)
            // from the top-left corner of the page.
            // NOTE: In PDF, location (0, 0) is at the bottom-left corner of the page
            // and the positive y axis extends vertically upward.
            double x = 50, y = page.CropBox.Top - 50 - image.Size.Height;

            // Draw the image to the page.
            page.Content.DrawImage(image, new PdfPoint(x, y));

            document.Save("Import Images.%OutputFileType%");
        }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = New PdfDocument()

            ' Add a page.
            Dim page = document.Pages.Add()

            ' Load the image from a file.
            Dim image = PdfImage.Load("%#FragonardReader.jpg%")

            ' Set the location of the bottom-left corner of the image.
            ' We want the top-left corner of the image to be at location (50, 50)
            ' from the top-left corner of the page.
            ' NOTE: In PDF, location (0, 0) is at the bottom-left corner of the page
            ' and the positive y axis extends vertically upward.
            Dim x As Double = 50, y As Double = page.CropBox.Top - 50 - image.Size.Height

            ' Draw the image to the page.
            page.Content.DrawImage(image, New PdfPoint(x, y))

            document.Save("Import Images.%OutputFileType%")
        End Using

    End Sub
End Module

Image positioning and transformations

The following example shows how to position and apply various transformations, such as rotation or scaling, to an image drawn on a PDF page.

PDF file that contains positioned and transformed image created with GemBox.Pdf library
Screenshot of PDF file that contains positioned and transformed image created with GemBox.Pdf library
using GemBox.Pdf;
using GemBox.Pdf.Content;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = new PdfDocument())
        {
            var page = document.Pages.Add();

            // Load the image from a file.
            var image = PdfImage.Load("%#Corner.png%");

            double margin = 50;

            // Set the location of the first image in the top-left corner of the page (with a specified margin).
            double x = margin;
            double y = page.CropBox.Top - margin - image.Size.Height;

            // Draw the first image.
            page.Content.DrawImage(image, new PdfPoint(x, y));

            // Set the location of the second image in the top-right corner of the page (with the same margin).
            x = page.CropBox.Right - margin - image.Size.Width;
            y = page.CropBox.Top - margin - image.Size.Height;

            // Initialize the transformation.
            var transform = PdfMatrix.Identity;
            // Use the translate operation to position the image.
            transform.Translate(x, y);
            // Use the scale operation to resize the image.
            // NOTE: The unit square of user space, bounded by user coordinates (0, 0) and (1, 1), 
            // corresponds to the boundary of the image in the image space.
            transform.Scale(image.Size.Width, image.Size.Height);
            // Use the scale operation to flip the image horizontally.
            transform.Scale(-1, 1, 0.5, 0);

            // Draw the second image.
            page.Content.DrawImage(image, transform);

            // Set the location of the third image in the bottom-left corner of the page (with the same margin).
            x = margin;
            y = margin;

            // Initialize the transformation.
            transform = PdfMatrix.Identity;
            // Use the translate operation to position the image.
            transform.Translate(x, y);
            // Use the scale operation to resize the image.
            transform.Scale(image.Size.Width, image.Size.Height);
            // Use the scale operation to flip the image vertically.
            transform.Scale(1, -1, 0, 0.5);

            // Draw the third image.
            page.Content.DrawImage(image, transform);

            // Set the location of the fourth image in the bottom-right corner of the page (with the same margin).
            x = page.CropBox.Right - margin - image.Size.Width;
            y = margin;

            // Initialize the transformation.
            transform = PdfMatrix.Identity;
            // Use the translate operation to position the image.
            transform.Translate(x, y);
            // Use the scale operation to resize the image.
            transform.Scale(image.Size.Width, image.Size.Height);
            // Use the scale operation to flip the image horizontally and vertically.
            transform.Scale(-1, -1, 0.5, 0.5);

            // Draw the fourth image.
            page.Content.DrawImage(image, transform);

            document.Save("Positioning and Transformations.%OutputFileType%");
        }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = New PdfDocument()

            Dim page = document.Pages.Add()

            ' Load the image from a file.
            Dim image = PdfImage.Load("%#Corner.png%")

            Dim margin As Double = 50

            ' Set the location of the first image in the top-left corner of the page (with a specified margin).
            Dim x As Double = margin
            Dim y As Double = page.CropBox.Top - margin - image.Size.Height

            ' Draw the first image.
            page.Content.DrawImage(image, New PdfPoint(x, y))

            ' Set the location of the second image in the top-right corner of the page (with the same margin).
            x = page.CropBox.Right - margin - image.Size.Width
            y = page.CropBox.Top - margin - image.Size.Height

            ' Initialize the transformation.
            Dim transform = PdfMatrix.Identity
            ' Use the translate operation to position the image.
            transform.Translate(x, y)
            ' Use the scale operation to resize the image.
            ' NOTE: The unit square of user space, bounded by user coordinates (0, 0) and (1, 1), 
            ' corresponds to the boundary of the image in the image space.
            transform.Scale(image.Size.Width, image.Size.Height)
            ' Use the scale operation to flip the image horizontally.
            transform.Scale(-1, 1, 0.5, 0)

            ' Draw the second image.
            page.Content.DrawImage(image, transform)

            ' Set the location of the third image in the bottom-left corner of the page (with the same margin).
            x = margin
            y = margin

            ' Initialize the transformation.
            transform = PdfMatrix.Identity
            ' Use the translate operation to position the image.
            transform.Translate(x, y)
            ' Use the scale operation to resize the image.
            transform.Scale(image.Size.Width, image.Size.Height)
            ' Use the scale operation to flip the image vertically.
            transform.Scale(1, -1, 0, 0.5)

            ' Draw the third image.
            page.Content.DrawImage(image, transform)

            ' Set the location of the fourth image in the bottom-right corner of the page (with the same margin).
            x = page.CropBox.Right - margin - image.Size.Width
            y = margin

            ' Initialize the transformation.
            transform = PdfMatrix.Identity
            ' Use the translate operation to position the image.
            transform.Translate(x, y)
            ' Use the scale operation to resize the image.
            transform.Scale(image.Size.Width, image.Size.Height)
            ' Use the scale operation to flip the image horizontally And vertically.
            transform.Scale(-1, -1, 0.5, 0.5)

            ' Draw the fourth image.
            page.Content.DrawImage(image, transform)

            document.Save("Positioning and Transformations.%OutputFileType%")
        End Using
    End Sub
End Module

The above example shows how to add multiple instances of the same image on a single page. To add multiple instances of the same image on multiple pages, see the Watermarks example.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.