【问题标题】:Is there any way to identify character styles with Apache POI xwpf documents?有没有办法用 Apache POI xwpf 文档识别字符样式?
【发布时间】:2016-02-07 11:45:07
【问题描述】:

Here 我们看到用于“HWPF”(MS Word 2000 .doc)文件的 Apache POI 有一个方法 CharacterRun.getStyleIndex()... 您可以通过它来识别 字符 适用于此运行的样式(不是段落样式)...

但是对于 XWPF 的东西(MS Word 2003+ .docx)文件,我找不到任何方法来识别 XWPFRun 对象中的字符样式。

【问题讨论】:

    标签: java apache-poi xwpf textstyle


    【解决方案1】:

    以下代码应该从XWPFDocument 内的所有运行[1] 中获取所有样式,如果它们被应用为字符样式,则打印它们的 XML:

    import java.io.FileInputStream;
    
    import org.apache.poi.xwpf.usermodel.*;
    
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
    
    import java.util.List;
    
    public class WordGetRunStyles {
    
     public static void main(String[] args) throws Exception {
    
      FileInputStream fis = new FileInputStream("This is a Test.docx");
      XWPFDocument xdoc = new XWPFDocument(fis);
    
      List<XWPFParagraph> paragraphs = xdoc.getParagraphs();
      for (XWPFParagraph paragraph : paragraphs) {
       List<XWPFRun> runs = paragraph.getRuns();
       for (XWPFRun run : runs) {
        CTRPr cTRPr = run.getCTR().getRPr();
        if (cTRPr != null) {
         if (cTRPr.getRStyle() != null) {
          String styleID = cTRPr.getRStyle().getVal();
          System.out.println("Style ID=====================================================");
          System.out.println(styleID);
          System.out.println("=============================================================");
          XWPFStyle xStyle = xdoc.getStyles().getStyle(styleID);
          if (xStyle.getType() == STStyleType.CHARACTER) {
           System.out.println(xStyle.getCTStyle());
          }
         }
        }
       }
      }
     }
    }
    

    [1] 请不要尝试内容过多的文档;-)。

    正如@mike rodent 的评论中提到的,如果你得到java.lang.NoClassDefFoundError: org/openxmlformats/schemas/*something*,那么你必须使用https://poi.apache.org/faq.html#faq-N10025 中提到的完整的ooxml-schemas-1.3.jar。

    对我来说,这段代码在没有这个的情况下运行,因为我不使用Phonetic Guide Properties (https://msdn.microsoft.com/en-us/library/office/documentformat.openxml.wordprocessing.rubyproperties.aspx)。我使用 Office 2007。

    【讨论】:

    • 谢谢...做得好。不过,这里发生了一些非常烦人的事情。我自己偶然发现了这个XWPFRun.getCTR() 方法......当我尝试使用它时,我得到:“NoClassDefFoundError: ... org/openxmlformats/schemas/wordprocessingml/x2006/main/CTRuby”。看来,虽然我已经包含了 POI 3.13 dload 中的所有 jar,但我缺少一些依赖项。事实证明,这是 ooxml-schemas-1.0.jar,例如,可以从 Maven 获得。我很好奇你为什么没有提到你必须使用这个罐子……(也许它已经在你的路上了)。
    • @mike rodent:请看我的补充。顺便说一句:“我很好奇你为什么不提……”。如果我是你,我会考虑如何对待那些无偿提供帮助的人。
    • 无意冒犯...我感兴趣(对我来说,这个形容词并不意味着批评自己)现在我知道答案了。我不是对自己“恼火”,而是对 Apache……为什么在地球上有人下载 POI 包时他们没有对此进行说明?实际上,我花了一些时间才弄清楚我需要这个额外的 jar ......因为下载中已经有 2 个名为“poi-ooxml ...”的 jar 和一个名为“ooxml-lib”的目录!
    • PS 这里的问题似乎是当您在“NoClassDefFoundError: ... org/openxmlformats/schemas/wordprocessingml/ ”。包括最后的“wordprocessingml”位是我未能定向到他们的常见问题解答的原因!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    相关资源
    最近更新 更多