News Category

Java: Convert Word to PDF

2021-12-09 07:54:00 Written by  support iceblue
Rate this item
(0 votes)

Spire.Doc for Java provides the Document.saveToFile() method to convert Word to other document formats, such as PDF, XPS, SVG and HTML. This article will introduce how to convert Word to PDF using Spire.Doc for Java. Furthermore, the ToPdfParameterList class is introduced to control how a Word document will be converted to PDF. For example, whether to disable hyperlinks in the generated PDF document.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.3.1</version>
    </dependency>
</dependencies>
    

Convert Word to PDF

The following are detailed steps to convert Word to PDF:

  • Create a Document instance and load a sample Word document using Document.loadFromFile() method.
  • Create a ToPdfParameterList instance.
  • Embed all fonts in the PDF document using ToPdfParameterList.isEmbeddedAllFonts(boolean value) method, remove the hyperlinks and keep the character formats using ToPdfParameterList.setDisableLink(boolean value) method.
  • Set the quality of the JPEG image in PDF using Document.setJPEGQuality(int value) method.
  • Save the document as a PDF file using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;

public class WordToPDF {
    public static void main(String[] args) {
        //Create a Document instance
        Document doc = new Document(false);
         //Load a sample Word document
        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");

        //Create a ToPdfParameterList instance
        ToPdfParameterList ppl=new ToPdfParameterList();

        //Embed all fonts in the PDF document 
        ppl.isEmbeddedAllFonts(true);

        //Remove the hyperlinks and keep the character formats
        ppl.setDisableLink(true);

        //Set the output image quality as 40% of the original image. 80% is the default setting.
        doc.setJPEGQuality(40);

        //Save the document as PDF
        doc.saveToFile("output/ToPDF.pdf", ppl);
    }
}

Java: Convert Word to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 27 October 2023 06:06