合并 XPS 文件 | 通过 .NET 为 Python 编写的 Aspose.Page
您可以通过免费在线 XPS Merger 检查 Aspose.Page XPS Merger 的质量并查看结果
要执行 XPS 合并,请按照以下步骤操作:
- 为第一个输入 XPS 文档初始化输入流
- 为输出 XPS 文档初始化输出流
- 创建一个将与第一个 XPS 文件合并的 XPS 文件数组
- 从先前创建的输入流创建 XpsDocument 实例
- 将 XPS 文件与创建的文档合并并保存
以下示例代码片段演示了如何使用 Python 将 XPS 文件合并为 PDF 文档:
1# For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Python
2
3from aspose.page.xps import *
4from aspose.page.xps.presentation.pdf import *
5from util import Util
6###############################################
7###### Class and Method declaration here ######
8###############################################
9
10# The path to the documents directory.
11data_dir = Util.get_data_dir_working_with_document_merging()
12# Initialize the PDF output stream
13with open(data_dir + "mergedXPSfiles.pdf", "wb") as pdf_stream:
14 # Initialize an XPS input stream
15 with open(data_dir + "input.xps", "rb") as xps_stream:
16 # Load the XPS document form the stream
17 document = XpsDocument(xps_stream, XpsLoadOptions())
18 # or load the XPS document directly from a file. No xpsStream is needed then.
19 # XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
20
21 # Initialize an options object with necessary parameters.
22 options = PdfSaveOptions()
23
24 options.jpeg_quality_level = 100
25 options.image_compression = PdfImageCompression.JPEG
26 options.text_compression = PdfTextCompression.FLATE
27
28 # Create a rendering device for PDF format
29 device = PdfDevice(pdf_stream)
30
31 # Create an array of XPS files that will be merged with the first one
32 files_to_merge = [ data_dir + "Demo.xps", data_dir + "sample.xps" ]
33
34 # Merge XPS files to output PDF document
35 document.merge(files_to_merge, device, options)Aspose.Page Python XPS 合并器还允许将 XPS 文件合并为 PDF 文档。
执行 XPS 到 PDF 合并需要执行以下几个步骤:
- 为第一个输入 XPS 文档初始化输入流。
- 为输出 PDF 文档初始化输出流。
- 创建一个将与第一个 XPS 文件合并的 XPS 文件数组。
- 从先前创建的输入流创建 XpsDocument 的实例。
- 指定 PdfSaveOptions 的 TextCompression、ImageCompression、JpegQualityLevel 和其他选项。
- 从之前创建的输出流创建 PdfDevice 的实例。
- 将 XPS 文件合并到创建的文档中,并使用 PDF 保存选项将其保存为 PDF。
以下代码片段展示了如何在 Python 中将多个 XPS 文件合并为 PDF 文档:
1# For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Python
2
3from aspose.page.xps import *
4from util import Util
5###############################################
6######## Class and Method declaration #########
7###############################################
8
9# The path to the documents directory.
10data_dir = Util.get_data_dir_working_with_document_merging()
11# Initialize an XPS output stream
12with open(data_dir + "mergedXPSfiles.xps", "wb") as out_stream:
13 # Initialize the XPS input stream
14 with open(data_dir + "input.xps", "rb") as in_stream:
15 # Load the XPS document from the stream
16 document = XpsDocument(in_stream, XpsLoadOptions())
17 # or load the 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 files_to_merge = [ data_dir + "Demo.xps", data_dir + "sample.xps" ]
22
23 # Merge XPS files to the output PDF document
24 document.merge(files_to_merge, out_stream)我们来看一下 PdfSaveOptions。使用此类,我们可以在将 XPS 合并为 PDF 时指定不同的转换参数。
- 当 ImageCompression 算法设置为 JPEG 时,JpegQualityLevel 会调节 PDF 文档中的图像质量。其值范围为 0 到 100。
- ImageCompression 算法封装在 PdfImageCompression 枚举中,提供多种选项,包括运行长度编码 (RLE)、Flate、带有基本或优化预测器的 Lempel-Ziv-Welch (LZW)、JPEG、无(原始图像字节)和自动(针对每幅图像采用最合适的压缩方式)。默认设置为自动压缩。
- TextCompression 算法封装在 PdfTextCompression 枚举中,提供多种选项,包括运行长度编码 (RLE)、Flate、Lempel-Ziv-Welch (LZW) 和无。 XPS 与 PDF 合并的默认值为 Flate。
- EncryptionDetails 封装在 PdfEncryptionDetails 中。它允许配置 PDF 文档的加密算法、权限、所有者和用户密码。
- OutlineTreeExpansionLevel 和 OutlineTreeHeight 参数用于控制文档轮廓的视图。