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. You can follow the next steps to add or import an image to a PDF document using GemBox.Pdf. See the code sample below to learn how to add images to a PDF file using C#. 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: The following code sample shows how to extract images from PDF using C#. 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: The following code sample shows how to remove an image from a PDF using C#. 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: Next, you can see the code sample showing how to position and mirror an image in PDF, using C#. 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.Add Images to a PDF File using C#
PdfDocument
class to create a new or load an existing PDF file.PdfImage.Load(String)
method.PdfPage.Content.DrawImage(PdfImage, PdfPoint)
method.PdfDocument.Save(String)
method.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#
PdfDocument
class, load the existing PDF file with the images you want to export.PdfImageContent.Save
methods.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#
PdfDocument
class.PdfContentElementCollection.Remove(PdfContentElement)
method.PdfDocument.Save
methods.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#
PdfDocument
class.PdfPage.Content.DrawImage(PdfImage, PdfPoint)
method.PdfMatrix
. Use a combination of PdfMatrix.Translate
and PdfMatrix.Scale
methods to flip the image.PdfPage.Content.DrawImage(PdfImage, PdfMatrix)
method.PdfDocument.Save
methods.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