Convert XPS to PDF using Java

Contents
[ ]

Aspose.Page Java XPS to PDF converter allows to convert XPS document to PDF document on any OS for which Java Virtual Machine exists.
It is necessary to do several steps in order to perform XPS to PDF conversion:

  1. Initialize an input stream for input XPS document.
  2. Initialize an output stream for output PDF document.
  3. Create an instance of XpsDocument from created earlier input stream.
  4. Specify TextCompressionImageCompressionJpegQualityLevel, and other options of PdfSaveOptions.
  5. Create an instance of PdfDevice from created earlier output stream.
  6. Save XPS document as PDF with PDF save options.


Following code snippet shows how to convert XPS to PDF document in Java:

// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize PDF output stream
FileOutputStream pdfStream = new FileOutputStream(dataDir + "XPStoPDF.pdf");

// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");

// Initialize options object with necessary parameters.
com.aspose.xps.rendering.PdfSaveOptions options = new com.aspose.xps.rendering.PdfSaveOptions();
options.setJpegQualityLevel(100);
options.setImageCompression(com.aspose.xps.rendering.PdfImageCompression.Jpeg);
options.setTextCompression(com.aspose.xps.rendering.PdfTextCompression.Flate);
options.setPageNumbers(new int[] { 1, 2, 6 });

// Create rendering device for PDF format
com.aspose.xps.rendering.PdfDevice device = new com.aspose.xps.rendering.PdfDevice(pdfStream);

document.save(device, options);

Let’s consider PdfSaveOptions. Using this class we can assign different conversion parameters while converting XPS to PDF.

  • JpegQualityLevel controls a quality of images in PDF document if ImageCompression algorithm is JPEG and can be from 0 to 100.
  • ImageCompression algorithm encapsulated in PdfImageCompression Enumeration, may be Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW) with base or optimized pedictor, JPEG , none (raw image bytes) and auto (the most appropriate compression for each image). Default is auto compression.
  • TextCompression algorithm encapsulated in PdfTextCompression Enumeration, may be Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW) and none. Default value in XPS to PDF conversion is Flate.
  • EncryptionDetails encapsulated in PdfEncryptionDetails. It allows setting encryption algorithm, permisions, owner and user passwords for PDF document.
  • PageNumbers represents an array of numbers of pages which will be saved to PDF.
  • OutlineTreeExpansionLevel and OutlineTreeHeight controls view of document outline.