Apache POI 的 XWPF 是否支持段落自动断字功能?

Does the XWPF of Apache POI support paragraph's authohyphenation feature?

我知道,HWPF 有这个功能,但是我在 XWPF 中找不到类似的东西。

也许有解决此问题的变通方法。如果你知道一些,请与我分享。

提前致谢!

在 Word Office OpenXML 中,自动断字设置是针对整个文档设置的,可能会针对单个段落取消设置。整个文档的设置在包的 /word/settings.xml 部分。这是XWPFSettings but it is not possible to get this using the high level objects of apache poi until now. So we need using low level objects and reflection to get this and having access to CTSettings.addNewAutoHyphenation

CTPPrBase.addNewSuppressAutoHyphens 中可能会抑制单个段落的自动断字,并且也无法使用高级 apache poi

示例:

import java.io.FileOutputStream;

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

import org.apache.poi.POIXMLDocumentPart;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STSectionMark;

import java.lang.reflect.Field;

import java.math.BigInteger;

public class CreateWordAutoHyphenation {

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

  XWPFDocument document = new XWPFDocument();

  POIXMLDocumentPart part = null;
  for (int i = 0; i < document.getRelations().size(); i++) {
   part = document.getRelations().get(i);
   if (part instanceof XWPFSettings) break;
  }
  if (part instanceof XWPFSettings) {
   XWPFSettings settings = (XWPFSettings)part;
   Field _ctSettings = XWPFSettings.class.getDeclaredField("ctSettings"); 
   _ctSettings.setAccessible(true); 
   CTSettings ctSettings = (CTSettings)_ctSettings.get(settings);
   ctSettings.addNewAutoHyphenation();
  }

  String testtext = "This text tests whether automatic hyphenation opportunities are set on for this document and not are suppressed for this paragraph. Since in Word Office OpenXML the automatic hyphenation settings are set for the whole document and may be suppressed for single paragraphs.";

  XWPFParagraph paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.BOTH);
  XWPFRun run = paragraph.createRun();
  run.setFontSize(18);
  run.getCTR().getRPr().addNewLang().setVal("en-US");
  run.setText(testtext);

  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.addBreak(BreakType.COLUMN);

  paragraph.setAlignment(ParagraphAlignment.BOTH);
  paragraph.getCTP().addNewPPr().addNewSuppressAutoHyphens();
  run = paragraph.createRun();
  run.setFontSize(18);
  run.getCTR().getRPr().addNewLang().setVal("en-US");
  run.setText(testtext);

  document.getDocument().getBody().addNewSectPr().addNewType().setVal(STSectionMark.CONTINUOUS);
  document.getDocument().getBody().getSectPr().addNewCols().setNum(BigInteger.valueOf(2));

  document.write(new FileOutputStream("CreateWordAutoHyphenation.docx"));
  document.close();

 }
}