Convert PDF to XPS using Java REST API

This short guide explains how to convert PDF to XPS using Java REST API. You will learn to automatically transform PDF to XPS using Java Low Code API with a Java-based Cloud SDK. It will also share details to relieve the output XPS file.

Prerequisite

Steps to Convert PDF to XPS Document using Java REST Interface

  1. Create the PdfApi object to change PDF to XPS
  2. Read the source PDF file contents into a byte array
  3. Convert the byte array to a MemoryStream for uploading
  4. Upload the MemoryStream to the Cloud storage containing a PDF file
  5. Call the GetPdfInStorageToXps() to transform PDF to XPS
  6. Parse the API response and fetch the XPS contents for saving on the disk

The above steps describe the process of converting the PDF of XPS using Java RESTful Service. Load the source PDF file into a byte array and create a memory stream for loading it to the Could storage. Finally, call the GetPdfInStorageToXps() method to change the online PDF file to XPS and return the converted XPS stream.

Code for PDF to XPS Converter Software using Java Java-based API

import com.aspose.pdf.cloud.ApiClient;
import com.aspose.pdf.cloud.api.PdfApi;
import com.aspose.pdf.cloud.model.FilesUploadResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
public class PdfTasks {
public static void convertPdfToXps() {
// Initialize API with credentials
String clientId = "ID";
String clientSecret = "Secret";
ApiClient apiClient = new ApiClient(clientId, clientSecret);
PdfApi pdfApi = new PdfApi(apiClient);
String documentName = "TextAndImages.pdf";
String outputFileName = "output.xps";
try {
// Upload the PDF file to cloud storage
File inputFile = new File(documentName);
FilesUploadResult uploadResult = pdfApi.uploadFile(documentName, inputFile, null);
// Convert PDF to XPS
InputStream xpsStream = pdfApi.getPdfInStorageToXps(documentName, null, null);
// Save the result locally
try (FileOutputStream outputStream = new FileOutputStream(outputFileName)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = xpsStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
System.out.println("PDF successfully converted to XPS and saved as: " + outputFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
}

This code demonstrates the conversion of PDF to XPS file using Java REST API. You may upload multiple PDF files to the Cloud storage and call the GetPdfInStorageToXps() method individually for each PDF and convert it to an XPS file. You may also customize the uploaded PDF file before converting it to the XPS format if required.

This guide has taught us the development of a PDF to XPS file converter using Java Low Code API. To transform a PCL file to PDF, refer to the article Convert PCL file to PDF using Java REST API.

 English