How to read and edit Word document in Java
Syncfusion® Java Word library (Essential® DocIO) is used to create, read and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can read and edit Word document in Java application.
Steps to read and edit Word document programmatically in Java
- Create a new ReadAndEdit.java file and include the following namespaces
JAVA
import com.syncfusion.docio.*;
- Use the following code sample to read and edit Word document.
JAVA
//Opens the Word template document WordDocument document = new WordDocument(); //Opens the template Word document. String basePath = getDataDir("Template.docx"); document.open(basePath, FormatType.Docx); //Gets the last paragraph IWParagraph paragraph = document.getLastParagraph(); //Gets the text range and modifies its content. WTextRange textRange=(WTextRange) paragraph.getItems().get(0); textRange.setText("Syncfusion"); //Saves the Word document document.save("Result.docx"); //Closes the document document.close();
A complete working example of how to read and edit Word document in Java can be downloaded from GitHub.
By executing the program, you will get the Word document as follows.
You can refer to here to know about how to run the Syncfusion® Java Word library (Essential® DocIO).
Take a moment to peruse the documentation. You can find basic the Word document processing options along with features like mail merge and merge and split documents.
Explore more about the rich set of Syncfusion® Word Framework features.
A Github example to create or generate a Word document with basic elements.