
- Apache POI Word - Home
- Apache POI Word - Overview
- Apache POI Word - Installation
- Apache POI Word - Core Classes
- Apache POI Word - Document
- Apache POI Word - Paragraph
- Apache POI Word - Borders
- Apache POI Word - Tables
- Apache POI Word - Font & Alignment
- Apache POI Word - Text Extraction
- Apache POI Word Resources
- Apache POI Word - Quick Guide
- Apache POI Word - Useful Resources
- Apache POI Word - Discussion
Apache POI Word - Font & Alignment
This chapter shows how to apply different font styles and alignments in a Word document using Java. Generally, Font Style contains: Font size, Type, Bold, Italic, and Underline. And Alignment is categorized into left, center, right, and justify.
Example - Applying Font Styles in a Document
The following code is used to set different styles of font −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.VerticalAlign; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream(new File("example.docx")); //create paragraph XWPFParagraph paragraph = document.createParagraph(); //Set Bold an Italic XWPFRun paragraphOneRunOne = paragraph.createRun(); paragraphOneRunOne.setBold(true); paragraphOneRunOne.setItalic(true); paragraphOneRunOne.setText("Font Style"); paragraphOneRunOne.addBreak(); //Set text Position XWPFRun paragraphOneRunTwo = paragraph.createRun(); paragraphOneRunTwo.setText("Font Style two"); paragraphOneRunTwo.setTextPosition(100); //Set Strike through and Font Size and Subscript XWPFRun paragraphOneRunThree = paragraph.createRun(); paragraphOneRunThree.setStrike(true); paragraphOneRunThree.setFontSize(20); paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT); paragraphOneRunThree.setText(" Different Font Styles"); document.write(out); out.close(); document.close(); System.out.println("example.docx written successully"); } }
Output
It will generate a Word file named example.docx in your current directory and display the following output on the command prompt −
example.docx written successfully
The fontstyle.docx file looks as follows.

Example - Setting Alignment to Paragraph Text of a Document
The following code is used to set alignment to the paragraph text −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream( new File("example.docx")); //create paragraph XWPFParagraph paragraph = document.createParagraph(); //Set alignment paragraph to RIGHT paragraph.setAlignment(ParagraphAlignment.RIGHT); XWPFRun run = paragraph.createRun(); run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning " + "purpose in the domains of Academics, Information " + "Technology, Management and Computer Programming " + "Languages."); //Create Another paragraph paragraph = document.createParagraph(); //Set alignment paragraph to CENTER paragraph.setAlignment(ParagraphAlignment.CENTER); run = paragraph.createRun(); run.setText("The endeavour started by Mohtashim, an AMU " + "alumni, who is the founder and the managing director " + "of Tutorials Point (I) Pvt. Ltd. He came up with the " + "website tutorialspoint.com in year 2006 with the help" + "of handpicked freelancers, with an array of tutorials" + " for computer programming languages. "); document.write(out); out.close(); document.close(); System.out.println("example.docx written successfully"); } }
Output
It will generate a Word file named example.docx in your current directory and display the following output in the command prompt −
example.docx written successfully
The example.docx file looks as follows −

Advertisements