使用Apache poi从docx获取文本样式

时间:2015-07-05 20:08:37

标签: java apache ms-word apache-poi docx

我正在尝试从MS docx文件中获取样式信息,使用粗体,斜体等添加样式编写文件内容没有问题。字体大小等,但阅读文件内容和获取样式信息并不是那么清楚。我尝试过使用XWPFDocument,这个API似乎没有能力读取样式。我现在正在尝试XWPFWordExtractor,它似乎有点前途,但我仍然无法获取文本的样式信息。

我阅读的内容类型与以下内容类似。

“您好,这是粗体文字,这是斜体文字 abd这是 粗体斜体文字

任何指向示例的指针都会很棒。

6 个答案:

答案 0 :(得分:2)

好的,基于Gagravarr的评论,解决方案如下所示,完全符合我的要求。所以基本上Gagravarr回答了这个问题,但是我不确定除了说它听到给他信任之外还有什么不同。

for (XWPFParagraph paragraph : docx.getParagraphs()) {
                int pos = 0;
                for (XWPFRun run : paragraph.getRuns()) {
                    System.out.println("Current run IsBold : " + run.isBold());
                    System.out.println("Current run IsItalic : " + run.isItalic());
                    for (char c : run.text().toCharArray()) {

                        System.out.print(c);
                        pos++;
                    }
                    System.out.println();
                }
            }

`

以下的输出

Current run IsBold : false Current run IsItalic : false "Hello, this is  Current run IsBold : true Current run IsItalic : false bold text Current run IsBold : false Current run IsItalic : false  and this is  Current run IsBold : false Current run IsItalic : true italic text Current run IsBold : false Current run IsItalic : false  a Current run IsBold : false Current run IsItalic : false n Current run IsBold : false Current run IsItalic : false d this is  Current run IsBold : true Current run IsItalic : true bold-italic text Current run IsBold : false Current run IsItalic : false "

答案 1 :(得分:0)

我放弃了尝试使用Apache poi,我发现另一个名为docx4j的lib,这似乎做了我需要的,我希望看一个现在可用的属性,一旦加载了docx文件你可以查看内容该文件采用xml格式,如下所示。

`

<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:ns27="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" mc:Ignorable="w14 wp14">
   <w:body>
      <w:p w:rsidR="009A66AB" w:rsidRDefault="000F4AD1">
         <w:r>
            <w:rPr>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>&quot;Hello, this is</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="apple-converted-space"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t> </w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Strong"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:bdr w:val="none" w:color="auto" w:sz="0" w:space="0" w:frame="true"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>bold text</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="apple-converted-space"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t> </w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>and this is</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="apple-converted-space"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t> </w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Emphasis"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:bdr w:val="none" w:color="auto" w:sz="0" w:space="0" w:frame="true"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>italic text</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="apple-converted-space"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t> </w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>an</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>d this is</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="apple-converted-space"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t> </w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Emphasis"/>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:b/>
               <w:bCs/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:bdr w:val="none" w:color="auto" w:sz="0" w:space="0" w:frame="true"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>bold-italic text</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:rFonts w:ascii="Helvetica" w:hAnsi="Helvetica" w:cs="Helvetica"/>
               <w:color w:val="222222"/>
               <w:sz w:val="23"/>
               <w:szCs w:val="23"/>
               <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
            </w:rPr>
            <w:t>&quot;</w:t>
         </w:r>
      </w:p>
      <w:sectPr w:rsidR="009A66AB">
         <w:pgSz w:w="11906" w:h="16838"/>
         <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
         <w:cols w:space="708"/>
         <w:docGrid w:linePitch="360"/>
      </w:sectPr>
   </w:body>
</w:document>

`

答案 2 :(得分:0)

你可以用 ConfigurationManager

答案 3 :(得分:0)

我找到了一种将样式从一个文档复制到另一个文档的非常好的方法。它并不像我希望的那样直接,但它有效。

  1. 将源word文档重命名为zip
  2. 提取内容
  3. 将styles.xml复制到字符串常量或读取文件
  4. 使用以下代码

    将样式复制到输出文档中
    public void copyStylesXml(String stylesXmlString) {
       try {
          CTStyles ctStyle = CTStyles.Factory.parse(stylesXmlString);
          XWPFStyles styles = getDoc().createStyles();
          styles.setStyles(ctStyle);
       } catch (Exception e) {
          log.warn(e, e);
       }
    }
    
  5. 同样的方法适用于复制列表格式

答案 4 :(得分:0)

这是从其他文档复制样式的一种非常好的方法。一点背景; docx文件实际上是包含styles.xml的许多xml文件的zip文件。在下面的代码示例中,我读取了numberin.xml,将其解析为CTStyles对象,然后在当前文档中进行设置。这是大部分代码。您可以使用相同的方法为您的Word编号复制numbering.xml。

// copy an existing style.xml document into this document to get styles
public void copyStylesFromDocument(String documentFileName) {
    log.debug("fileName " + documentFileName);
    try {
        InputStream is = CertificationReportHelper.getInputStreamFromZipFile(documentFileName, FILE_NAME_STYLES);
        CTStyles ctStyle = CTStyles.Factory.parse(is);
        XWPFStyles styles = getDoc().createStyles();
        styles.setStyles(ctStyle);
        log.info("Styles copied from file " + FILE_NAME_STYLES + " in document" + documentFileName);
    } catch (Exception e) {
        String msg = "Error copying styles from file " + FILE_NAME_STYLES + " in document" + documentFileName;
        addErrorMessage(msg, e);
        log.debug(e, e);
    }
    @SuppressWarnings("resource") // closing stream causes input stream to close and operation fails
public static InputStream getInputStreamFromZipFile(String zipFileName, String containedFile) {
    InputStream is = null;
    ZipFile zfile = null;
    try {
        zfile = new ZipFile(zipFileName);
        ZipEntry entry = zfile.getEntry(containedFile);
        log.trace(entry);
        if (entry != null) {
            is = zfile.getInputStream(entry);
            log.trace("created input stream  for file " + containedFile + " from zip file" + zipFileName);
        } else {
            String msg = "Error getting input stream for file " + containedFile + " from zip file " + zipFileName;
            // closing stream causes input stream to close and operation fails
            throw new ApplicationRuntimeException(msg);
        }
    } catch (Exception e) {
        String msg = "Error getting input stream for file " + containedFile + " from zip file " + zipFileName + "  Message:"
                + e.getMessage();
        log.warn("*** Throwing exception " + msg);
        throw new ApplicationRuntimeException(msg, e);
    } finally {
        // closing stream causes input stream to close and operation fails
        // try {
        // zfile.close();
        // } catch (IOException e) {
        // log.warn("Catching exception "+e+" closing zip file "+zipFileName);
        // }
    }
    return is;

答案 5 :(得分:0)

这是获取粗体属性的简单技巧。

run.getCTR().xmlText().contains("<w:b w:val=\"1\"/>") return true if bold otherwise false.