SoFunction
Updated on 2025-03-03

Summary of several common methods of converting Word documents into PDF files in Java

1. Use Apache POI + iText

Apache POIis a popular Java library for handling Microsoft Office documents. You can use it to read Word documents, andiTextCan be used to generate PDF files. Combining these two libraries can implement Word to PDF conversion.

Sample code

import ;
import ;
import ;

import ;
import ;
import ;

public class WordToPdfConverter {
    public static void main(String[] args) {
        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream(new File("")));
            PdfOptions pdfOptions = ();
            
            OutputStream out = new FileOutputStream(new File(""));
            ().convert(document, pdfOptions, out);
            ();
        } catch (Exception e) {
            ();
        }
    }
}

2. Use for Java

for JavaIt is a powerful business library that supports conversion between multiple document formats, including from Word to PDF.

Sample code

import ;
import ;

import ;

public class WordToPdfConverter {
    public static void main(String[] args) {
        try {
            // Load Word document            Document doc = new Document("");
            // Save as PDF            ("", );
        } catch (Exception e) {
            ();
        }
    }
}

3. Use Docx4j

Docx4jIt is an open source Java library used to process Office Open XML files (.docx, .xlsx, etc.). It can be used to read and modify Word documents and convert them to PDF format.

Sample code

import org.docx4j.Docx4J;
import org..Docx4JException;
import org.;
import org.;
import org.;
import org.;

import ;
import ;

public class WordToPdfConverter {
    public static void main(String[] args) {
        try {
            InputStream wordInputStream = new FileInputStream(new File(""));
            WordprocessingMLPackage wordMLPackage = (wordInputStream);
            FOSettings foSettings = new PDFSettings();
            XSLFOTransformer transformer = new XSLFOTransformer(wordMLPackage, foSettings);
            (new FileOutputStream(new File("")));
        } catch (Exception e) {
            ();
        }
    }
}

4. Use JODConverter

JODConverteris a Java library for document conversion, which relies on OpenOffice or LibreOffice to handle document conversion. Although not a direct Java library, it provides good documentation conversion support.

Sample code

import ;
import ;
import ;
import ;

import .extension_office.LibreOfficeStandalone;

import ;

public class WordToPdfConverter {
    public static void main(String[] args) {
        try {
            // Start LibreOffice            ();

            // Create an OfficeManager instance            OfficeManager officeManager = new LocalOfficeManager();
            ();

            // Create a converter            DocumentConverter converter = new DocumentConverter(officeManager);

            // Convert document            (new File(""), new File(""));

            // Stop OfficeManager            ();
        } catch (Exception e) {
            ();
        }
    }
}

Things to note

  • Library compatibility: Ensure that the selected library is compatible with the Java environment and that the required dependencies are installed.
  • Performance considerations: Some libraries may require the installation of additional software (such as LibreOffice), which can affect conversion speed and resource consumption.
  • license: Commercial libraries (such as) usually require the purchase of licenses, while open source libraries may have certain restrictions.

Summarize

This is the end of this article about several common methods of converting Word documents into PDF files in Java. For more related Java to convert Word documents into PDF content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!