DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
E-iceblue Product Family
E-iceblue Product Family

Posted on

How to convert word to PDF in 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

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

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

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.

Top comments (0)

Join the One Year Club

You can earn this badge by being a registered member of the DEV Community for at least one year. Create an account and get started today.