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.

Font Style

The following code is used to set different styles of font −

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3.  
  4. import org.apache.poi.xwpf.usermodel.VerticalAlign;
  5. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  6. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  7. import org.apache.poi.xwpf.usermodel.XWPFRun;
  8.  
  9. public class FontStyle {
  10.  
  11. public static void main(String[] args)throws Exception {
  12.  
  13. //Blank Document
  14. XWPFDocument document = new XWPFDocument();
  15.  
  16. //Write the Document in file system
  17. FileOutputStream out = new FileOutputStream(new File(“fontstyle.docx”));
  18.  
  19. //create paragraph
  20. XWPFParagraph paragraph = document.createParagraph();
  21.  
  22. //Set Bold an Italic
  23. XWPFRun paragraphOneRunOne = paragraph.createRun();
  24. paragraphOneRunOne.setBold(true);
  25. paragraphOneRunOne.setItalic(true);
  26. paragraphOneRunOne.setText(“Font Style”);
  27. paragraphOneRunOne.addBreak();
  28.  
  29. //Set text Position
  30. XWPFRun paragraphOneRunTwo = paragraph.createRun();
  31. paragraphOneRunTwo.setText(“Font Style two”);
  32. paragraphOneRunTwo.setTextPosition(100);
  33.  
  34. //Set Strike through and Font Size and Subscript
  35. XWPFRun paragraphOneRunThree = paragraph.createRun();
  36. paragraphOneRunThree.setStrike(true);
  37. paragraphOneRunThree.setFontSize(20);
  38. paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
  39. paragraphOneRunThree.setText(“ Different Font Styles”);
  40.  
  41. document.write(out);
  42. out.close();
  43. System.out.println(“fontstyle.docx written successully”);
  44. }
  45. }

Save the above code as FontStyle.java and then compile and execute it from the command prompt as follows −

  1. $javac FontStyle.java
  2. $java FontStyle

It will generate a Word file named fontstyle.docx in your current directory and display the following output on the command prompt −

  1. fontstyle.docx written successfully

The fontstyle.docx file looks as follows.

Font Style

Alignment

The following code is used to set alignment to the paragraph text −

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  4. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  5. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  6. import org.apache.poi.xwpf.usermodel.XWPFRun;
  7.  
  8. public class AlignParagraph {
  9.  
  10. public static void main(String[] args)throws Exception {
  11.  
  12. //Blank Document
  13. XWPFDocument document = new XWPFDocument();
  14.  
  15. //Write the Document in file system
  16. FileOutputStream out = new FileOutputStream(
  17. new File(“alignparagraph.docx”));
  18.  
  19. //create paragraph
  20. XWPFParagraph paragraph = document.createParagraph();
  21.  
  22. //Set alignment paragraph to RIGHT
  23. paragraph.setAlignment(ParagraphAlignment.RIGHT);
  24. XWPFRun run = paragraph.createRun();
  25. run.setText(“At tutorialspoint.com, we strive hard to +
  26. provide quality tutorials for self-learning +
  27. purpose in the domains of Academics, Information +
  28. Technology, Management and Computer Programming +
  29. Languages.”);
  30.  
  31. //Create Another paragraph
  32. paragraph = document.createParagraph();
  33.  
  34. //Set alignment paragraph to CENTER
  35. paragraph.setAlignment(ParagraphAlignment.CENTER);
  36. run = paragraph.createRun();
  37. run.setText(“The endeavour started by Mohtashim, an AMU +
  38. alumni, who is the founder and the managing director +
  39. of Tutorials Point (I) Pvt. Ltd. He came up with the +
  40. website tutorialspoint.com in year 2006 with the help +
  41. of handpicked freelancers, with an array of tutorials +
  42. for computer programming languages. “);
  43.  
  44. document.write(out);
  45. out.close();
  46. System.out.println(“alignparagraph.docx written successfully”);
  47. }
  48. }

Save the above code as AlignParagraph.java and then compile and execute it from the command prompt as follows −

  1. $javac AlignParagraph.java
  2. $java AlignParagraph

It will generate a Word file named alignparagraph.docx in your current directory and display the following output in the command prompt −

  1. alignparagraph.docx written successfully

The alignparagraph.docx file looks as follows −

alignparagraph.jpg