Extract images from PDF in C++
Contents
[
Hide
]
Save an embedded PDF image, extract images whose placements lie in a region, and read image alternative text using Aspose.PDF for C++.
Save the first image
The input must contain at least one image resource on page 1. This exports an embedded image, not a rendered page.
- Open the input PDF as a Document.
- Create an output file stream.
- Select the first XImage resource on page 1.
- Call Save() on the image with the output stream.
- Close the stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/XImage.h>
#include <Aspose.PDF.Cpp/XImageCollection.h>
#include <Aspose.PDF.Cpp/ImagePlacement.h>
#include <Aspose.PDF.Cpp/ImagePlacementAbsorber.h>
#include <Aspose.PDF.Cpp/ImagePlacementCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Pdf;
void ExtractImage(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto output = System::IO::File::Create(outputFile);
document->get_Pages()->idx_get(1)->get_Resources()->get_Images()->idx_get(1)->Save(output);
output->Close();
}
Extract images placed inside a region
For example, image_index.jpg becomes image_1.jpg, image_2.jpg, and so on. The function filters placements by their rectangles and exports complete embedded images; it does not crop image pixels to the region.
- Require an output filename containing the literal text index.
- Open the input PDF as a Document.
- Visit page 1 with ImagePlacementAbsorber.
- Create a region Rectangle from (0, 0) to (590, 590).
- Check whether both lower-left and upper-right placement corners are contained in the region.
- For each matching ImagePlacement, save its XImage to a numbered output stream.
- Close each output stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/XImage.h>
#include <Aspose.PDF.Cpp/XImageCollection.h>
#include <Aspose.PDF.Cpp/ImagePlacement.h>
#include <Aspose.PDF.Cpp/ImagePlacementAbsorber.h>
#include <Aspose.PDF.Cpp/ImagePlacementCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Pdf;
void ExtractFromRegion(String inputFile, String outputFile)
{
if (outputFile.Contains(u"index") == false)
{
std::cout << "Please provide a valid output filename containing 'index'." << std::endl;
return;
}
auto document = MakeObject<Document>(inputFile);
auto page = document->get_Pages()->idx_get(1);
auto absorber = MakeObject<ImagePlacementAbsorber>();
page->Accept(absorber);
auto region = MakeObject<Rectangle>(0, 0, 590, 590);
int index = 1;
for (auto& placement : absorber->get_ImagePlacements())
{
auto rect = placement->get_Rectangle();
auto lower = MakeObject<Point>(rect->get_LLX(), rect->get_LLY());
auto upper = MakeObject<Point>(rect->get_URX(), rect->get_URY());
if (!region->Contains(lower, true) || !region->Contains(upper, true))
continue;
auto output = System::IO::File::Create(outputFile.Replace(u"index", String::Format(u"{0}", index++)));
placement->get_Image()->Save(output);
output->Close();
}
}
Read image alternative text
The outputFile parameter is unused: this function writes to the console. It prints only the first returned alternative-text item for each placement. Use an SDK version that exposes GetAlternativeText().
- Open the input PDF as a Document.
- Visit page 1 with ImagePlacementAbsorber.
- Iterate through the detected ImagePlacement objects.
- Call GetAlternativeText() on each XImage with the page.
- Print the first returned item, or an empty string if none is returned.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Resources.h>
#include <Aspose.PDF.Cpp/XImage.h>
#include <Aspose.PDF.Cpp/XImageCollection.h>
#include <Aspose.PDF.Cpp/ImagePlacement.h>
#include <Aspose.PDF.Cpp/ImagePlacementAbsorber.h>
#include <Aspose.PDF.Cpp/ImagePlacementCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Pdf;
void ExtractAltText(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto page = document->get_Pages()->idx_get(1);
auto absorber = MakeObject<ImagePlacementAbsorber>();
page->Accept(absorber);
for (auto& placement : absorber->get_ImagePlacements())
{
auto lines = placement->get_Image()->GetAlternativeText(page);
std::cout << "Alt Text: " << (lines->get_Count() ? lines->idx_get(0) : String::Empty) << std::endl;
}
}