Merge PS documents to PDF

C++ API solution to combine several PostScript files

 

PostScript file format can contain multiple pages, but it does not have the capability to combine multiple files into a single document like the XPS format. Aspose.Page API solution for C++ provides you with the functionality to merge several PS or EPS files into one PDF document.

The following code example demonstrates how to merge PostScript files using C++. If you need to learn how this functionality can be integrated into a web solution or to merge files online, you can try the cross-platform PS Merger tool.

To merge PS and EPS files, we need:

  • Aspose.Page for C++ API which is a feature-rich, powerful and easy to use document manipulation and conversion API for C++ platform.

  • You can download its latest version directly, just open NuGet package manager, and search for Aspose.Page.Cpp and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Steps to merge PostScript files with C++

  1. Initialize the PDF output and PostScript input streams.
  2. Create an array of PS files to merge.
  3. Use PdfSaveOptions Class to specify the necessary parameters. This class allows you setting additional parameters while merging.
  4. Create an instance of PdfDevice from created earlier output stream and specity the size and image format.
  5. Merge the files. To learn more about this functionality go to Aspose.Page Documentation .

C++ Code to merge PS to PDF

    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;
	    }
    }

Merge Encapsulated PostScript files with C++

To merge EPS to PDF you will need to take the same steps as for PS to PDF merging. To learn a more detailed code example go to Aspose.Page Documentation .

C++ Code to merge EPS to 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 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 What is PS File Format

PS format is one of the page description language (PDL) formats. It is capable to contain graphic as well as text information on the page. That is why the format was supported by most of the programs for image editing. The postscript file itself is a kind of instruction for printers. It contains information on what and how to print from its page.