Apache POI 的 XWPF 是否支持段落的 authohyphenation 功能? [英] Does the XWPF of Apache POI support paragraph's authohyphenation feature?

查看:53
本文介绍了Apache POI 的 XWPF 是否支持段落的 authohyphenation 功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know, that HWPF has this feature, but I can't find any simular in XWPF.

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

Maybe there are workarounds for solving this problem. If you know some, please share with me.

提前致谢!

推荐答案

在 Word Office OpenXML 中,自动断字设置是为整个文档设置的,并且可以为单个段落隐藏.整个文档的设置在包的/word/settings.xml 部分.这是 XWPFSettings 但这是不可能的到目前为止,使用 apache poi 的高级对象来获取它.所以我们需要使用低级对象和反射来获得它并可以访问 CTSettings.addNewAutoHyphenation.

In Word Office OpenXML the automatic hyphenation settings are set for the whole document and may be suppressed for single paragraphs. The settings for the whole document are in /word/settings.xml part of the package. This is 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.

The possible suppressing of automatic hyphenation for single paragraphs is done in CTPPrBase.addNewSuppressAutoHyphens and is also not get-able using high level 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();

 }
}

这篇关于Apache POI 的 XWPF 是否支持段落的 authohyphenation 功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆