Convert XPS to PDF using C++

Contents
[ ]

Aspose.Page C++ XPS to PDF converter allows to convert XPS document to PDF document on Windows and Linux.
It is necessary to do several steps in order to perform XPS to PDF conversion:

  1. Initialize an output stream for output PDF document.
  2. Initialize an input stream for input XPS document.
  3. Create an instance of XpsDocument from created earlier input stream.
  4. Specify TextCompressionImageCompressionJpegQualityLevel, and other options of PdfSaveOptions.
  5. Create an instance of PdfDevice from created earlier output stream.
  6. Save XPS document as PDF with PDF save options.


Following code snippet shows how to convert XPS to PDF document in C++:

//For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
// Input file
System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(RunExamples::outDir() + u"XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
// ------------------------------------------
try {
	System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(RunExamples::dataDir() + u"input.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 form 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 options 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, 2, 6 })); 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);

		document->Save(device, options);
	}
	catch (...)
	{
		__dispose_guard_0.SetCurrentException(std::current_exception());
	}
}
catch (...)
{
	__dispose_guard_1.SetCurrentException(std::current_exception());
}

Let’s consider PdfSaveOptions. Using this class we can assign different conversion parameters while converting XPS to PDF.

  • JpegQualityLevel controls a quality of images in PDF document if ImageCompression algorithm is JPEG and can be from 0 to 100.
  • ImageCompression algorithm encapsulated in PdfImageCompression Enumeration, may be Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW) with base or optimized pedictor, JPEG , none (raw image bytes) and auto (the most appropriate compression for each image). Default is auto compression.
  • TextCompression algorithm encapsulated in PdfTextCompression Enumeration, may be Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW) and none. Default value in XPS to PDF conversion is Flate.
  • EncryptionDetails encapsulated in PdfEncryptionDetails. It allows setting encryption algorithm, permisions, owner and user passwords for PDF document.
  • PageNumbers represents an array of numbers of pages which will be saved to PDF.
  • OutlineTreeExpansionLevel and OutlineTreeHeight controls view of document outline.