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
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 {
public static void main(String[] args) throws Exception
{
XWPFDocument xwpfdocument = new XWPFDocument();
File file = new File( "C:/addParagraph.docx" );
FileOutputStream ostream
= new FileOutputStream(file);
XWPFParagraph para = xwpfdocument.createParagraph();
XWPFRun xwpfrun = para.createRun();
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. " );
xwpfdocument.write(ostream);
ostream.close();
}
}
|
Output: The program produces the following file in the local directory:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Dec, 2020
Like Article
Save Article