DEV Community


Posted on

Java/Convert PDF to XPS and Vice Versa

XPS (XML Paper Specification) is a fixed-layout document format designed to preserve document fidelity, providing device-independent document appearance. This article will introduce how to convert PDF to XPS and XPS to PDF in high quality using Free Spire.PDF for Java.

Import JAR Dependency

Method 1: You can download the free library and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Or you can directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

Enter fullscreen mode Exit fullscreen mode

Convert PDF to XPS

The PdfDocument.saveToFile() method offered by Free Spire.PDF for Java allows you to save the input PDF document to other file Formats. All you need to do is load the PDF document using PdfDocument.loadFromFile() method and then save it to .xps file format.

import com.spire.pdf.*;

public class PDFtoXPS {
    public static void main(String[] args) {
        //Load the PDF file
        PdfDocument pdf = new PdfDocument();

        //Save to XPS
        pdf.saveToFile("ToXPS.xps", FileFormat.XPS);
Enter fullscreen mode Exit fullscreen mode


Convert XPS to PDF

import com.spire.pdf.*;

public class XPStoPDF{
    public static void main(String[] args) {
        //Load XPS 
        PdfDocument pdf = new PdfDocument();

        //Save to PDF
        pdf.saveToFile("toPDF.pdf", FileFormat.PDF);
Enter fullscreen mode Exit fullscreen mode


Top comments (0)