How to Convert DOC to PDF with Java REST API

This step by step tutorial elaborates how to convert DOC to PDF with Java REST API in the cloud. In order to export DOC to PDF format, we’re using Aspose.Words for Java Cloud SDK. If you would like DOC to PDF Conversion in Java Low Code API then this can be done by using the following sample code and simple steps.

Prerequisite

Steps to Convert DOC to PDF in Java REST API

  1. Set Client ID and Client Secret for the API
  2. Create an instance of WordsAPI class with client credentials
  3. Specify input and output files
  4. Read input DOC file and upload to cloud storage
  5. Create an object of WordsAPI with input and output file formats
  6. Call convertDocument method to Convert DOC to PDF using REST API
  7. Save the output PDF file on local disk

Code for DOC 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 = "DOCToPDF.doc";
String outputFile = "DOCToPDF.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 above sample code assists you to convert DOC to PDF with Java REST API. You simply need to supply DOC file with the help of the Aspose.Words REST API SDK for Java and download output PDF file to save it locally by using Aspose conversion API online.

The above DOC to PDF Conversion can be used with any no code or low code apps on any platform.

A similar feature can be found in the following topic: How to Convert DOCX to PDF with Java REST API

 English