gpt4 book ai didi

java - Apache POI 的 XWPF 是否支持段落的自动连字符功能?

转载 作者:行者123 更新时间:2023-12-02 02:33:39 27 4
gpt4 key购买 nike

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

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

提前致谢!

最佳答案

在 Word Office OpenXML 中,自动连字设置是为整个文档设置的,并且可能会禁止单个段落的连字设置。整个文档的设置位于包的 /word/settings.xml 部分中。这是XWPFSettings但到目前为止,还无法使用 apache poi 的高级对象来获得此信息。因此,我们需要使用低级对象和反射来获取此内容并有权访问 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();

}
}

关于java - Apache POI 的 XWPF 是否支持段落的自动连字符功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46724896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com