个性化阅读
专注于IT技术分析

apache poi word对齐

本文概述

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

ParagraphAlignment.RIGHT将段落右对齐。

ParagraphAlignment.LEFT将段落左对齐。

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

让我们看一个例子,其中我们将文本向左对齐。

Apache POI 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("srcmini.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);
	    }
	}
}

输出:

赞(0)
未经允许不得转载:srcmini » apache poi word对齐

评论 抢沙发

评论前必须登录!