Open In App

Java Program to Write a Paragraph in a Word Document

Improve
Improve
Like Article
Like
Save
Share
Report

Java provides us various packages in-built into the environment, which facilitate the ease of reading, writing, and modifying the documents. The package org.apache.poi.xwpf.usermodel provides us the various features of formatting and appending the content in word documents. There are various classes available in this package like XWPFDocument to create a new word document and XWPFParagraph to create and write new paragraphs into the corresponding created document. File class can be used to create a file at the specified path-name and FileOutputStream to create a file stream connection. 

Approaches: The following procedure is followed to add a paragraph in the document : 

1. XWPFDocument: A Java class to create and work with .docx files. Each time a blank .docx document is created. An object of this class is created, to begin with, the process, using new XWPFDocument() in Java. A file output stream is also parallel created to create and append contents of the document to a file at the local system. A stream connection is established by using FileOutputStream class. 

2. XWPFParagraph: A Java class to create paragraphs corresponding to the XWPFDocument created. Multiple paragraphs can be created in a single document, each of which is instantiated using the specified document. The following method is invoked using the created object of the XWPFDocument in Java.

Syntax: 

1. createParagraph()

xwpfdocument.createParagraph()

Return type: An object of the class XWPF Paragraph. 

2. createRun()

XWPFRun is a Java class to add a run to each of the paragraphs created in the document. XWPFRun simulates the addition of content to the paragraph using the createRun() method. The following method is invoked on the paragraph created in Java : 

xwpfparagraph.createRun()

Return type: An object of the class XWPF Run.

3. setText()

The setText() method is invoked over this created run object to add content in Java : 

xwpfrun.setText(content)

Arguments: The content in string form is accepted as an argument. 

Return type: Doesn’t return anything. 

Note: The content specified in the document is then written to the file stream connection using the stream connection object and appended by invoking write() method over the XWPFDocument object. And, then the connection is closed successively.

Implementation: Java Programming to Write a paragraph in a Word Document

Java




// Java Programming to Write a paragraph in a Word Document
  
// Importing required packages
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] args) throws Exception
    {
  
        // Create a blank document
        XWPFDocument xwpfdocument = new XWPFDocument();
  
        // Create a blank file at C:
        File file = new File("C:/addParagraph.docx");
  
        // Create a file output stream connection
        FileOutputStream ostream
            = new FileOutputStream(file);
  
        /* Create a new paragraph using the document */
  
        // CreateParagraph() method is used
        // to instantiate a new paragraph
        XWPFParagraph para = xwpfdocument.createParagraph();
  
        // CreateRun method appends a new run to the
        // paragraph created
        XWPFRun xwpfrun = para.createRun();
  
        // SetText sets the text to the run
        // created using XWPF run
        xwpfrun.setText(
            "Geeks for Geeks is a computer science portal which aims "
            + "to provide all in one platform for learning and "
            + "practicing.We can learn multiple programming languages here. ");
  
        // Write content set using XWPF classes available
        xwpfdocument.write(ostream);
  
        // Close connection
        ostream.close();
    }
}


Output: The program produces the following file in the local directory:



Last Updated : 02 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads