Apache POI: Edit and Edit a Word File with Java
In this chapter we will see how to add a paragraph to a Word file already created in java. After this chapter, you will be able to create and read a paragraph in Java. Before that, you need to download and import the necessary libraries to make it work (read the first part of the course How to create a Word document in java).
Before you begin, you need to download the Apache POI API.
Before you begin, you need to download the Apache POI API.
How to create a Word paragraph in Java?
First, we will study the classes responsible for creating and reading or writing a paragraph, you need to read the previous articlehow to create a Word document in Java so that you can create a paragraph.
XWPFParagraph is the class of the package. org.apache.poi.xwpf.usermodel and is used to create a paragraph in a Word document. This instance is also used to add all types of elements to a word.
The following are the methods of the XWPFParagraph:
1- createRun()
Concatenated a new paragraph to the previous one
2-getAlignment()
Returns the alignment of the paragraph that will be applied to the next text in that paragraph.
3-setAlignment(ParagraphAlignment align)
Specifies the alignment of the paragraph that will be applied to the next text in this paragraph.
4-setBorderBottom(Borders border)
Specifies the border that should be displayed below.
5-setBorderLeft(Borders border)
Specifies the border that should be displayed on the left.
6-setBorderRight(Borders border)
Specifies the border that should be displayed on the right.
7-setBorderTop(Borders border)
Specifies the border that should be displayed above.
The following code creates a paragraph in word:
//create an empty document
XWPFDocument document= new XWPFDocument();
//create a sheet with an empty paragraph
XWPFParagraph paragraph = document.createParagraph();
Add text
Now there is the text to insert. You can input the text using the XWPFRun of the package org.apache.poi.xwpf.usermodel. The instance XWPFParagraph allows you to retrieve the object XWPFRun:
XWPFRun run=paragraph.createRun();
Example:
import java.io.File;Output:
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 CreateParagraph
{
public static void main(String[] args)throws Exception
{
//create new document
XWPFDocument document= new XWPFDocument();
//create a write stream to save content in nouveaudoc.docx
FileOutputStream out = new FileOutputStream(
new File("nouveaudoc.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//create the run
XWPFR object run=paragraph.createRun();
//text to add
run.setText("In codeurjava.com, we are working to improve" +
"the quality of tutorials for self-learning" +
"in the java programming field");
//update the nouveaudoc.docx
document.write(out) file;
//close write stream
out.close();
System.out.println("the changes were made successfully");
}
}