How to Apply Borders to the Text in a Word Document using Java?
Java Library Apache POI will be used to apply borders to text in a Word Document in 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. Use Apache guide to install the Apache POI libraries for Windows/Linux Systems.
- Create an empty Document Object using XWPFDocument from the 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.
- Apply Borders to the Paragraph.
- Create Lines of Text using XWPFRun Object in the paragraph.
Illustration: Sample Input Image
- Step 1: Create a blank document
- Step 2: Get a path of the current working directory to create the PDF file in the same directory of the running program.
- Step 3: Create a file object with the path specified.
- Step 4: Create a paragraph.
- Step 5: Set the borders.
- Step 6: Save changes to the document.
- Step 7: Close the connections.
Word Document with Border Text created successfully!
Text is inserted in the same Word document and the border is successfully applied to the same text in a Word while comparing output image with sample input image taken as illustration to show changes to it after the implementation.