Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Format the Text in a Word Document using Java?

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 17 Jan, 2022
Improve Article
Save Article

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.


  1. Create an empty Document Object using XWPFDocument from Apache POI package.
  2. Create a FileOutputStream Object to save the Word Document in the desired path/location in the system.
  3. Create a Paragraph using XWPFParagraph Object in the document.
  4. Create Lines of Text using XWPFRun Object and apply formatting attributes.


  1. Creating a blank document.
  2. Getting path of current working directory to create the PDF file in the same directory.
  3. Creating a file object with the path specified above.
  4. Create paragraph using createParagraph() method.
  5. Formatting of lines.
  6. Saving changes to document.
  7. Closing the connections.

Sample input Image:



// Java Program to format the text
// in a word document
// Importing input output java files
// importing Apache POI environment packages
import org.apache.poi.xwpf.usermodel.*;
// Class to format text in Word file
public class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
        // Step 1: Creating blank document
        XWPFDocument document = new XWPFDocument();
        // Step 2: Getting path of current working directory
        // to create the pdf file in the same directory
        String path = System.getProperty("user.dir");
        path += "/FormattedWord.docx";
        // Step 3: Creating a file object with the path
        // specified
        FileOutputStream out
            = new FileOutputStream(new File(path));
        // Step 4: Create paragraph
        // using createParagrapth() method
        XWPFParagraph paragraph
            = document.createParagraph();
        // Step 5: Formatting lines
        // Line 1
        // Creating object for line 1
        XWPFRun line1 = paragraph.createRun();
        // Formatting line1 by setting  bold
        line1.setText("Formatted with Bold");
        // Line 2
        // Creating object for line 2
        XWPFRun line2 = paragraph.createRun();
        // Formatting line1 by setting italic
        line2.setText("Formatted with Italics");
        // Line 3
        // Creating object for line 3
        XWPFRun line3 = paragraph.createRun();
        // Formatting line3 by setting
        // color & font size
        line3.setText(" Formatted with Color");
        // Step 6: Saving changes to document
        // Step 7: Closing the connections
        // Print message after program has compiled
        // successfully showcasing formatting text in file
        // successfully.
            "Word Document with Formatted Text created successfully!");


Output: changes made as per above program in the sample input image illustrated.




My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!