XPS Converter for .NET

Introduction to XPS Converter for .NET

The XPS Converter for .NET is a powerful library that allows developers to convert XPS (XML Paper Specification) files to various image formats, including PNG, JPEG, TIFF, and more. This guide will walk you through the available features of the library and provide code examples to help you get started.

Converting XPS to PDF

Converting an XPS file is a straightforward process that involves loading the XPS file and specifying the output format. The following code example demonstrates how to convert an XPS file to a PDF document:

XpsConverter converter = new XpsConverter();
XpsConverterToPdfOptions opt = new XpsConverterToPdfOptions();
opt.AddDataSource(new FileDataSource("input.xps"));
opt.AddSaveDataSource(new FileDataSource("output.pdf"));
converter.Process(opt);

In this example, we load the XPS file from a stream and create a new XpsConverter object. We then specify the output format as PDF and save the document to a new stream.

Converting XPS to Images

Converting an XPS file is a straightforward process that involves loading the XPS file and specifying the output format. The following code example demonstrates how to convert an XPS file to a PNG image:

XpsConverter converter = new XpsConverter();
PsConverterToImageOptions opt = new PsConverterToImageOptions();
opt.AddDataSource(new FileDataSource("input.xps"));
opt.AddSaveDataSource(new FileDataSource("output.png"));
converter.Process(opt);

In this example, we load the XPS file from a stream and create a new XpsConverter object. We then specify the output format as PNG and save the document to a new stream.

Supported Output Formats

The XPS Converter for .NET supports a wide range of output formats, including:

  • PNG (Portable Network Graphics)
  • JPEG (Joint Photographic Experts Group)
  • TIFF (Tagged Image File Format)
  • BMP (Bitmap)
  • GIF (Graphics Interchange Format)

Handling Errors and Exceptions

The XPS Converter for .NET throws exceptions if an error occurs during the conversion process. You can handle these exceptions using try-catch blocks.

try
{
    XpsConverter converter = new XpsConverter();
    XpsConverterToPdfOptions opt = new XpsConverterToPdfOptions();
    opt.AddDataSource(new FileDataSource("input.xps"));
    opt.AddSaveDataSource(new FileDataSource("output.pdf"));
    converter.Process(opt);
}
catch (NotSupportedException ex)
{
    Console.WriteLine($"Error converting file: {ex.Message}");
}

Note that the NotSupportedException class provides additional information about the error, such as the error code and message.

 English