How to convert word to PDF in Java
Tags: wordtopdf, freejavaapi, java
Published: 2019-04-22 08:48:29

---
title: How to convert word to PDF in Java
published: true
description: Java converting word to pdf
tags: word to pdf, Free Java API, Java
---

Converting word to PDF is the most popular requirements for processing word documents. With the help of free Spire.Doc for Java, conversion from word to PDF becomes very simple and easy in Java applications. Many word elements can be supported to convert to PDF, such as hyperlinks, table, header and footer, bookmarks, the hidden fonts and paragraphs, etc. I will introduce how to convert word to PDF in the following two aspects:

•All-in-one solution to convert Word to PDF
•Convert word to PDF by customized option

Firstly, view the sample Microsoft word document as below:
![Sample word](https://thepracticaldev.s3.amazonaws.com/i/zqujdljvb3f3ts0ovp7k.png)

It is very simple to convert the word to PDF with default options. It only needs three lines of codes.

```
import com.spire.doc.*;
public class WordtoPDF {
    public static void main(String[] args) {
        Document document = new Document();
        document.loadFromFile("Sample.docx");

        document.saveToFile("out/result.pdf", FileFormat.PDF);
    }
}
```
After debugging, we will get the result PDF page as below:
![word to PDF](https://thepracticaldev.s3.amazonaws.com/i/0mq6wt3n9o89b7kpeg3s.png)

From the above result PDF page, we found that the hidden texts and bookmarks were not shown on the resulted PDF page. If you want to show all these word elements, Spire.Doc for Java all support it. Now here comes to the codes of converting word to PDF by customized options.


```
import com.spire.doc.*;
import com.spire.doc.documents.rendering.*;
import java.awt.*;

public class WordtoPDF {
    public static void main(String[] args) {

        Document document = new Document();
        document.loadFromFile("Sample.docx");

        ToPdfParameterList toPdf = new ToPdfParameterList();

        //embedded all fonts
        toPdf.isEmbeddedAllFonts(true);

        //convert hidden fonts
        toPdf.isHidden(true);

        //disable the hyperlinks
        toPdf.setDisableLink(true);

        //keep bookmarks
        toPdf.setCreateWordBookmarks(true);
        toPdf.setWordBookmarksTitle("Bookmark");
        toPdf.setWordBookmarksColor(Color.GRAY);

        //the event of BookmarkLayout occurs when drawing a bookmark
        document.BookmarkLayout = new BookmarkLevelHandler() {
            @Override
            public void invoke(Object sender, BookmarkLevelEventArgs args) {
                document_BookmarkLayout(sender, args);
            }
        };

        //save the document to a PDF file.
        document.saveToFile("out/Result2.pdf", toPdf);
    }
    private static void document_BookmarkLayout(Object sender, BookmarkLevelEventArgs args) {

        if (args.getBookmarkLevel().getLevel() == 2) {
            args.getBookmarkLevel().setColor(Color.RED);
            args.getBookmarkLevel().setStyle(BookmarkTextStyle.Bold);
        } else if (args.getBookmarkLevel().getLevel() == 3) {
            args.getBookmarkLevel().setColor(Color.GRAY);
            args.getBookmarkLevel().setStyle(BookmarkTextStyle.Italic);
        } else {
            args.getBookmarkLevel().setColor(Color.GREEN);
            args.getBookmarkLevel().setStyle(BookmarkTextStyle.Regular);
        }
    }
}

```

The result PDF page with embedded fonts, hidden texts and bookmarks.

![word to PDF 2](https://thepracticaldev.s3.amazonaws.com/i/pp5sfang4vsgfrs91mjc.png)

Conclusion

It is quite easy for us to use Free Spire.Doc for Java to convert word to PDF in Java. Thanks for your reading and wish it helps.