簡體   English   中英

使用 Apache poi 從 docx 獲取文本樣式

[英]Getting text style from docx using Apache poi

我正在嘗試從 MS docx 文件中獲取樣式信息,我可以使用粗體、斜體等添加樣式編寫文件內容。 字體大小等,但讀取文件內容和獲取樣式信息不是那么清楚。 我試過使用 XWPFDocument,這個 API 似乎沒有讀取樣式的能力。 我現在正在嘗試 XWPFWordExtractor,它看起來更有希望,但我仍然無法獲取文本的樣式信息。

我閱讀的內容類型類似於以下內容。

“您好,這是粗體文字,這是斜體文字,這是粗斜體文字

任何指向示例的指針都會很棒。

好的,根據 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 "

這是獲取粗體屬性的簡單技巧。

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

我放棄了嘗試使用 Apache poi,我找到了另一個名為 docx4j 的庫,這似乎滿足了我的需要,我想查看的屬性現在可用,一旦加載了 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>

`

你可以使用paragraph.getCTP().getPPr().getRPr().isSetB()

我發現了一種將樣式從一個文檔復制到另一個文檔的好方法。 它不像我希望的那樣直接,但它有效。

  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); } }

相同的方法適用於復制列表格式

這是從另一個文檔復制樣式的非常好的方法。 一點背景; docx 文件實際上是許多 xml 文件(包括 style.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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM