Extract images from PDF in C++
Contents
[
Hide
]
Save an embedded image resource from a PDF page as JPEG using Aspose.PDF for C++.
Extract the first image from page 1
Export one image from the first page resource collection.
The input must have page 1 and at least one image resource on it. The example does not iterate over all images.
- Open the input PDF as a Document.
- Select page 1 and retrieve its first XImage resource.
- Open a writable output stream.
- Save the image using JPEG encoding.
- Close the stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/XImage.h>
#include <Aspose.PDF.Cpp/XImageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;
void ExtractImage(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto xImage = document->get_Pages()->idx_get(1)->get_Resources()->get_Images()->idx_get(1);
auto outputImage = System::IO::File::OpenWrite(outputFile);
xImage->Save(outputImage, System::Drawing::Imaging::ImageFormat::get_Jpeg());
outputImage->Close();
}