Java Program to Write a Paragraph in a Word Document
Last Updated :
02 Dec, 2020
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:
Similar Reads
Java Program to Extract Paragraphs From a Word Document
The article demonstrates how to extract paragraphs from a word document using the getParagraphs() method of XWPFDocument class provided by the Apache POI package. Apache POI is a project developed and maintained by Apache Software Foundation that provides libraries to perform numerous operations on
2 min read
Java Program to Create a Word Document
A Word file is supposed to be created without Word with the help of Java application protocol interfaces. Concepts Involved: Apache POI and Maven Apache POI Is an API provided by Apache foundation which is a collection of different java libraries. This facility gives the library to read, write, and
5 min read
Java Program to Add Tables to a Word Document
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision. It has C++, Python, Java, and MATLAB int
3 min read
Java Program to Write into a File
FileWriter class in Java is used to write character-oriented data to a file as this class is character-oriented because it is used in file handling in Java. There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows: Using writeString
6 min read
Java Program to Create a Blank PPT Document
Program to create a blank PPT document using Java. The external jar(Java archive) file is required for the creation of a new PPT document. Below is the implementation for the same. An object of XMLSlideShow class inside External Apache POI module is required for the creation of a new PPT. Algorithm:
1 min read
Adding Paragraphs as Text to a PDF using Java
iText is a Java library developed, to access and manipulate PDF files, that is to extract and modify the PDF content. Java allows us to incorporate various fully developed packages and modules in order to work with PDF files. We will see how to create a PDF document and add a paragraph to it using t
4 min read
Adding Pages to a PDF Document using Java
PDDocument class of 'org.apache.pdfbox.pdmodel' package which extends 'java.lang.Object'. is used. Declaration: public class PDDocument extends Object implements Pageable, Closeable Pre-requisite: Constructors PDDocument(): This constructor used to construct a new PDF document with zero pages.PDDocu
4 min read
Java Program to Extract Content from a TXT document
Java class< file using the Apache Tika library is used. Â For document type detection and content extraction from various file formats, it uses various document parsers and document type detection techniques to detect and extract data. It provides a single generic API for parsing different file fo
3 min read
How to Apply Borders to the Text in a Word Document using Java?
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
3 min read
Drawing a Line in a PDF Document using Java
In this article, we will learn how to Draw a line in a PDF document using Java. For drawing a line in a PDF, we will use the iText library. These are the steps that should be followed to Draw a line in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents the DocWriter for
3 min read