This quick tutorial elaborates how to convert DOCX to PDF with Java REST API. DOCX is a file format used by Microsoft Word 2007 and later. It is an XML-based file format based on the Open XML standard and uses the .docx file extension. DOCX files are smaller than their predecessors (DOC files) and are designed to make sharing and collaboration easier. They can contain text, images, tables, charts, and other data.
Whereas, A PDF (Portable Document Format) file is a type of document created by Adobe that preserves the formatting of a document regardless of the application or operating system used to create it. PDF files can be viewed, printed, and shared easily, and they can also be password protected and digitally signed. If you require DOCX to PDF Conversion in Java Low Code API then the same can be performed with the help of this sample code.
Prerequisite
- Create account and get API credentials
- Download Aspose.Words Cloud SDK for Java
- Setup Java project with the above SDK
Steps to Convert DOCX to PDF in Java REST API
- Set Client ID and Client Secret for the API
- Create an instance of WordsAPI class with client credentials
- Specify input and output files
- Read input DOCX file and upload to cloud storage
- Create an object of WordsAPI with input and output file formats
- Call convertDocument method to Convert DOCX to PDF using REST API
- Save the output PDF file on local disk
Code for DOCX to PDF Conversion in Java Low Code API
String ClientID = Client.getID(); //replace Client.getID() with your own client ID here | |
String ClientSecret = Client.getSecret(); //replace3 Client.getSecret() with your own client secret here | |
String APIBaseUrl="https://api.aspose.cloud"; | |
String Local_Path = "C:/Temp/"; | |
try { | |
//Create API client with credentials | |
ApiClient apiClient = new ApiClient(ClientID, ClientSecret, APIBaseUrl); | |
//Create SDK object | |
WordsApi wordsApi = new WordsApi(apiClient); | |
String inputFile = "DOCXToPDF.docx"; | |
String outputFile = "DOCXToPDF.pdf"; | |
String outputFormat = "pdf"; | |
//Read input file to bytes array | |
byte[] inputFileData = Files.readAllBytes(Paths.get(Local_Path + inputFile).toAbsolutePath()); | |
//create conversion request object with input and output files | |
ConvertDocumentRequest convertDocumentRequest = new ConvertDocumentRequest(inputFileData, outputFormat, null, null, null, null, null, null, null); | |
//convert the input file to output format | |
byte[] outputFileData = wordsApi.convertDocument(convertDocumentRequest); | |
//save the output file from the bytes array | |
FileOutputStream fileOutputStream = new FileOutputStream(Local_Path + outputFile); | |
fileOutputStream.write(outputFileData); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} |
The code sample shown above empowers you to convert DOCX to PDF with Java REST API. You merely have to input DOCX file with the help of the Aspose.Words REST API SDK and download output PDF file to save it locally.
The above DOCX to PDF Conversion can be exploited with any no code or low code apps on Windows, Linux, or Mac.
A similar feature can be found in the following topic: How to Convert PDF to XLSX with Java REST API