Convert PDF to images in C++
Render PDF pages as BMP, GIF, JPEG, PNG, or multipage TIFF and export SVG files.
Configure the Aspose.PDF for C++ SDK before running these functions. Pass an existing input file and a writable destination path; create any required output directories first. Each function is an independent example, and its options do not carry over to other examples.
Render every PDF page as BMP
Render all pages at 300 DPI with BmpDevice. Each file name appends the one-based page number and .bmp to outputFile; pass a prefix such as output/page- rather than a complete image file name.
- Open the PDF and get its Pages collection.
- Create the image device with Resolution(300).
- Iterate from page 1 through the page count and create a separate FileStream for each numbered output file.
- Call the device Process() method for the current page, then close the output stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Devices/BmpDevice.h>
#include <Aspose.PDF.Cpp/Devices/Resolution.h>
#include <system/io/file_stream.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Devices;
void ConvertPdfToBmp(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto pages = document->get_Pages();
auto device = MakeObject<BmpDevice>(MakeObject<Resolution>(300));
for (int page = 1; page <= pages->get_Count(); ++page) {
auto output = MakeObject<System::IO::FileStream>(outputFile + String::Format(u"{0}.bmp", page), System::IO::FileMode::Create);
device->Process(pages->idx_get(page), output);
output->Close();
}
}
}
Render every PDF page as GIF
Render all pages at 300 DPI with GifDevice. Each file name appends the one-based page number and .gif to outputFile; pass a prefix such as output/page- rather than a complete image file name.
- Open the PDF and get its Pages collection.
- Create the image device with Resolution(300).
- Iterate from page 1 through the page count and create a separate FileStream for each numbered output file.
- Call the device Process() method for the current page, then close the output stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Devices/GifDevice.h>
#include <Aspose.PDF.Cpp/Devices/Resolution.h>
#include <system/io/file_stream.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Devices;
void ConvertPdfToGif(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto pages = document->get_Pages();
auto device = MakeObject<GifDevice>(MakeObject<Resolution>(300));
for (int page = 1; page <= pages->get_Count(); ++page) {
auto output = MakeObject<System::IO::FileStream>(outputFile + String::Format(u"{0}.gif", page), System::IO::FileMode::Create);
device->Process(pages->idx_get(page), output);
output->Close();
}
}
}
Render every PDF page as JPEG
Render all pages at 300 DPI with JpegDevice. Each file name appends the one-based page number and .jpeg to outputFile; pass a prefix such as output/page- rather than a complete image file name.
- Open the PDF and get its Pages collection.
- Create the image device with Resolution(300).
- Iterate from page 1 through the page count and create a separate FileStream for each numbered output file.
- Call the device Process() method for the current page, then close the output stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Devices/JpegDevice.h>
#include <Aspose.PDF.Cpp/Devices/Resolution.h>
#include <system/io/file_stream.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Devices;
void ConvertPdfToJpeg(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto pages = document->get_Pages();
auto device = MakeObject<JpegDevice>(MakeObject<Resolution>(300));
for (int page = 1; page <= pages->get_Count(); ++page) {
auto output = MakeObject<System::IO::FileStream>(outputFile + String::Format(u"{0}.jpeg", page), System::IO::FileMode::Create);
device->Process(pages->idx_get(page), output);
output->Close();
}
}
}
Render every PDF page as PNG
Render all pages at 300 DPI with PngDevice. Each file name appends the one-based page number and .png to outputFile; pass a prefix such as output/page- rather than a complete image file name.
- Open the PDF and get its Pages collection.
- Create the image device with Resolution(300).
- Iterate from page 1 through the page count and create a separate FileStream for each numbered output file.
- Call the device Process() method for the current page, then close the output stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Devices/PngDevice.h>
#include <Aspose.PDF.Cpp/Devices/Resolution.h>
#include <system/io/file_stream.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Devices;
void ConvertPdfToPng(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto pages = document->get_Pages();
auto device = MakeObject<PngDevice>(MakeObject<Resolution>(300));
for (int page = 1; page <= pages->get_Count(); ++page) {
auto output = MakeObject<System::IO::FileStream>(outputFile + String::Format(u"{0}.png", page), System::IO::FileMode::Create);
device->Process(pages->idx_get(page), output);
output->Close();
}
}
}
Render PNG with a fallback font
Render every page at 300 DPI after setting RenderingOptions::DefaultFontName to Arial. Ensure Arial is available. Output names append the page number and .png to outputFile.
- Open the PDF and create a PngDevice with Resolution(300).
- Create RenderingOptions, set DefaultFontName to Arial, and assign the options to the device.
- Loop through all pages, process each page into its numbered PNG stream, and 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/Devices/PngDevice.h>
#include <Aspose.PDF.Cpp/Devices/Resolution.h>
#include <system/io/file_stream.h>
#include <Aspose.PDF.Cpp/RenderingOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Devices;
void ConvertPdfToPngWithDefaultFont(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto pages = document->get_Pages();
auto device = MakeObject<PngDevice>(MakeObject<Resolution>(300));
auto renderingOptions = MakeObject<RenderingOptions>();
renderingOptions->set_DefaultFontName(u"Arial");
device->set_RenderingOptions(renderingOptions);
for (int page = 1; page <= pages->get_Count(); ++page) {
auto output = MakeObject<System::IO::FileStream>(outputFile + String::Format(u"{0}.png", page), System::IO::FileMode::Create);
device->Process(pages->idx_get(page), output);
output->Close();
}
}
}
Export PDF to SVG
Disable CompressOutputToZipArchive and set TreatTargetFileNameAsDirectory to true. Pass a destination directory as outputFile; this example writes SVG output rather than a ZIP archive.
- Open the input PDF as Document.
- Create SvgSaveOptions.
- Set
CompressOutputToZipArchivetofalsein SvgSaveOptions. - Set
TreatTargetFileNameAsDirectorytotruein SvgSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/SvgSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToSvg(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<SvgSaveOptions>();
options->CompressOutputToZipArchive = false;
options->TreatTargetFileNameAsDirectory = true;
document->Save(outputFile, options);
}
}
Render all pages to one TIFF
Render pages 1 through the document page count into a single TIFF at 300 DPI. Use LZW compression, default color depth, and SkipBlankPages=false to retain blank pages.
- Open the PDF and create TiffSettings.
- Set Compression to LZW, Depth to Default, and SkipBlankPages to false.
- Create a TiffDevice with Resolution(300) and the settings.
- Process the document from page 1 through the page count into one output stream, then close it.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Devices/TiffDevice.h>
#include <Aspose.PDF.Cpp/Devices/TiffSettings.h>
#include <Aspose.PDF.Cpp/Devices/CompressionType.h>
#include <Aspose.PDF.Cpp/Devices/ColorDepth.h>
#include <Aspose.PDF.Cpp/Devices/Resolution.h>
#include <system/io/file_stream.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Devices;
void ConvertPdfToTiff(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto settings = MakeObject<TiffSettings>();
settings->set_Compression(CompressionType::LZW);
settings->set_Depth(ColorDepth::Default);
settings->set_SkipBlankPages(false);
auto device = MakeObject<TiffDevice>(MakeObject<Resolution>(300), settings);
auto output = MakeObject<System::IO::FileStream>(outputFile, System::IO::FileMode::Create);
device->Process(document, 1, document->get_Pages()->get_Count(), output);
output->Close();
}
}