将 XPS 文档合并为 PDF

合并多个 XPS 文件的 C++ API 解决方案

 

XPS 文件格式允许在单个文件中存储多个文档,并且还能够包含多个页面。 Aspose.Page API Solution for C++ 提供了将多个文件合并为一个文件的能力,从而生成包含所有原始文件组合内容的 XPS 或 PDF 文件。

您将在此处找到有关如何使用 API 合并 XPS 文件的代码示例。要探索如何将此功能集成到 Web 解决方案或用于在线文件合并,您可以尝试跨平台 XPS Merger 工具。

要合并 PS 和 EPS 文件,您需要:

  • Aspose.Page for C++ API 是一个功能丰富、功能强大且易于使用的 C++ 平台文档操作和转换 API。

  • 你可以直接下载它的最新版本,打开NuGet包管理器,搜索Aspose.Page.Cpp并安装。您也可以从包管理器控制台使用以下命令。

Package Manager Console Command


    PM> Install-Package Aspose.Page

使用 C++ 将多个 XPS 文件合并为 PDF 的步骤。

  1. 设置文档目录的路径。
  2. 初始化 PDF 输出和 XPS 文件输入流。
  3. 从先前创建的输入流创建 XpsDocument 的实例。
  4. 通过 PdfSaveOptions 使用输出文件的 TextCompression、ImageCompression、JpegQualityLevel 参数初始化选项对象.pdf_save_options) 类。
  5. 使用 PdfDevice 类,为 PDF 格式创建渲染设备。
  6. 合并 XPS 文件以输出 XPS 文档。

将 XPS 合并为 PDF 的 C++ 代码

    using Aspose::Page::XPS;
    using Aspose::Page::IO;
    using Aspose::Page::Presentation::Pdf;
    // Input file
    System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(RunExamples::outDir() + u"XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
    // Clearing resources under 'using' statement
    System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
    // ------------------------------------------
    try {
	    System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(RunExamples::dataDir() + u"input xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
	    // Clearing resources under 'using' statement
	    System::Details::DisposeGuard<1> __dispose_guard_0({ xpsStream });
	    // ------------------------------------------

	    try
	    {
		    // Load XPS document form the stream
		    System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
		    // or load XPS document directly from file. No xpsStream is needed then.
		    // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());

		    // Initialize options object with necessary parameters.
		    System::SharedPtr<Aspose::Page::Xps::Presentation::Pdf::PdfSaveOptions> options = [&] { auto tmp_0 = System::MakeObject<Aspose::Page::Xps::Presentation::Pdf::PdfSaveOptions>(); tmp_0->set_JpegQualityLevel(100); tmp_0->set_ImageCompression(Aspose::Page::Xps::Presentation::Pdf::PdfImageCompression::Jpeg); tmp_0->set_TextCompression(Aspose::Page::Xps::Presentation::Pdf::PdfTextCompression::Flate); tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 })); return tmp_0; }();

		    // Create rendering device for PDF format
		    System::SharedPtr<Aspose::Page::Xps::Presentation::Pdf::PdfDevice> device = System::MakeObject<Aspose::Page::Xps::Presentation::Pdf::PdfDevice>(pdfStream);

		    // Create an array of XPS files that will be merged with the first one
		    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});

		    document->Merge(filesForMerge, device, options);
	    }
	    catch (...)
	    {
		    __dispose_guard_0.SetCurrentException(std::current_exception());
	    }
    }
    catch (...)
    {
	    __dispose_guard_1.SetCurrentException(std::current_exception());
    }

使用 C++ 将多个 XPS 文件合并为一个文件的步骤。

  1. 初始化 XPS 输出和输入流。
  2. 创建要与第一个文件合并的 XPS 文件数组。
  3. 从先前创建的输入流创建 XpsDocument 实例。
  4. 合并 XPS 文件以输出 XPS 文档。

将 XPS 合并到 XPS 的 C++ 代码

    // Output stream
    System::SharedPtr<System::IO::Stream> outStream = System::IO::File::Open(RunExamples::outDir() + u"mergedXPSfiles.xps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
    // Clearing resources under 'using' statement
    System::Details::DisposeGuard<1> __dispose_guard_1({ outStream });
    // ------------------------------------------
    try {
	    System::SharedPtr<System::IO::Stream> inStream = System::IO::File::Open(RunExamples::dataDir() + u"input.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
	    // Clearing resources under 'using' statement
	    System::Details::DisposeGuard<1> __dispose_guard_0({ inStream });
	    // ------------------------------------------

	    try
	    {
		    // Load XPS document form the stream
		    System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
		    // or load XPS document directly from file. No xpsStream is needed then.
		    // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());

		    // Create an array of XPS files that will be merged with the first one
		    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});

		    document->Merge(filesForMerge, outStream);
	    }
	    catch (...)
	    {
		    __dispose_guard_0.SetCurrentException(std::current_exception());
	    }
    }
    catch (...)
    {
	    __dispose_guard_1.SetCurrentException(std::current_exception());
    }

XPS 什么是XPS文件格式

XPS 格式类似于 PDF 格式。两者都是页面描述语言 (PDL) 格式。 EPS 基于 HTML 而不是 PostScript 语言。 .eps 文件能够包含文档结构的标记以及有关文档外观的信息。还添加了有关如何打印和呈现文档的说明。该格式的特点是它修复了文档的描述,这意味着无论谁以及从哪个操作系统打开它,它看起来都是一样的。