Merge XPS Documents into PDF with Aspose.Page for .NET

Introduction

In the ever-evolving landscape of document processing, Aspose.Page for .NET emerges as a powerful tool for seamlessly merging XPS documents into PDF format. This tutorial will guide you through the process, breaking down each step to ensure a smooth and effective execution.

Prerequisites

Before diving into the tutorial, make sure you have the following prerequisites in place:

  • Aspose.Page for .NET: Ensure you have the Aspose.Page library installed. You can download it from here.

  • Document Files: Have the XPS document (input.xps) ready in your specified directory.

Import Namespaces

In your .NET project, include the necessary namespaces for working with Aspose.Page:

using Aspose.Page.XPS;

This step ensures that you have access to the classes and methods required for the document conversion.

Step 1: Initialize Streams

// ExStart:3
// The path to the documents directory.
string dataDir = "Your Document Directory";
// Initialize PDF output stream
using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF_out.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
// Initialize XPS input stream
using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))
{
    // ...
}
// ExEnd:3

This step involves setting up the input and output streams for the XPS and PDF files. Ensure the correct paths and file names are used.

Step 2: Load XPS Document

// ExStart:4
// Load XPS document form the stream
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// ExEnd:4

Here, we load the XPS document into the XpsDocument object, preparing it for further processing.

Step 3: Initialize Save Options

// ExStart:5
// Initialize options object with necessary parameters.
PdfSaveOptions options = new PdfSaveOptions()
{
    JpegQualityLevel = 100,
    ImageCompression = PdfImageCompression.Jpeg,
    TextCompression = PdfTextCompression.Flate,
    PageNumbers = new int[] { 1, 2, 6 }
};
// ExEnd:5

Customize the PdfSaveOptions object based on your preferences, specifying parameters such as image compression, text compression, and page numbers.

Step 4: Create Rendering Device

// ExStart:6
// Create rendering device for PDF format
PdfDevice device = new PdfDevice(pdfStream);
// ExEnd:6

The PdfDevice is the tool responsible for rendering the XPS document into PDF format.

Step 5: Save the Document

// ExStart:7
document.Save(device, options);
// ExEnd:7

Finally, save the document using the rendering device and the specified options.

Conclusion

Congratulations! You’ve successfully merged XPS documents into PDF using Aspose.Page for .NET. This seamless process ensures the preservation of document quality and formatting.

FAQ’s

Q1: Can I merge multiple XPS files into a single PDF?

A1: Yes, you can. Simply adjust the PageNumbers parameter in the PdfSaveOptions to include the desired pages from different XPS files.

Q2: Is a temporary license available for Aspose.Page for .NET?

A2: Yes, you can obtain a temporary license here for testing purposes.

Q3: Are there any limitations on file size when using Aspose.Page for document conversion?

A3: Aspose.Page for .NET does not impose strict limitations on file size, but optimal performance is achieved with reasonable file sizes.

Q4: Can I customize the output PDF further, such as adding watermarks or annotations?

A4: Yes, Aspose.Page for .NET provides extensive features for PDF manipulation. Check the documentation for advanced customization options.

Q5: Does Aspose.Page for .NET support cross-platform development?

A5: Yes, Aspose.Page for .NET is designed to work seamlessly across various platforms.