Convert Word Document to PDF in Java using REST API

Convert Word Document to PDF in Java using REST API

In the last blog post, we described how to programmatically convert PDF to Word. This blog post will show us how to use the Java library to convert Word documents to PDF without losing format. This library easily converts Word documents into PDF files programmatically in your Java applications. Such conversion is useful when you want to share documents, secure data, or ensure that a PDF viewer is available on any platform that has it. PDFs are easier to print, more portable, more secure, and better suited for long-term archiving than Word documents. Therefore, in this article, we are going to demonstrate how to convert Word document to PDF in Java using REST API.

The following topics are covered in this article:

Java Convert Word to PDF - DOCX to PDF Java library Installation

In order to convert Word DOC to PDF, I will be using Java SDK of GroupDocs.Conversion Cloud API. Word to PDF converter API supports a fast, and reliable file conversion in Java without installing any third-party software. It also supports conversion among all popular business document formats such as Excel, PDF, PowerPoint, HTML, Email, Word, Photoshop, CorelDraw, AutoCAD, raster image file formats, and many more. Further, it renders the whole document, or partially to speed up the file conversion process. Our Java API is compatible with all Java versions and supports all popular operating systems(Windows, Linux, macOS) capable of running Java runtime.

The installation method of this java library is straightforward. Download the JAR of the API or simply add the following pom.xml configuration in your Maven-based Java application to try the below-mentioned Java code snippets.

Maven Repository:

<repository>
    <id>groupdocs-artifact-repository</id>
    <name>GroupDocs Artifact Repository</name>
    <url>https://repository.groupdocs.cloud/repo</url>
</repository>

Maven Dependency:

<dependency>
    <groupId>com.groupdocs</groupId
    <artifactId>groupdocs-conversion-cloud</artifactId>
    <version>23.4</version>
    <scope>compile</scope>
</dependency>

Please obtain your Client ID and Client Secret from the dashboard before you start following the steps and available code snippets. Once you have your ID and secret, please add these in the code examples as mentioned below:

How to Convert Word File to PDF in Java using REST API

The simple steps listed below can be used to convert a Word file into a PDF file:

Upload the File

Firstly, upload the Word document to the cloud storage with the following code snippet:

Hence, the uploaded Word file will be available in the files section of your dashboard on the cloud.

Convert DOCX to PDF in Java

This section explains how to programmatically convert a Word document to a PDF file by using the steps listed below:

  • Firstly, create an instance of ConvertApi
  • Next, create an instance of the ConvertSettings
  • Then, put in your storage name.
  • Now, set the input Word file path and the output file format as “pdf”
  • Next, create an instance of the DocxLoadOptions
  • Now, set the password, loadOptions, and the output PDF file path
  • Next, create ConvertDocumentRequest with ConvertSettings
  • Finally, call the convert_document() method with ConvertDocumentRequest

The following sample code snippet shows how to convert DOCX to PDF online using REST API:

Download the Converted File

The above code sample will save the converted PDF file to the cloud. You can download it using the following code snippet:

This is how the PDF converter library in java works. In the next section, let’s explore more advanced conversion settings using Java API.

Convert Word into PDF Online in Java using Advanced Options

In this section, you can also convert Word document to PDF file using some advanced options programmatically by following the steps given below:

  • Firstly, create an instance of ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Thirdly, provide the cloud storage name
  • Set the input Word file path and output file format as “pdf”
  • Create an instance of the DocxLoadOptions
  • Now, set password and load option values
  • Now, create an instance of the PdfConvertOptions
  • Set various convert options like setCenterWindow, setFromPage, setPagesCount, setImageQuality, setPassword, setDpi, etc.
  • Provide convert options and set the output file path
  • Next, create ConvertDocumentRequest with ConvertSettings as a parameter
  • Lastly, call the conversion using the convert_document() class with ConvertDocumentRequest

The following code example shows how to convert Word file into PDF document using advanced settings:

Please follow the aforementioned steps to upload and download the files.

How to Save a Word Document as a PDF in Java using Pages Range Filter

In this section, you can also convert Word document to PDF file using some advanced settings programmatically by following the steps given below:

  • Firstly, create an instance of ConvertApi class
  • Secondly, create an instance of the ConvertSettings
  • Next, provide the cloud storage name
  • Set the input Word file path and output file format as “pdf”
  • Create an instance of the DocxLoadOptions
  • Now, set password and load options values
  • Now, create an instance of the PdfConvertOptions
  • Set various convertOptions like setFromPage, setPagesCount, etc.
  • Provide convert options and the output file path
  • Next, create ConvertDocumentRequest with ConvertSettings as a parameter
  • Finally, call conversion using the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert range of pages from Word file to PDF in Java:

Convert Specific Pages of MS Word to PDF Online in Java

This section explains how to programmatically convert Word DOC files to PDF files by using the steps listed below:

  • Firstly, create an instance of ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Next, provide the cloud storage name
  • Set the input Word file path and output file format as “pdf”
  • Next, create an instance of the DocxLoadOptions
  • Provide the password and load options values
  • Next, create an instance of the PdfConvertOptions
  • Now, set the pages collection array list with comma-separated values
  • Provide convert options and the output file path
  • Next, create ConvertDocumentRequest with ConvertSettings as a parameter
  • Finally, convert specific pages by calling the convert_document()

The following code example shows how to convert specific pages of Word DOCX to PDF file in Java:

Online Word to PDF Converter Free

How to convert Word to PDF file online free? Please try free Word DOCX to PDF converter to turn Word to PDF online, which was developed using the above API.

Conclusion

We are ending this article at this point with the hope that you have learned:

  • how to convert Word DOC to PDF format using Java library programmatically;
  • how to convert DOCX to PDF file using some advanced options in Java;
  • programmatically upload the Word file to the cloud and then download the converted PDF file from the cloud;
  • how to save a Word document as a PDF in Java using a page range filter;
  • convert specific pages of MS Word to PDF online in Java;
  • online DOCX to PDF converter tool;

Additionally, visit GroupDocs.Conversion file conversion APIs using the documentation. We also have an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Online Java SDK source code is freely available on Github. Please check and visit for Java Examples here.

Moreover, we advise you to refer to our Getting Started Guide.

Finally, groupdocs.cloud is writing new blog posts on various file format solutions using REST API. So, please get in touch for regular updates.

Ask a question

For any queries/discussions about Word to PDF Converter Java API, please feel free to contact us via the forum.

FAQs

How do I convert Word to PDF in Java?

Please follow this link to learn the Java code snippet for how to change Word file to PDF format quickly, and easily.

Can we convert Word to PDF in Java using REST API?

Create an instance of ConvertApi, set the values of the convert settings, and invoke the convertDocument method with ConvertDocumentRequest to convert Word to PDF in Java.

How to convert DOCX to PDF online for free?

DOCX to PDF converter online free allows you to convert Word to PDF free, quickly and easily. Once the online conversion of Word to PDF is completed, you can instantly download the converted PDF file instantly.

How do I online convert Word DOCX to PDF?

  • Open free DOCX to PDF converter online
  • Click inside the file drop area to upload a PDF file or drag & drop a PDF file.
  • Click on Convert Now button, free online DOCX to PDF converter will convert Word to PDF file online free.
  • Download link of the resultant PDF file will be available instantly after converting Word to PDF file free.

How to install Word to PDF Java library?

Download and install Java library to process, manipulate and convert Word to PDF file in Java programmatically.

How to convert Word to PDF offline in windows?

Please visit this link to download Word DOC to PDF converter offline for windows. This Word to PDF converter free download software can be used to import Word into PDF file in windows quickly, with a single click.

See Also

We recommend you visit the following articles to learn about: