合并 XPS 文件 | .NET API 解决方案
您可以通过免费在线工具 XPS Merger
Aspose.Page .NET XPS Merge 允许使用 .NET 平台支持的任何语言(例如 C#、VB 和 J#)将 XPS 文件合并为 XPS 文档。
执行 XPS 合并需要执行以下几个步骤:
- 从第一个 XPS 文件创建 XpsDocument 实例。
- 创建一个将与第一个 XPS 文件合并的 XPS 文件数组。
- 将 XPS 文件与创建的文档合并并保存。
以下代码片段展示了如何在 C# 中合并 XPS 文件:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithDocumentMerging();
4// Load XPS document from XPS file
5XpsDocument document = new XpsDocument(dataDir + "input1.xps", new XpsLoadOptions());
6
7// Create an array of XPS files that will be merged with the first one
8string[] filesToMerge = new string[] { dataDir + "input2.xps", dataDir + "input3.xps" };
9
10// Merge XPS files to output XPS document
11document.Merge(filesToMerge, dataDir + "mergedXPSfiles.xps");
Aspose.Page .NET XPS 合并还允许将 XPS 文件合并为 PDF 文档。
执行 XPS 到 PDF 合并需要执行以下几个步骤:
- 从第一个 XPS 文件创建 XpsDocument 实例。
- 创建一个将与第一个 XPS 文件合并的 XPS 文件数组。
- 指定 PdfSaveOptions 的 TextCompression、ImageCompression、JpegQualityLevel 和其他选项。
- 将 XPS 文件与创建的文档合并,并使用 PDF 保存选项将其保存为 PDF。
以下代码片段展示了如何在 C# 中将 XPS 文件合并为 PDF 文档:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithDocumentMerging();
4// Load XPS document form the XPS file
5XpsDocument document = new XpsDocument(dataDir + "input1.xps", new XpsLoadOptions());
6
7// Initialize options object with necessary parameters.
8Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions()
9{
10 JpegQualityLevel = 100,
11 ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,
12 TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate
13};
14
15// Create an array of XPS files that will be merged with the first one
16string[] filesToMerge = new string[] { dataDir + "input2.xps", dataDir + "input3.xps" };
17
18// Merge XPS files to output PDF file
19document.MergeToPdf(dataDir + "mergedXPSfiles.pdf", filesToMerge, 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 控制文档大纲的视图。