Java: Convert PDF to XPS and XPS to PDF

PDF (Portable Document Format) and XPS (XML Paper Specification) are two commonly used document formats for sharing and printing documents. While PDF is widely known and supported, XPS is a Microsoft-developed format that has gained popularity due to its superior graphics rendering capabilities. In this article, we will demonstrate how to use Spire.PDF for Java to convert PDF to XPS and XPS to PDF in high quality.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.10.7</version>
    </dependency>
</dependencies>
    

Convert PDF to XPS in Java

Spire.PDF for Java has a powerful conversion feature, which can convert PDF to XPS in just three steps. The detailed steps are as follows:

  • Create a PdfDocument instance.
  • Load a PDF sample document using PdfDocument.loadFromFile() method.
  • Save the document as XPS using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

public class PDFtoXPS {
    public static void main(String[] args) {

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load the PDF file
       pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf.pdf");

        //Save to XPS
        pdf.saveToFile("ToXPS.xps", FileFormat.XPS);
        pdf.close();
    }
}

Java: Convert PDF to XPS and XPS to PDF

Convert XPS to PDF in Java

The PdfDocument.saveToFile() method provided by Spire.PDF for Java enables the conversion of a XPS file into a PDF document. The following are steps to convert XPS to PDF.

  • Create a PdfDocument instance.
  • Load a XPS file document using PdfDocument.loadFromFile() method.
  • Save the document as PDF using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

public class XPStoPDF {
    public static void main(String[] args) {

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a XPS file
        pdf.loadFromXPS("C:\\Users\\Administrator\\Desktop\\sample.xps");

        //Save to PDF
        pdf.saveToFile("toPDF.pdf", FileFormat.PDF);
        pdf.close();
    }
}

Java: Convert PDF to XPS and XPS to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.