合并 XPS 文件 | Aspose.Page for Java
您可以通过免费在线工具 XPS Merger
Aspose.Page Java XPS Merger 允许在任何支持 Java 虚拟机的操作系统上将 XPS 文件合并为 XPS 文档。
执行 XPS 合并需要执行以下几个步骤:
- 从第一个 XPS 文件创建 XpsDocument 实例。
- 创建一个将与第一个 XPS 文件合并的 XPS 文件数组。
- 将 XPS 文件与创建的文档合并并保存。
以下代码片段展示了如何在 Java 中将 XPS 文件合并为 PDF 文档:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir();
4// Load the first XPS file in a document
5XpsDocument document = new XpsDocument(dataDir + "input.xps");
6
7// Create an array of XPS files that will be merged with the first one
8String[] filesForMerge = new String[] { dataDir + "Demo.xps", dataDir + "sample.xps" };
9
10//merge and save to output XPS file
11document.merge(filesForMerge, dataDir + "mergedXPSfiles.xps");
Aspose.Page Java XPS 合并器也允许将 XPS 文件合并为 PDF 文档。
执行 XPS 到 PDF 合并需要执行以下几个步骤:
- 从第一个 XPS 文件创建 XpsDocument 实例。
- 创建一个将与第一个 XPS 文件合并的 XPS 文件数组。
- 指定 PdfSaveOptions 的 TextCompression、ImageCompression、JpegQualityLevel 和其他选项。
- 将 XPS 文件与创建的文档合并,并使用 PDF 保存选项将其保存为 PDF。
以下代码片段展示了如何在 Java 中将 XPS 文件合并为 PDF 文档:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir();
4
5// Load the first XPS file in a document
6XpsDocument document = new XpsDocument(dataDir + "input.xps");
7
8// Create an array of XPS files that will be merged with the first one
9String[] filesForMerge = new String[] { dataDir + "Demo.xps", dataDir + "sample.xps" };
10
11// Initialize options object with necessary parameters.
12com.aspose.xps.rendering.PdfSaveOptions options = new com.aspose.xps.rendering.PdfSaveOptions();
13options.setJpegQualityLevel(100);
14options.setImageCompression(com.aspose.xps.rendering.PdfImageCompression.Jpeg);
15options.setTextCompression(com.aspose.xps.rendering.PdfTextCompression.Flate);
16options.setPageNumbers(new int[] { 1, 2, 6 });
17
18document.mergeToPdf(filesForMerge, dataDir + "XPStoPDF.pdf", options);
我们来看一下
PdfSaveOptions。使用这个类,我们可以在将 XPS 合并为 PDF 时指定不同的转换参数。
- 如果 ImageCompression 算法为 JPEG,JpegQualityLevel 可控制 PDF 文档中图像的质量,取值范围为 0 到 100。
- ImageCompression 算法封装在 PdfImageCompression 枚举中,可以是运行长度编码 (RLE)、Flate、带有基本或优化预测器的 Lempel-Ziv-Welch (LZW)、JPEG、none(原始图像字节)和 auto(针对每幅图像采用最合适的压缩方式)。默认为自动压缩。
- TextCompression 算法封装在 PdfTextCompression 枚举中,可以是运行长度编码 (RLE)、Flate、Lempel-Ziv-Welch (LZW) 或 None。XPS 到 PDF 合并的默认值为 Flate。
- EncryptionDetails 封装在 PdfEncryptionDetails 中。它允许设置 PDF 文档的加密算法、权限、所有者和用户密码。
- OutlineTreeExpansionLevel 和 OutlineTreeHeight 控制文档大纲的视图。