Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Apply Borders to the Text in a Word Document using Java?

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 13 Jan, 2021
Improve Article
Save Article

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.



// Java Program to apply borders to the text
// in a Word document
// Importing inout output classes
// importing Apache POI environment packages
import org.apache.poi.xwpf.usermodel.*;
// Class-BorderText
public class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
        // Step 1: Creating a blank document
        XWPFDocument document = new XWPFDocument();
        // Step 2: Getting path of current working directory
        // to create the pdf file in the same directory of
        // the running java program
        String path = System.getProperty("user.dir");
        path += "/BorderText.docx";
        // Step 3: Creating a file object with the path specified
        FileOutputStream out
            = new FileOutputStream(new File(path));
        // Step 4: Create a paragraph
        XWPFParagraph paragraph
            = document.createParagraph();
        // Step 5: Setting borders
        // Set bottom border to paragraph
        // Set left border to paragraph
        // Set right border to paragraph
        // Set top border to paragraph
        XWPFRun line = paragraph.createRun();
            "Whether programming excites you or you feel stifled"
            + ", wondering how to prepare for interview questions"
            + " or how to ace data structures and algorithms"
            + ", GeeksforGeeks is a one-stop solution.");
        // Step 6: Saving changes to document
        // Step 7: Closing the connections
        // Display message on console to illustrate
        // successful execution of the program
            "Word Document with Border Text created successfully!");


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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!