Merge XPS files | Aspose.Page for Python via .NET
You can check the quality of Aspose.Page XPS Merger and view the results via free online XPS Merger
To perform an XPS merge, follow these steps:
- Initialize an input stream for the first input XPS documen
- Initialize an output stream for the output XPS document.
- Create an array of XPS files that will be merged with the first one.
- Create an instance of XpsDocument rom the previously created input stream.
- Merge XPS files with created document and save it.
Here’s an example code snippet demonstrating how to merge XPS files into a PDF document using Python:
1# For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Python
2# The path to the documents directory.
3data_dir = Util.get_data_dir_working_with_document_merging()
4# Initialize the PDF output stream
5with open(data_dir + "mergedXPSfiles.pdf", "wb") as pdf_stream:
6 # Initialize an XPS input stream
7 with open(data_dir + "input.xps", "rb") as xps_stream:
8 # Load the XPS document form the stream
9 document = XpsDocument(xps_stream, XpsLoadOptions())
10 # or load the XPS document directly from a file. No xpsStream is needed then.
11 # XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
12
13 # Initialize an options object with necessary parameters.
14 options = PdfSaveOptions()
15
16 options.jpeg_quality_level = 100
17 options.image_compression = PdfImageCompression.JPEG
18 options.text_compression = PdfTextCompression.FLATE
19
20 # Create a rendering device for PDF format
21 device = PdfDevice(pdf_stream)
22
23 # Create an array of XPS files that will be merged with the first one
24 files_to_merge = [ data_dir + "Demo.xps", data_dir + "sample.xps" ]
25
26 # Merge XPS files to output PDF document
27 document.merge(files_to_merge, device, options)
Aspose.Page Python XPS merger allows also to merge XPS files to PDF document.
It is necessary to do several steps in order to perform XPS to PDF merge:
- Initialize an input stream for the first input XPS document.
- Initialize an output stream for output PDF 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 the previously created output stream.
- Merge XPS files into created document and save it as PDF with PDF save options.
Following code snippet shows how to merge several XPS files to PDF document in Python:
1# For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Python
2
3# The path to the documents directory.
4data_dir = Util.get_data_dir_working_with_document_merging()
5# Initialize an XPS output stream
6with open(data_dir + "mergedXPSfiles.xps", "wb") as out_stream:
7 # Initialize the XPS input stream
8 with open(data_dir + "input.xps", "rb") as in_stream:
9 # Load the XPS document from the stream
10 document = XpsDocument(in_stream, XpsLoadOptions())
11 # or load the XPS document directly from file. No xpsStream is needed then.
12 # XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
13
14 # Create an array of XPS files that will be merged with the first one
15 files_to_merge = [ data_dir + "Demo.xps", data_dir + "sample.xps" ]
16
17 # Merge XPS files to the output PDF document
18 document.merge(files_to_merge, out_stream)
Let’s consider PdfSaveOptions. Using this class we can assign different conversion parameters while merging XPS to PDF.
- JpegQualityLevel regulates the image quality within a PDF document when the ImageCompression algorithm is set to JPEG. Its value ranges from 0 to 100.
- ImageCompression algorithm encapsulated in PdfImageCompression Enumeration, offers several options including Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW) with base or optimized predictor, JPEG, none (raw image bytes), and auto (the most suitable compression for each image). The default setting is auto compression.
- TextCompression algorithm encapsulated in PdfTextCompression Enumeration, provides options such as Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW), and none. The default value during XPS to PDF merging is Flate.
- EncryptionDetails encapsulated in PdfEncryptionDetails. It allows the configuration of encryption algorithm, permissions, owner, and user passwords for the PDF document.
- OutlineTreeExpansionLevel and OutlineTreeHeight parameters regulate the view of the document outline.