Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Read the complete article about converting PUB files to Images using C++ by visiting the following link.
https://blog.aspose.com/2021/07/23/convert-pub-to-image-using-cpp/
// Source PUB and output PDF file paths
System::String filePub = u"SourceDirectory\\1.pub";
System::String filePdf = u"OutputDirectory\\1.pdf";
// Load the PUB file
System::SharedPtr<IPubParser> parser = PubFactory::CreateParser(filePub);
System::SharedPtr<Aspose::Pub::Document> document = parser->Parse();
// Convert the PUB file to PDF
PubFactory::CreatePdfConverter()->ConvertToPdf(document, filePdf);
// Load the generated PDF file
auto pdfDocument = MakeObject<Aspose::Pdf::Document>(filePdf);
auto info = MakeObject<Facades::PdfFileInfo>(pdfDocument);
// Iterate through the PDF pages
for (auto page : pdfDocument->get_Pages())
{
// Get dimensions of the PDF page
int width = info->GetPageWidth(page->get_Number());
int height = info->GetPageHeight(page->get_Number());
// Create an instance of the Resolution class
auto resolution = MakeObject<Devices::Resolution>(300);
// Create JPEG device with the specified Width, Height and Resolution
auto device = MakeObject<Devices::JpegDevice>(width, height, resolution);
// Create the File Stream for the output image
System::SharedPtr<System::IO::FileStream> imageStream = System::IO::File::Create(String::Format(u"OutputDirectory\\page_{0}.jpg", page->get_Number()));
// Convert the PDF page to JPG image
device->Process(page, imageStream);
// Close the stream
imageStream->Close();
}
// Source PUB and output PDF file paths
System::String filePub = u"SourceDirectory\\1.pub";
System::String filePdf = u"OutputDirectory\\1.pdf";
// Load the PUB file
System::SharedPtr<IPubParser> parser = PubFactory::CreateParser(filePub);
System::SharedPtr<Aspose::Pub::Document> document = parser->Parse();
// Convert the PUB file to PDF
PubFactory::CreatePdfConverter()->ConvertToPdf(document, filePdf);
// Load the generated PDF file
auto pdfDocument = MakeObject<Aspose::Pdf::Document>(filePdf);
auto info = MakeObject<Facades::PdfFileInfo>(pdfDocument);
// Iterate through the PDF pages
for (auto page : pdfDocument->get_Pages())
{
// Get dimensions of the PDF page
int width = info->GetPageWidth(page->get_Number());
int height = info->GetPageHeight(page->get_Number());
// Create an instance of the Resolution class
auto resolution = MakeObject<Devices::Resolution>(300);
// Create PNG device with the specified Width, Height and Resolution
auto device = MakeObject<Devices::PngDevice>(width, height, resolution);
// Create the File Stream for the output image
System::SharedPtr<System::IO::FileStream> imageStream = System::IO::File::Create(String::Format(u"OutputDirectory\\page_{0}.png", page->get_Number()));
// Convert the PDF page to PNG image
device->Process(page, imageStream);
// Close the stream
imageStream->Close();
}
// Source PUB and output PDF file paths
System::String filePub = u"SourceDirectory\\1.pub";
System::String filePdf = u"OutputDirectory\\1.pdf";
// Load the PUB file
System::SharedPtr<IPubParser> parser = PubFactory::CreateParser(filePub);
System::SharedPtr<Aspose::Pub::Document> document = parser->Parse();
// Convert the PUB file to PDF
PubFactory::CreatePdfConverter()->ConvertToPdf(document, filePdf);
// Load the generated PDF file
auto pdfDocument = MakeObject<Aspose::Pdf::Document>(filePdf);
auto info = MakeObject<Facades::PdfFileInfo>(pdfDocument);
// Get dimensions of the first PDF page
int width = info->GetPageWidth(1);
int height = info->GetPageHeight(1);
// Create an instance of the Resolution class
auto resolution = MakeObject<Devices::Resolution>(300);
// Create an instance of TiffSettings class and set the required settings
auto settings = MakeObject<Devices::TiffSettings>();
settings->set_Compression(Devices::CompressionType::None);
settings->set_Depth(Devices::ColorDepth::Default);
// Create TIFF device with the specified Width, Height, Resolution and TiffSettings
auto device = MakeObject<Devices::TiffDevice>(width, height, resolution, settings);
// Create the File Stream for the output image
System::SharedPtr<System::IO::FileStream> imageStream = System::IO::File::Create(u"OutputDirectory\\pdf.tiff");
// Convert the PDF file to TIFF image
device->Process(pdfDocument, 1, 1, imageStream);
// Close the stream
imageStream->Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment