View Word as PDF using Java

Increasing reliance on technology in today’s world demands the ability to view documents as PDFs as a fundamental skill. PDF is a widely accepted standard, so it’s often necessary to convert Word documents into this portable format when sharing. This article will discuss how to programmatically render Word documents as PDF in Java. Additionally, we will see how to create secure PDFs and disable the printing option using Java.

The following topics are discussed in this article:

Word & PDF Viewer – Java API

GroupDocs.Viewer empowers developers with its APIs to create powerful document viewer applications. The API enables the rendering of documents into PDF, HTML, and image formats within the application. In this article, we will use its Java API to view Word documents as PDFs.

Download or Configure

You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pom.xml of your maven-based Java applications.

<repository>
    <id>groupdocs-artifacts-repository</id>
    <name>GroupDocs Artifacts Repository</name>
    <url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-viewer</artifactId>
    <version>22.12</version>
</dependency>

How to View Word Documents as PDFs in Java

The following steps outline how to easily render a Word document to a PDF using a few lines of Java code:

  • Utilize the Viewer class to load the Word document.
  • Set up the default PDF View Options.
  • Use the view method to convert the loaded Word document to a PDF.

Here is an example of the Java code that can be used to perform this conversion:

This is what the above code produces as a result:

Word as PDF

Creating Secure PDF with Disabled Printing Option from Word Document in Java

Here, you will find the answers to the following questions in just a few lines of code:

  1. How to convert a Word Document into a password-protected PDF in Java?
  2. How to create a PDF with disabled printing options in Java?

These steps will guide you on how to convert a Word document into a protected PDF with limited permissions in Java:

  • Set up the security settings using the Security class.
    • Specify the document opening password to restrict access to the document.
    • Define the permission password to restrict PDF printing, modification, and data extraction.
    • Set the permissions to allow or deny permissions.
  • Set the PDF View Options and add the security settings to it.
  • Load the Word (DOC/DOCX) document using the Viewer class.
  • Use the view method to render the loaded Word document as a password-protected PDF with limited permissions.

The following Java code demonstrates how to convert a Word document into a protected PDF with no printing permissions:

PDF Secured with Password

When the PDF is opened, it will now prompt the user to enter a password before accessing the document.

Enter Password for Secured PDF

The output PDF will have limited permissions as set during the conversion process. As a result, the user will not be able to print the PDF due to the disabled printing option.

Word as Protected PDF

Obtain a Free Trial License

You can obtain a free temporary license to test the library without any limitations.

Summary

In summary, we have discussed how to programmatically view and convert Word documents into PDFs using Java, including the ability to password-protect the resulting PDF. We also learned how to restrict PDF file printing by setting its permissions. With this information, you can now easily create your own Word and PDF viewer application similar to the GroupDocs.Viewer Online App.

To gain a deeper understanding of GroupDocs.Viewer for Java, refer to its documentation. If you have any questions or concerns, feel free to reach out to us through the forum for assistance.

See Also