使用 Apache POI XWPFDocument 编写 RTL 语言时出现符号(撇号、括号)问题

problems with symbols (apostrophe, parenthesis) when writing RTL language with Apache POI XWPFDocument

我一直在尝试将 excel 文件中的希伯来语数据复制到文档中。 虽然字母本身被正确复制,但每当涉及到一些符号时,它就会变得一团糟。

例如:我得到的不是(text),而是)text(

到目前为止,这是我的代码:

XWPFParagraph newPara = document.insertNewParagraph(cursor);
newPara.setAlignment (ParagraphAlignment.RIGHT); 
CTP ctp = newPara.getCTP();
CTPPr ctppr;
if ((ctppr = ctp.getPPr()) == null) ctppr = ctp.addNewPPr();
ctppr.addNewBidi().setVal(STOnOff.ON);
XWPFRun newParaRun = newPara.createRun();
newParaRun.setText(name);

我尝试了一些“双向文本方向支持”(bidi) 行

(从这里得到: )

但不是那样,也与对齐无关...

使用较旧的文字处理软件应用程序时,当 LTR 字符和 RTL 字符混合在一个文本中时似乎会出现问题 运行。然后使用特殊的 BiDi 字符类型可能是解决方案。参见 https://en.wikipedia.org/wiki/Bidirectional_text#Table_of_possible_BiDi_character_types

另见 bidirectional with word document using Aphace POI

使用它可以完成以下工作:

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();
  String line = "(שָׁלוֹם)";
  run.setText("\u202E" + line + "\u202C");

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

它在包含 LTR 字符(())和 RTL 字符(שָׁלוֹם)的文本行之前使用 U+202E RIGHT-TO-LEFT OVERRIDE (RLO),在之后使用 U+202C POP DIRECTIONAL FORMATTING (PDF)该文本行。这告诉文字处理软件 RTL 的确切开始和结束位置。这导致我使用 MS Word 365WordPad.

的正确输出

apache poi 5.0.0用于Bidi .setVal(STOnOff.ON)不是更可行,但可以使用.setVal(true)

  //ctppr.addNewBidi().setVal(STOnOff.ON); // up to apache poi 4.1.2
  ctppr.addNewBidi().setVal(true); // from apache poi 5.0.0 on