Images can be used to show a variety of things ranging from product photographs to workflows and flow charts. PDF files can contain images based on the type of information that it has. For example, a chair installation guide will include pictures of the chair demonstrating how to assemble it. There might be scenarios where you need to manipulate images in PDF files programmatically. For such cases, this article will teach you how to work with images in PDF files using C++. Particularly, we will cover how to add, replace, extract and remove images in a PDF using C++.
- C++ API to Add, Update, Remove, and Replace PDF Images
- Add an Image to a PDF File using C++
- Extract Images from a PDF File using C++
- Remove an Image from a PDF File using C++
- Replace an Image in a PDF File using C++
C++ API to Add, Update, Remove, and Replace PDF Images
Aspose.PDF for C++ is a robust C++ library that allows you to create, read, and modify PDF files. Furthermore, the API supports working with images in PDF documents. You can either install the API through NuGet or download it directly from the downloads section.
PM> Install-Package Aspose.PDF.Cpp
Add an Image to a PDF File in C++
The following are the steps to add an image to a PDF file.
- Load the PDF file using the Document class.
- Retrieve the page where you want to add the image using the Document->get_Pages()->idx_get(int32_t index) method.
- Create an instance of the Rectangle class.
- Add the image to the page using the Page->AddImage(System::String imagePath, System::SharedPtrAspose::Pdf::Rectangle rectangle) method.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to add an image to a PDF file using C++.
Extract Images from a PDF in C++
In order to extract images from a PDF file in C++, follow the steps given below.
- Load the PDF file using the Document class.
- Extract the image using the Document->get_Pages()->idx_get(int32_t index)->get_Resources()->get_Images()->idx_get(int32_t index) method.
- Create an instance of the FileStream class to save the output image.
- Save the image using the XImage->Save(System::SharedPtrSystem::IO::Stream stream, System::SharedPtrSystem::Drawing::Imaging::ImageFormat format) method.
- Close the stream.
The following sample code demonstrates how to extract an image from a PDF in C++.
Remove an Image from a PDF File using C++
The following are the steps to remove an image from a PDF file in C++.
- Load the PDF file using the Document class.
- Delete a particular image using the Document->get_Pages()->idx_get(int32_t index)->get_Resources()->get_Images()->Delete(int32_t index) method.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to remove an image from a PDF file in C++.
Replace an Image in a PDF in C++
In order to replace an image in a PDF in C++, follow the steps given below.
- Load the PDF file using the Document class.
- Open the new image using the FileStream class.
- Replace the old image using the Document->get_Pages()->idx_get(int32_t index)->get_Resources()->get_Images()->Replace(int32_t index, System::SharedPtrSystem::IO::Stream stream) method.
- Close the stream.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following code sample shows how to replace an image in a PDF file in C++.
C++ API to Work with PDF Images - Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to work with images in PDF files using C++. Specifically, you have learned how to add, extract, replace and remove images from PDF files using Aspose.PDF for C++ API. The API provides a bunch of additional features for working with PDF files that you can explore in detail by visiting the official documentation. In case of any queries, please feel free to reach us at our free support forum.