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中实现.但是基础对象

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");

  doc.write(new FileOutputStream("WordDocument.docx"));

 }
}