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++.
- C++ API for Working with Images in PDF Files
- 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 for Working with Images in PDF Files
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 using 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::SharedPtr<Aspose::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 File using C++
In order to extract images from a PDF file, 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::SharedPtr<System::IO::Stream> stream, System::SharedPtr<System::Drawing::Imaging::ImageFormat> format) method.
- Close the stream.
The following sample code demonstrates how to extract an image from a PDF file using C++.
Remove an Image from a PDF File using C++
The following are the steps to remove an image from a PDF file using 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 delete a particular image from a PDF file using C++.
Replace an Image in a PDF File using C++
In order to replace an existing image in a PDF file, 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::SharedPtr<System::IO::Stream> stream) method.
- Close the stream.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following code shows how to replace an image in a PDF file using C++.
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.