Apache POI Word - 字体和对齐
本章介绍如何使用 Java 在 Word 文档中应用不同的字体样式和对齐方式。通常,字体样式包含:字体大小、类型、粗体、斜体和下划线。对齐方式分为左对齐、居中对齐、右对齐和两端对齐。
字体样式
以下代码用于设置不同样式的字体 −
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 FontStyle { public static void main(String[] args)throws Exception { //空白文档 XWPFDocument document = new XWPFDocument(); //将文档写入文件系统 FileOutputStream out = new FileOutputStream(new File("fontstyle.docx")); //创建段落 XWPFParagraph passage = document.createParagraph(); //设置粗体和斜体 XWPFRun passageOneRunOne = passage.createRun(); passageOneRunOne.setBold(true); passageOneRunOne.setItalic(true); passageOneRunOne.setText("字体样式"); passageOneRunOne.addBreak(); //设置文本位置 XWPFRun passageOneRunTwo = passage.createRun(); passageOneRunTwo.setText("字体样式二"); passageOneRunTwo.setTextPosition(100); //设置删除线、字体大小和下标 XWPFRun paragraphOneRunThree = paragraph.createRun(); paragraphOneRunThree.setStrike(true); paragraphOneRunThree.setFontSize(20); paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT); paragraphOneRunThree.setText(" Different Font Styles"); document.write(out); out.close(); System.out.println("fontstyle.docx written successully"); } }
将上述代码保存为 FontStyle.java,然后编译并从命令提示符执行它,如下所示 −
$javac FontStyle.java $java FontStyle
它将在当前目录中生成一个名为 fontstyle.docx 的 Word 文件,并在命令提示符上显示以下输出 −
fontstyle.docx 写入成功
fontstyle.docx 文件如下所示。

对齐
以下代码用于设置段落文本的对齐方式 −
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 AlignParagraph { public static void main(String[] args)throws Exception { //空白文档 XWPFDocument document = new XWPFDocument(); //将文档写入文件系统 FileOutputStream out = new FileOutputStream( new File("alignparagraph.docx")); //创建段落 XWPFParagraph passage = document.createParagraph(); //将段落对齐方式设置为右对齐 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."); //创建另一个段落 paragraph = document.createParagraph(); //将段落对齐方式设置为居中 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(); System.out.println("alignparagraph.docx 写入成功"); } }
将上述代码保存为 AlignParagraph.java,然后从命令提示符中编译并执行它,如下所示 −
$javac AlignParagraph.java $java AlignParagraph
它将在当前目录中生成一个名为 alignparagraph.docx 的 Word 文件,并在命令提示符中显示以下输出 −
alignparagraph.docx 写入成功
alignparagraph.docx 文件如下所示 −
