Java Program to Align the Text in a Word document
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.
- 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.
Output: The code creates a file in the local directory:
Please Login to comment...