本文介绍了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");

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

 }
}

这篇关于apache poi word(XWPF)如何更改文档中的文本方向(而不是段落对齐)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:30