poi 宋体_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

{

//Blank Document

XWPFDocument document= new XWPFDocument();

//Write the Document in file system

FileOutputStream out = new FileOutputStream(

new File("fontstyle.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();

System.out.println("fontstyle.docx written successully");

}

}

将上述代码保存为FontStyle.java,然后从命令提示符处编译并执行它,如下所示:$javac FontStyle.java

$java FontStyle

它将在当前目录中生成名为font style.docx的Word文件,并在命令提示符下显示以下输出:fontstyle.docx written successfully

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

{

//Blank Document

XWPFDocument document= new XWPFDocument();

//Write the Document in file system

FileOutputStream out = new FileOutputStream(

new File("alignparagraph.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();

System.out.println("alignparagraph.docx written successfully");

}

}

将上述代码保存为AlignParagraph.java,然后从命令提示符处编译并执行,如下所示:$javac AlignParagraph.java

$java AlignParagraph

它将在当前目录中生成名为align paragraph.docx的Word文件,并在命令提示符中显示以下输出:alignparagraph.docx written successfully

alignparagraph.docx文件如下所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值