将 PS 文档合并为 PDF

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

 

PostScript 文件格式可以包含多个页面,但它不能像 XPS 格式那样将多个文件组合成一个文档。 Aspose.Page API C++ 解决方案为您提供了将多个 PS 或 EPS 文件合并为一个 PDF 文档的功能。

以下代码示例演示了如何使用 C++ 合并 PostScript 文件。如果您需要了解如何将此功能集成到 Web 解决方案或在线合并文件,您可以尝试跨平台 PS Merger 工具.

要合并 PS 和 EPS 文件,我们需要:

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

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

Package Manager Console Command


    PM> Install-Package Aspose.Page

使用 C++ 合并 PostScript 文件的步骤

  1. 初始化 PDF 输出和 PostScript 输入流。
  2. 创建一组要合并的 PS 文件。
  3. 使用 PdfSaveOptions 类指定必要的参数。此类允许您在合并时设置其他参数。
  4. 从先前创建的输出流创建 PdfDevice 实例,并指定大小和图像格式。
  5. 合并文件。要了解有关此功能的更多信息,请转至 Aspose.Page 文档

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

    using Aspose::Page::IO;
    using Aspose::Page::Presentation::Pdf;
    // Initialize PDF output stream
    System::SharedPtr<System::IO::FileStream> pdfStream = System::MakeObject<System::IO::FileStream>(outDir() + u"outputPDF_out.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
    
    // Initialize PostScript input stream
    System::SharedPtr<System::IO::FileStream> psStream = System::MakeObject<System::IO::FileStream>(dataDir() + u"input.ps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
    System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(psStream);

    // Create an array of PostScript files that will be merged with the first one
    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.ps", dataDir() + u"input3.ps"});
 
    //Initialize options object with necessary parameters.
    System::SharedPtr<PdfSaveOptions> options = System::MakeObject<PdfSaveOptions>(suppressErrors);
    // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
    options->set_AdditionalFontsFolders(System::MakeArray<System::String>({ u"{FONT_FOLDER}" }));

    // Default page size is 595x842 and it is not mandatory to set it in PdfDevice
    System::SharedPtr<Aspose::Page::EPS::Device::PdfDevice> device = System::MakeObject<Aspose::Page::EPS::Device::PdfDevice>(pdfStream);


    {
	    auto __finally_guard_0 = ::System::MakeScopeGuard([&psStream, &pdfStream]()
	    {
		    psStream->Close();
		    pdfStream->Close();
	    });

	    try
	    {
		    document->Merge(filesForMerge, device, options);
	    }
	    catch (...)
	    {
		    throw;
	    }
    }

使用 C++ 合并封装的 PostScript 文件

要将 EPS 合并到 PDF,您需要采取与 PS 到 PDF 合并相同的步骤。要了解更详细的代码示例,请转至 Aspose.Page 文档

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

    // Initialize PDF output stream
    System::SharedPtr<System::IO::FileStream> pdfStream = System::MakeObject<System::IO::FileStream>(outDir() + u"outputPDF_out.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
    
    // Initialize EPS input stream
    System::SharedPtr<System::IO::FileStream> psStream = System::MakeObject<System::IO::FileStream>(dataDir() + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
    System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(psStream);

    // Create an array of EPS files that will be merged with the first one
    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.eps", dataDir() + u"input3.eps"});
 
    //Initialize options object with necessary parameters.
    System::SharedPtr<PdfSaveOptions> options = System::MakeObject<PdfSaveOptions>(suppressErrors);
    // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
    options->set_AdditionalFontsFolders(System::MakeArray<System::String>({ u"{FONT_FOLDER}" }));

    // Default page size is 595x842 and it is not mandatory to set it in PdfDevice
    System::SharedPtr<Aspose::Page::EPS::Device::PdfDevice> device = System::MakeObject<Aspose::Page::EPS::Device::PdfDevice>(pdfStream);


    {
	    auto __finally_guard_0 = ::System::MakeScopeGuard([&psStream, &pdfStream]()
	    {
		    psStream->Close();
		    pdfStream->Close();
	    });

	    try
	    {
		    document->Merge(filesForMerge, device, options);
	    }
	    catch (...)
	    {
		    throw;
	    }
    }

PS 什么是PS文件格式

PS 格式是页面描述语言 (PDL) 格式之一。它能够在页面上包含图形和文本信息。这就是为什么大多数图像编辑程序都支持该格式的原因。 postscript 文件本身就是对打印机的一种指令。它包含有关从其页面打印什么以及如何打印的信息。