如何更改 apache poi word 文档中的文本方向(不是段落对齐)?(XWPF)

问题描述:

我正在尝试使用 Apache poi word 3.8 以波斯语/阿拉伯语创建 word 文档.我的问题是:如何更改文档中的文本方向?(这意味着改变文本方向,而不仅仅是改变段落文本对齐方式)在 MS Word 中,我们可以使用从右到左的文本方向 来更改文本方向并右对齐 设置对齐方式.什么相当于 poi set 属性中的第一个?

I'm trying to use Apache poi word 3.8 to create word document in persian/arabic language. My question is: how change text direction in document? ( it means changing text direction not changing just paragraph text alignment) In MS Word we could use Right-to-left text direction to change text direction and Align Right to set alignment. What’s equivalent of the first one in poi set property?

这是双向文本方向支持 (bidi),默认情况下尚未在 apache poi 中实现.但是底层对象 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrBase 支持这一点.所以我们必须从 XWPFParagraph 中获取这个底层对象.

This is bidirectional text direction support (bidi) and is not yet implemented in apache poi per default. But the underlying object org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrBase supports this. So we must get this underlying object from the XWPFParagraph.

示例:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;

public class CreateWordRTLParagraph {

 public static void main(String[] args) throws Exception {

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("Paragraph 1 LTR");

  paragraph = doc.createParagraph();

  CTP ctp = paragraph.getCTP();
  CTPPr ctppr;
  if ((ctppr = ctp.getPPr()) == null) ctppr = ctp.addNewPPr();
  ctppr.addNewBidi().setVal(STOnOff.ON);

  run = paragraph.createRun();
  run.setText("السلام عليكم");

  paragraph = doc.createParagraph();
  run = paragraph.createRun();
  run.setText("Paragraph 3 LTR");
    
  FileOutputStream out = new FileOutputStream("WordDocument.docx");
  doc.write(out);
  out.close();
  doc.close();    
 }
}