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

查看:129
本文介绍了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的高级对象来获得此功能.因此,我们需要使用低级对象和反射来获得此权限并可以访问

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.

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天全站免登陆