Merge XPS files | Aspose.Page for C++
You can check the quality of Aspose.Page XPS merging and view the results via free online XPS Merger
Aspose.Page C++ XPS merger allows to merge XPS files to XPS document on Windows and Linux.
It is necessary to do several steps in order to perform XPS merge:
- Initialize an output stream for output XPS document.
- Initialize an input stream for the first input XPS document.
- Create an array of XPS files that will be merged with the first one.
- Create an instance of XpsDocument from created earlier input stream.
- Merge XPS files with created document and save it.
Following code snippet shows how to merge XPS files in C++:
1//For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2// Output stream
3System::SharedPtr<System::IO::Stream> outStream = System::IO::File::Open(RunExamples::outDir() + u"mergedXPSfiles.xps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
4// Clearing resources under 'using' statement
5System::Details::DisposeGuard<1> __dispose_guard_1({ outStream });
6// ------------------------------------------
7try {
8 System::SharedPtr<System::IO::Stream> inStream = System::IO::File::Open(RunExamples::dataDir() + u"input.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
9 // Clearing resources under 'using' statement
10 System::Details::DisposeGuard<1> __dispose_guard_0({ inStream });
11 // ------------------------------------------
12
13 try
14 {
15 // Load XPS document form the stream
16 System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
17 // or load XPS document directly from file. No xpsStream is needed then.
18 // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
19
20 // Create an array of XPS files that will be merged with the first one
21 System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});
22
23 document->Merge(filesForMerge, outStream);
24 }
25 catch (...)
26 {
27 __dispose_guard_0.SetCurrentException(std::current_exception());
28 }
29}
30catch (...)
31{
32 __dispose_guard_1.SetCurrentException(std::current_exception());
33}
Aspose.Page C++ XPS merger allows also to merge XPS files into PDF document on Windows and Linux.
It is necessary to do several steps in order to perform XPS to PDF merge:
- Initialize an output stream for output PDF document.
- Initialize an input stream for the first input XPS document.
- Create an array of XPS files that will be merged with the first one.
- Create an instance of XpsDocument from created earlier input stream.
- Specify TextCompression, ImageCompression, JpegQualityLevel, and other options of PdfSaveOptions.
- Create an instance of PdfDevice from created earlier output stream.
- Merge XPS files with created document and save it as PDF with PDF save options.
Following code snippet shows how to merge XPS files to PDF document in C++:
1//For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2// Input file
3System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(RunExamples::outDir() + u"XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
4// Clearing resources under 'using' statement
5System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
6// ------------------------------------------
7try {
8 System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(RunExamples::dataDir() + u"input.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
9 // Clearing resources under 'using' statement
10 System::Details::DisposeGuard<1> __dispose_guard_0({ xpsStream });
11 // ------------------------------------------
12
13 try
14 {
15 // Load XPS document form the stream
16 System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
17 // or load XPS document directly from file. No xpsStream is needed then.
18 // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
19
20 // Initialize options object with necessary parameters.
21 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; }();
22
23 // Create rendering device for PDF format
24 System::SharedPtr<Aspose::Page::Xps::Presentation::Pdf::PdfDevice> device = System::MakeObject<Aspose::Page::Xps::Presentation::Pdf::PdfDevice>(pdfStream);
25
26 // Create an array of XPS files that will be merged with the first one
27 System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});
28
29 document->Merge(filesForMerge, device, options);
30 }
31 catch (...)
32 {
33 __dispose_guard_0.SetCurrentException(std::current_exception());
34 }
35}
36catch (...)
37{
38 __dispose_guard_1.SetCurrentException(std::current_exception());
39}
Let’s consider
PdfSaveOptions. Using this class we can assign different conversion parameters while merging 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 merging is Flate.
- EncryptionDetails encapsulated in PdfEncryptionDetails. It allows setting encryption algorithm, permisions, owner and user passwords for PDF document.
- OutlineTreeExpansionLevel and OutlineTreeHeight controls view of document outline.
Evaluate XPS merging online on our XPS Merger.
You can download examples and data files from
GitHub.