我知道,HWPF有这个特性,但是我在XWPF中找不到任何同义词。
也许有解决这个问题的变通方法。如果你知道一些,请和我分享。
提前感谢!
在Word Office OpenXML中,自动断字设置是为整个文档设置的,并且可能会为单个段落进行抑制。整个文档的设置在包的/word/settings. xml
部分中。这是XWPFSettings,但到目前为止,使用apache poi
的高级对象是不可能获得这个的。因此,我们需要使用低级对象和反射来获得这个并访问CTSettings.addNewAutoHyphation。
单个段落的自动连字符的可能抑制是在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();
}
}