How to Format the Text in a Word Document using Java?
Apache POI is a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project provides pure Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint, and Excel. First do install Apache in order to import modules as per the guide for the ecosystem based on operating systems such as Windows/Linux Systems. In order to format the text in a Word document using java, the basic requirement in importing libraries is Apache POI.
- Create an empty Document Object using XWPFDocument from Apache POI package.
- Create a FileOutputStream Object to save the Word Document in the desired path/location in the system.
- Create a Paragraph using XWPFParagraph Object in the document.
- Create Lines of Text using XWPFRun Object and apply formatting attributes.
- Creating a blank document.
- Getting path of current working directory to create the PDF file in the same directory.
- Creating a file object with the path specified above.
- Create paragraph using createParagraph() method.
- Formatting of lines.
- Saving changes to document.
- Closing the connections.
Sample input Image:
Output: changes made as per above program in the sample input image illustrated.