Merge XPS documents to XPS and PDF

.NET API solution to combine several XPS files

 

XPS can hold multiple documents in one file as well as XPS documents may have multiple pages included. Merging functionality offered by Aspose.Page API solution is developed so you could combine a few files into one. As a result you will get an XPS or PDF file that contains the content of all the merged files. Here you will find the code example on how to Merge XPS with C# .NET. To see how could such functionality be implemented into web solution or to merge files online try out XPS Merger .

To merge XPS files, we need:

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

  • Open the NuGet package manager, and search for Aspose.Page 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 several XPS files to a single one with C# .NET.

  1. Set the path to the documents directory.
  2. Initialize the XPS output stream.
  3. Initialize the XPS file input stream.
  4. Load the XPS document from the stream XpsDocument Class or load it directly from a file. In this case, you do not need xpsStream.
  5. Create an array of XPS files that will be merged with the first one.
  6. Merge XPS files to output XPS document.

C# Code to merge XPS to XPS

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

Steps to merge several XPS to PDF with C# .NET.

  1. Set the path to the documents directory.
  2. Initialize the PDF output stream.
  3. Initialize the XPS file input stream.
  4. Load XPS document from the stream XpsDocument Class or load it directly from a file. In this case, you do not need xpsStream.
  5. Initialize options object with the necessary parameters for the output file by means of the PdfSaveOptions Class.
  6. Using the PdfDevice Class, create a rendering device for PDF format.
  7. Create an array of XPS files that will be merged with the first one.
  8. Merge XPS files to output XPS document.

C# to merge XPS to 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);
    }



FAQ

1. How to Merge XPS files?

Set the path to the documents directory and initialize output and input streams. Load XPS files from a stream or files and merge them. If you would better merge XPS files online use our free cross-platform XPS Merger .

2. What output formats are supported?

By now Aspose.Page Solution allows you to merge XPS files into a single XPS file or a PDF one.

3. Is the functionality free?

The cross-platform Mergers are free, when for the API solution you can get a free Trial and then buy the product if needed.

XPS What is XPS File Format

XPS format is similar to PDF format. Both are page description language (PDL) formats. EPS is based on HTML and not on PostScript language. The .eps file is capable to contain a markup of the document's structure along with the information on how the document would look like. There are also added instructions on how to print and render the document. The feature of the format is that it fixes the document's description which means that it will look the same no matter who and from what operational system opens it.