Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Difficulty Level : Easy
  • Last Updated : 13 Jan, 2021

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.

Approach:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  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.

Implementation:

  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:



Example

Java




// Java Program to format the text
// in a word document
  
// Importing input output java files
import java.io.*;
// 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();
  
        // Formating line1 by setting  bold
        line1.setBold(true);
        line1.setText("Formatted with Bold");
        line1.addBreak();
  
        // Line 2
        // Creating object for line 2
        XWPFRun line2 = paragraph.createRun();
  
        // Formating line1 by setting italic
        line2.setText("Formatted with Italics");
        line2.setItalic(true);
        line2.addBreak();
  
        // Line 3
        // Creating object for line 3
        XWPFRun line3 = paragraph.createRun();
  
        // Formatting line3 by setting
        // color & font size
        line3.setColor("73fc03");
        line3.setFontSize(20);
        line3.setText(" Formatted with Color");
  
        // Step 6: Saving changes to document
        document.write(out);
  
        // Step 7: Closing the connections
        out.close();
        document.close();
  
        // Print message after program has compiled
        // successfully showcasing formatting text in file
        // successfully.
        System.out.println(
            "Word Document with Formatted Text created successfully!");
    }
}

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

Output




My Personal Notes arrow_drop_up
Recommended Articles
Page :