在word中,我们可以给段落设置左对齐、居中对齐、右对齐和两端对齐等对齐方式。本文将介绍如何使用spire.doc for java给word文档的段落设置对齐方式。
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class paragraphalighment {
public static void main(string[] args){
//创建word文档
document document = new document();
//添加一个section
section section = document.addsection();
//创建段落样式以给段落设置字体名称和字号
paragraphstyle style = new paragraphstyle(document);
style.setname("style");
style.getcharacterformat().setfontname("宋体");
style.getcharacterformat().setfontsize(11f);
document.getstyles().add(style);
//添加段落,应用段落样式,并设置左对齐
paragraph paragraph = section.addparagraph();
paragraph.appendtext("该段落为左对齐");
paragraph.applystyle(style.getname());
paragraph.getformat().sethorizontalalignment(horizontalalignment.left);
//添加段落,应用段落样式,并设置居中对齐
paragraph = section.addparagraph();
paragraph.appendtext("该段落为居中对齐");
paragraph.applystyle(style.getname());
paragraph.getformat().sethorizontalalignment(horizontalalignment.center);
//添加段落,应用段落样式,并设置右对齐
paragraph = section.addparagraph();
paragraph.appendtext("该段落为右对齐");
paragraph.applystyle(style.getname());
paragraph.getformat().sethorizontalalignment(horizontalalignment.right);
//添加段落,应用段落样式,并设置两端对齐
paragraph = section.addparagraph();
paragraph.appendtext("该段落为两端对齐");
paragraph.applystyle(style.getname());
paragraph.getformat().sethorizontalalignment(horizontalalignment.justify);
//保存文档
document.savetofile("paragraphalignment.docx", fileformat.docx_2013);
}
}