将 XPS 文档合并为 XPS 和 PDF
用于组合多个 XPS 文件的 .NET API 解决方案
XPS 可以在一个文件中包含多个文档,而且 XPS 文档也可以包含多个页面。Aspose.Page API 解决方案提供的合并功能是为了让您可以将几个文件组合成一个文件而开发的。合并后,您将获得一个包含所有合并文件内容的 XPS 或 PDF 文件。在这里,您可以找到有关如何使用 C# .NET 合并 XPS 的代码示例。要查看如何在 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 文件输入流。
- 从流中加载 XPS 文档 XpsDocument 类 或直接从文件加载。在这种情况下,您不需要 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 文件输入流。
- 从流中加载 XPS 文档 XpsDocument 类或直接从文件加载。在这种情况下,您不需要 xpsStream。
- 通过 PdfSaveOptions 类,使用输出文件所需的参数初始化选项对象。
- 使用 PdfDevice 类,创建一个 PDF 格式的渲染设备。
- 创建一个将与第一个文件合并的 XPS 文件数组。
- 将 XPS 文件合并到输出 XPS 文档。
合并 XPS 为 PDF 的 C#
// 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(XML Paper Specification)是 Microsoft 的 PDF 替代方案,基于 XML/HTML,跨平台保持布局一致,且不依赖操作系统。