将 XPS 文档合并为 XPS 和 PDF
.NET API 解决方案,用于组合多个 XPS 文件
XPS 可以在一个文件中保存多个文档,并且 XPS 文档可能包含多个页面。 Aspose.Page API 解决方案提供的合并功能已开发,因此您可以将几个文件合并为一个。结果,您将获得一个 XPS 或 PDF 文件,其中包含所有合并文件的内容。在这里,您将找到有关如何将 XPS 与 C# .NET 合并的代码示例。要了解如何将此类功能实现到 Web 解决方案或在线合并文件,请尝试 XPS Merger 。
要合并 XPS 文件,我们需要:
Aspose.Page for .NET API 是一个功能丰富、功能强大且易于使用的 C# 平台文档操作和转换 API。
打开 NuGet 包管理器,搜索 Aspose.Page 并安装。您也可以从包管理器控制台使用以下命令。
Package Manager Console Command
PM> Install-Package Aspose.Page
使用 C# .NET 将多个 XPS 文件合并为一个文件的步骤。
- 设置文档目录的路径。
- 初始化 XPS 输出流。
- 初始化 XPS 文件输入流。
- 从流 XpsDocument Class 加载 XPS 文档或直接从文件加载。在这种情况下,您不需要 xpsStream。
- 创建一组将与第一个文件合并的 XPS 文件。
- 合并 XPS 文件以输出 XPS 文档。
将 XPS 合并到 XPS 的 C# 代码
using Aspose.Page.XPS;
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentMerging();
// Load XPS document from XPS file
XpsDocument document = new XpsDocument(dataDir + "input.xps", new XpsLoadOptions());
// Create an array of XPS files that will be merged with the first one
string[] filesToMerge = new string[] { dataDir + "Demo.xps", dataDir + "sample.xps" };
// Merge XPS files to output XPS document
document.Merge(filesToMerge, dataDir + "mergedXPSfiles.xps");
}
使用 C# .NET 将多个 XPS 合并为 PDF 的步骤。
- 设置文档目录的路径。
- 初始化 PDF 输出流。
- 初始化 XPS 文件输入流。
- 从流 XpsDocument Class 加载 XPS 文档或直接从文件加载。在这种情况下,您不需要 xpsStream。
- 通过 PdfSaveOptions 类使用输出文件的必要参数初始化选项对象。
- 使用 PdfDevice 类,创建 PDF 格式的渲染设备。
- 创建一组将与第一个文件合并的 XPS 文件。
- 合并 XPS 文件以输出 XPS 文档。
C# 将 XPS 合并为 PDF
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentMerging();
// Initialize the PDF output stream
using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "mergedXPSfiles.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
// Initialize the XPS input stream
using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))
{
// Load the XPS document form the stream
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
// or load it directly from a file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with the necessary parameters.
Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions()
{
JpegQualityLevel = 100,
ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,
TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate
};
// Create a rendering device for the PDF format
Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);
// Create an array of XPS files that will be merged with the first one
string[] filesToMerge = new string[] { dataDir + "Demo.xps", dataDir + "sample.xps" };
// Merge XPS files to the output PDF document
document.Merge(filesToMerge, device, options);
}
常问问题
1. 如何合并 XPS 文件?
设置文档目录的路径并初始化输出和输入流。从流或文件加载 XPS 文件并合并它们。如果您想更好地在线合并 XPS 文件,请使用我们的免费跨平台 XPS Merger 。
2. 支持哪些输出格式?
到目前为止,Aspose.Page Solution 允许您将 XPS 文件合并为单个 XPS 文件或 PDF 文件。
3. 功能免费吗?
跨平台合并是免费的,对于 API 解决方案,您可以获得免费试用,然后根据需要购买产品。
XPS 什么是XPS文件格式
XPS 格式类似于 PDF 格式。两者都是页面描述语言 (PDL) 格式。 EPS 基于 HTML 而不是 PostScript 语言。 .eps 文件能够包含文档结构的标记以及有关文档外观的信息。还添加了有关如何打印和呈现文档的说明。该格式的特点是它修复了文档的描述,这意味着无论谁以及从哪个操作系统打开它,它看起来都是一样的。