The text will be extracted first from a Word file using Apache in java. Apache POI let us allow access to Microsoft documents which includes Word, Excel, and PowerPoint. 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. The approach is simply first to create a paragraph, set alignment for it, and inserting text to it using inbuilt functions.
Algorithm:
- Import the jar files while creating a package for java program and add the jar files9if necessarily as per IDE’s) and import File class.
- Call for XWPFDocument by creating an object of it.
- Passing FileInputStream as a parameter to deal with local directories
- Inside it pass the pathname or name the file with extensions.
- Create a blank file and FileOutputStream connection.
- Creating a paragraph using createParagraph() method.
- Set alignment using setAllignment() in built function.
- Insert text using setText() function.
- Write the content of a paragraph using XWPF class
- Close the connection.
Implementation: A blank Word document is supposed to be input in which processing is carried out to display the output in the same Word document after the processing.
Java
// Java Program to Align the Text in a Word document // importing file libraries import java.io.File; import java.io.FileOutputStream; // Importing API packages 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:/alignParagraph.docx" ); // Create a file output stream connection FileOutputStream ostream = new FileOutputStream(file); // Creating new paragraph using the document // createParagraph() for instantiate new paragraph XWPFParagraph para = xwpfdocument.createParagraph(); // Set center alignment to paragraph in Java paragraph.setAlignment(ParagraphAlignment.CENTER); // createRun() method appends a new run to the // paragraph created XWPFRun xwpfrun = para.createRun(); /* Setting text to a paragraph */ // setText() method 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. It also provided content for" + "UGC NET and JRF exams." ); // Create another paragraph para = xwpfdocument.createParagraph(); // Set alignment of paragraph to right para.setAlignment(ParagraphAlignment.RIGHT); xwpfrun = para.createRun(); /* Set text to another paragraph */ xwpfrun.setText( "It also helps you to also prepare for various other " + "competitive exams.Also lets you prepare for interviews." ); // Write content set using XWPF classes available xwpfdocument.write(ostream); // Close connection ostream.close(); } } |
Output: The code creates a file in the local directory: