Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/8beeaf080a0e3e59c1936ced7c066456 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/8beeaf080a0e3e59c1936ced7c066456 to your computer and use it in GitHub Desktop.
Convert XPS or OXPS Files to PDF Format using C++
Convert XPS or OXPS Files to PDF Format using C++
// Output file
System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(u"OutputDirectory\\XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
// ------------------------------------------
try {
// Source file
System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(u"SourceDirectory\\sample.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_0({ xpsStream });
// ------------------------------------------
try
{
// Load XPS document from the stream
System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize PdfSaveOptions object with necessary parameters.
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions> options = [&] {
auto tmp_0 = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions>();
tmp_0->set_JpegQualityLevel(100);
tmp_0->set_ImageCompression(Aspose::Page::XPS::Presentation::Pdf::PdfImageCompression::Jpeg);
tmp_0->set_TextCompression(Aspose::Page::XPS::Presentation::Pdf::PdfTextCompression::Flate);
tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 3 }));
return tmp_0;
}();
// Create rendering device for PDF format
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfDevice> device = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfDevice>(pdfStream);
// Save PDF file
document->Save(device, options);
}
catch (...)
{
__dispose_guard_0.SetCurrentException(std::current_exception());
}
}
catch (...)
{
__dispose_guard_1.SetCurrentException(std::current_exception());
}
// Output file
System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(u"OutputDirectory\\XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
// ------------------------------------------
try {
// Source file
System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(u"SourceDirectory\\sample.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_0({ xpsStream });
// ------------------------------------------
try
{
// Load XPS document from the stream
System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize PdfSaveOptions object with necessary parameters.
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions> options = [&] {
auto tmp_0 = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions>();
tmp_0->set_JpegQualityLevel(100);
tmp_0->set_ImageCompression(Aspose::Page::XPS::Presentation::Pdf::PdfImageCompression::Jpeg);
tmp_0->set_TextCompression(Aspose::Page::XPS::Presentation::Pdf::PdfTextCompression::Flate);
return tmp_0;
}();
// Create rendering device for PDF format
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfDevice> device = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfDevice>(pdfStream);
// Save PDF file
document->Save(device, options);
}
catch (...)
{
__dispose_guard_0.SetCurrentException(std::current_exception());
}
}
catch (...)
{
__dispose_guard_1.SetCurrentException(std::current_exception());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment