Merge XPS documents to PDF

C++ API solution to combine several XPS files

 

The XPS file format allows the storage of multiple documents within a single file and is also able to contain multiple pages. Aspose.Page API Solution for C++ offers the ability to merge multiple files into one, resulting in an XPS or PDF file that contains the combined content of all the original files.

Here you will find a code example of how to use the API for merging XPS files. To explore how this functionality can be integrated into a web solution or used for online file merging, you can try out the cross-platform XPS Merger tool.

To merge PS and EPS files, you need:

  • Aspose.Page for C++ API which is a feature-rich, powerful and easy to use document manipulation and conversion API for C++ platform.

  • You can download its latest version directly, just open NuGet package manager, and search for Aspose.Page.Cpp and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Steps to merge several XPS files to PDF with C++.

  1. Set the path to the documents directory.
  2. Initialize the PDF output and XPS file input streams.
  3. Create an instance of XpsDocument from created earlier input stream.
  4. Initialize options object with the TextCompression, ImageCompression, JpegQualityLevel parameters for the output file by means of the PdfSaveOptions Class.
  5. Using the PdfDevice Class, create a rendering device for PDF format.
  6. Merge XPS files to output XPS document.

C++ Code to merge XPS to PDF

    using Aspose::Page::XPS;
    using Aspose::Page::IO;
    using Aspose::Page::Presentation::Pdf;
    // 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);

		    // Create an array of XPS files that will be merged with the first one
		    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});

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

Steps to merge several XPS files into a single file with C++.

  1. Initialize the XPS output and input streams.
  2. Create an array of XPS files to be merged with the first one.
  3. Create an instance of XpsDocument from created earlier input stream.
  4. Merge XPS files to output XPS document.

C++ Code to merge XPS to XPS

    // Output stream
    System::SharedPtr<System::IO::Stream> outStream = System::IO::File::Open(RunExamples::outDir() + u"mergedXPSfiles.xps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
    // Clearing resources under 'using' statement
    System::Details::DisposeGuard<1> __dispose_guard_1({ outStream });
    // ------------------------------------------
    try {
	    System::SharedPtr<System::IO::Stream> inStream = 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({ inStream });
	    // ------------------------------------------

	    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());

		    // Create an array of XPS files that will be merged with the first one
		    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});

		    document->Merge(filesForMerge, outStream);
	    }
	    catch (...)
	    {
		    __dispose_guard_0.SetCurrentException(std::current_exception());
	    }
    }
    catch (...)
    {
	    __dispose_guard_1.SetCurrentException(std::current_exception());
    }

XPS What is XPS File Format

XPS format is similar to PDF format. Both are page description language (PDL) formats. EPS is based on HTML and not on PostScript language. The .eps file is capable to contain a markup of the document's structure along with the information on how the document would look like. There are also added instructions on how to print and render the document. The feature of the format is that it fixes the document's description which means that it will look the same no matter who and from what operational system opens it.