apache poi word对齐

本文概述

  • 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); } } }

【apache poi word对齐】输出:
apache poi word对齐

文章图片

    推荐阅读