无涯教程-POI Word - 对齐方式

1 阅读1分钟

为了使文本向右,向左和居中对齐,Apache POI提供了setAlignment()方法,该方法需要对齐常量(例如CENTER)。

ParagraphAlignment.RIGHT      -  将段落右对齐。

ParagraphAlignment.LEFT          -  使该段落向左对齐。

ParagraphAlignment.CENTER   -  将段落与中心对齐。

让无涯教程看一个例子,其中将文本向左对齐。

Word对齐示例

package poiexample;
import java.io.FileOutputStream;
import java.io.OutputStream;
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 AligningExample {
	public static void main(String[] args) {
		XWPFDocument doc = new XWPFDocument();
	    try(OutputStream os = new FileOutputStream("Learnfk.docx")) {
	    	 XWPFParagraph paragraph = doc.createParagraph();
	    	 paragraph.setAlignment(ParagraphAlignment.RIGHT);
	         XWPFRun run = paragraph.createRun();
	         run.setText("Text is aligned right");
	         doc.write(os);
	    }catch(Exception e) {
	    	System.out.println(e);
	    }
	}
}

输出:

Apache POI Word Aligning

参考链接

www.learnfk.com/apache-poi-…