Hey, friends! During development, you often encounter the need to convert Word documents into PDF format, such as generating reports, contracts, etc. There are many useful libraries in Java that can implement this function. Here are two common methods, using Apache POI and Docx4J combined with iText library to implement Word to PDF.
Method 1: Use Apache POI and iText
1. Introduce dependencies
If you use Maven to manage your project,Add the following dependencies:
<dependencies> <!-- Apache POI deal with Word document --> <dependency> <groupId></groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> <!-- iText generate PDF document --> <dependency> <groupId></groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.3</version> </dependency> </dependencies>
2. Code example
import .*; import ; import ; import ; import ; import .*; public class WordToPdfWithPOI { public static void main(String[] args) { try { // Read Word documents FileInputStream fis = new FileInputStream(""); XWPFDocument document = new XWPFDocument(fis); // Create PDF document Document pdfDoc = new Document(); (pdfDoc, new FileOutputStream("")); (); // traverse paragraphs of Word document for (XWPFParagraph paragraph : ()) { StringBuilder text = new StringBuilder(); // traverse the text running object in the paragraph for (XWPFRun run : ()) { ((0)); } // Add paragraph text to PDF document Paragraph pdfParagraph = new Paragraph(()); (pdfParagraph); } // Close documents and streams (); (); (); ("Word to PDF successfully!"); } catch (Exception e) { (); ("Word to PDF failed:" + ()); } } }
3. Code explanation
Read Word Documents:use
FileInputStream
ReadFiles, use them again
XWPFDocument
The class loads it into memory.Create PDF documents: Create
Document
Object representation PDF document, usePdfWriter
Associate the output stream, and then open the document and prepare to write content.Traversing Word document paragraphs: traverse each paragraph of the Word document, extract the text in the paragraph, and add it to
StringBuilder
, create againParagraph
Objects are added to PDF documents.Close documents and streams: After the operation is completed, close the PDF document, Word document, and input stream.
Method 2: Use Docx4J
1. Introduce dependencies
existAdd the following dependencies:
<dependencies> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-JAXB-Internal</artifactId> <version>11.4.9</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-JAXB-ReferenceImpl</artifactId> <version>11.4.9</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j</artifactId> <version>11.4.9</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-export-fo</artifactId> <version>11.4.9</version> </dependency> </dependencies>
2. Code example
import org.docx4j.Docx4J; import org.; import org.; import ; import ; import ; public class WordToPdfWithDocx4J { public static void main(String[] args) { try { // Load Word documents WordprocessingMLPackage wordMLPackage = (new File("")); // Create FOSettings object FOSettings foSettings = (); (wordMLPackage); // Create output stream OutputStream os = new FileOutputStream(new File("")); // Convert and save as PDF (foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); // Turn off the output stream (); ("Word to PDF successfully!"); } catch (Exception e) { (); ("Word to PDF failed:" + ()); } } }
3. Code explanation
Loading Word Documents:use
Method loading
document.
Create FOSettings object:
FOSettings
Use to configure conversion settings and set the loaded Word document in.Create an output stream: Create
FileOutputStream
Used to output PDF files.Convert and save as PDF: Call
The method performs conversion and saves the result to the output stream.
Turn off the output stream: After the operation is completed, close the output stream.
Hey, friends! Both methods can help you convert Word documents into PDFs in Java. You can choose the right method according to your needs and preferences. Try it now and make your program easy to convert documents!
This is the article about Java's example of converting Word documents into PDF with one click. For more related Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!