Hey, friends! When dealing with Word documents in Java development, Docx4j is a superb library that allows us to easily create, read, modify and convert Word documents. Let’s talk in detail about how to use Docx4j to process Word documents.
1. Introduce dependencies
If you use Maven to manage your project, add these dependencies to it:
<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> </dependencies>
2. Create a simple Word document
Here is a Java code example to create a simple Word document:
import org.docx4j.Docx4J; import org.; import org.; import org.; import org.; import org.; import ; import ; import ; public class CreateWordDocument { public static void main(String[] args) { try { // Create a WordprocessingMLPackage object to represent a Word document WordprocessingMLPackage wordMLPackage = (); // Get the main document part of the document MainDocumentPart mainDocumentPart = (); // Create a paragraph P paragraph = ("This is a simple Word document created with Docx4j."); // Save the document OutputStream os = new FileOutputStream(new File("")); (wordMLPackage, os, Docx4J.FLAG_NONE); (); ("Word document was created successfully!"); } catch (Exception e) { (); ("Word document creation failed:" + ()); } } }
Code explanation
WordprocessingMLPackage wordMLPackage = ();: Creates a WordprocessingMLPackage object that represents the entire Word document.
MainDocumentPart mainDocumentPart = ();: Get the main document part of the document, and subsequent operations are mainly carried out in this part.
P paragraph = ("This is a simple Word document created with Docx4j.");: Create a paragraph and add text content.
OutputStream os = new FileOutputStream(new File("")); and (wordMLPackage, os, Docx4J.FLAG_NONE);: Save the document as a file.
3. Read Word document content
Here is a sample code for reading the contents of Word documents:
import org.docx4j.Docx4J; import org.; import org.; import org.; import org.; import ; import ; public class ReadWordDocument { public static void main(String[] args) { try { // Load Word documents WordprocessingMLPackage wordMLPackage = (new File("")); // Get the main document part MainDocumentPart mainDocumentPart = (); // Get all paragraphs List<Object> paragraphs = (); for (Object paraObj : paragraphs) { if (paraObj instanceof P) { P paragraph = (P) paraObj; List<Object> runs = (); for (Object runObj : runs) { if (runObj instanceof Text) { Text text = (Text) runObj; (()); } } (); } } } catch (Exception e) { (); ("Word document reading failed:" + ()); } } }
Code explanation
WordprocessingMLPackage wordMLPackage = (new File(""));: Load the file.
By traversing the contents of the main document section, find all paragraphs and text, and print out the text content.
4. Replace text in Word documents
Here is a sample code to replace the specified text in a Word document:
import org.docx4j.Docx4J; import org.; import org.; import org.; import org.; import ; import ; import ; import ; public class ReplaceTextInWord { public static void main(String[] args) { try { // Load Word documents WordprocessingMLPackage wordMLPackage = (new File("")); // Get the main document part MainDocumentPart mainDocumentPart = (); // Text to be replaced and text after replacement String oldText = "Old text"; String newText = "New Text"; // Get all paragraphs List<Object> paragraphs = (); for (Object paraObj : paragraphs) { if (paraObj instanceof P) { P paragraph = (P) paraObj; List<Object> runs = (); for (Object runObj : runs) { if (runObj instanceof Text) { Text text = (Text) runObj; String value = (); if (value != null && (oldText)) { ((oldText, newText)); } } } } } // Save the modified document OutputStream os = new FileOutputStream(new File("output_replace.docx")); (wordMLPackage, os, Docx4J.FLAG_NONE); (); ("Word document text replacement succeeded!"); } catch (Exception e) { (); ("Word document text replacement failed:" + ()); } } }
Code explanation
Load the file first, then iterate through all paragraphs and text in the document, find the place containing the specified old text, and replace it with the new text.
Finally, save the modified document as the output_replace.docx file.
Hey, friends! With these sample codes, we can easily process Word documents with Docx4j. Try it now and let your Java programs show off their skills in Word document processing!
This is the end of this article about Java calling Docx4j library to play Word document processing. For more related Java Docx4j processing Word content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!