How to Convert Word to PDF in Java

     PDF documents have strong stability and compatibility, and the typesetting effect is not affected by factors such as equipment and operating systems. And through watermarks, permission passwords, digital signatures and other means to limit and protect the content of the document. Therefore, the PDF format is almost everyone's first choice for transmission, printing and reading. What I want to introduce to you today is to use Java code to convert Word documents to PDF format. The following are the specific steps and codes I have compiled for your reference, I hope it will be helpful to you.

Programming Environment

First, 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.
method 1:
Introduced manually. Download Free Spire.Doc for Java locally, unzip it, and find the Spire.Doc.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

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>
            <url>https://repo.e-iceblue.com/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

Specific Steps:
  • 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.
Full Code:
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();

        //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);
    }
}
Effective Shot:


conclusion

In this post, you have learned how to remove Duplicate Values When Merging Cells in Word in Java. Not only that, we also have other
functions, such as,Java: Convert RTF to Word Doc/Docx and Vice Versa , Java: Convert XML to PDF and so on. Apart from that, if you'd like to learn more, you can visit this link

to explore more about for Spire.Doc for Java.







Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application