Add, Export, Remove and Transform Images in PDF

In the same way as other document formats, PDF documents require manipulating images and graphics to represent elements when text alone isn't enough. Manipulation doesn't mean only adding images, but sometimes replacing the existing ones or exporting them to a new document, for example. This article will teach you how to add, export, remove, and transform images in your PDF files programmatically, using the GemBox.Pdf API in C#.

How to Manipulate Images in PDF Programmatically

GemBox.Pdf is a standalone .NET component, which doesn't depend on Adobe Acrobat, and it's 100% managed code. This API allows you to read, write, create, and update PDF files in .NET, .NET Core, .NET Framework, Mono, and Xamarin.

GemBox.Pdf also provides an API for manipulating PDF file security options, allowing you to encrypt and digitally sign your files, and manage other PDF content in a very straightforward way.

Furthermore, you can work with images by adding, exporting, removing, and transforming them in an existing PDF file in a much faster and optimized way.

The following topics will cover how to perform all of these actions, with examples of code you can use to edit PDF documents programmatically.

Add Images to a PDF File using C#

PDF file with added image
Screenshot of a PDF file with added image

You can follow the next steps to add or import an image to a PDF document using GemBox.Pdf.

  1. Use the PdfDocument class to create a new or load an existing PDF file.
  2. Load the image from a file using the PdfImage.Load(String) method.
  3. Set the location of the image on the page.
  4. Draw the image to the page with the PdfPage.Content.DrawImage(PdfImage, PdfPoint) method.
  5. Save the updated PDF file using the PdfDocument.Save(String) method.

See the code sample below to learn how to add images to a PDF file using C#.

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%");
    
    // Define image x and y position
    // 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 = 100;

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

    // Save the resulting document as a PDF file
    document.Save("AddImage.pdf");
}

Export Images from PDF using C#

PDF file with images
Screenshot of a PDF file with images

If you want to extract images from a PDF file in JPEG, BMP, PNG, or TIFF image formats, you can do it by following the next steps:

  1. With the PdfDocument class, load the existing PDF file with the images you want to export.
  2. Find images iterateing through PDF pages and each page's content elements.
  3. Save the found images in the desired format using one of the PdfImageContent.Save methods.

The following code sample shows how to extract images from PDF using C#.

using (var document = PdfDocument.Load("%#ExportImages.pdf%"))
{
    var i = 0;

    // Filter image elements by iterating through all PDF page content elements
    var elements = document.Pages
        .SelectMany(p => p.Content.Elements.All())
        .Where(e => e.ElementType == PdfContentElementType.Image)
        .Cast<PdfImageContent>();

    // Iterate through all image elements
    foreach (var element in elements)
        // Export an image content element to the selected image format
        element.Save($"Image_{++i}.jpeg");
}

Remove Images from PDF using C#

PDF file with removed image
Screenshot of a PDF file with removed image

You can always remove the images from existing PDF files. The process of removing images from a PDF file using C# consists of 3 easy steps:

  1. Load the PDF file using the PdfDocument class.
  2. Remove image(s) using the PdfContentElementCollection.Remove(PdfContentElement) method.
  3. Save the updated PDF file using one of the PdfDocument.Save methods.

The following code sample shows how to remove an image from a PDF using C#.

using (var document = PdfDocument.Load("%#ExportImages.pdf%"))
{
    // Find first image in the PDF document by iterating through all page content elements
    var image = document.Pages
        .SelectMany(p => p.Content.Elements.All())
        .Where(e => e.ElementType == PdfContentElementType.Image)
        .Cast<PdfImageContent>()
        .FirstOrDefault();

    // Remove image from its parent collection
    image.Collection.Remove(image);

    // Save modified document to a PDF file
    document.Save("RemoveImage.pdf");
}

Position and transform images in PDF using C#

PDF file with transformed image
Screenshot of a PDF file with transformed image

With GemBox.Pdf, you can also position and apply various transformations, such as rotation or scaling, to an image drawn on a PDF page.

For example, if you want to mirror an image, you can follow the next steps:

  1. Create a new or load an existing PDF file with the PdfDocument class.
  2. Set the location of the image on the page.
  3. Draw the image using the PdfPage.Content.DrawImage(PdfImage, PdfPoint) method.
  4. Start the transformation by getting an identity PdfMatrix. Use a combination of PdfMatrix.Translate and PdfMatrix.Scale methods to flip the image.
  5. Draw the transformed image using the PdfPage.Content.DrawImage(PdfImage, PdfMatrix) method.
  6. Save the PDF document to a file using one of the PdfDocument.Save methods.

Next, you can see the code sample showing how to position and mirror an image in PDF, using C#.

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

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

    // Define a page margin
    var margin = 20.0;

    // Set the location of the image in the top-left corner of the page (with a specified margin)
    var x = margin;
    var 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
    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);

    // Save the document to a PDF file
    document.Save("TransformImage.pdf");
}

Conclusion

Images are crucial elements in PDF documents. That's why it's essential to understand how to manipulate them properly when managing PDF in C#. After completing this tutorial, you can start creating PDF documents with images or edit existing ones in a much easier and faster way.

For more information, check the GemBox.Pdf documentation.

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy