run.getFontFamily() returns 使用 Apache POI 的 .docx 文件为空

run.getFontFamily() returns null for .docx files using Apache POI

我想逐段阅读 .docx 文件,我想检查每个段落的 font-family、font-size、边距、对齐方式、颜色等。 这是我的 .docx 文件的示例:

这是我的代码:

FileInputStream fis = new FileInputStream("D:/test3.docx");
XWPFDocument docx = new XWPFDocument(fis);
List<XWPFParagraph> paragraphList = docx.getParagraphs();
for (int i = 0; i < paragraphList.size(); i++) {
            System.out.println("paragraph " + i + " is::    " + paragraphList.get(i).getText());
            for (XWPFRun run : paragraphList.get(i).getRuns()) {
                System.out.println("paragraph :: run text is::    " + run.text());
                System.out.println("paragraph :: run color is::    " + run.getColor());
                System.out.println("paragraph :: run font-famyly is::    " + run.getFontFamily()); //It always return null; why?
                System.out.println("paragraph :: run font-name is::    " + run.getFontName()); //It always return null; why?
                System.out.println("paragraph :: run text position is::    " + run.getTextPosition()); //It always return -1; why?
                System.out.println("paragraph :: run font-size is::    " + run.getFontSize());
                System.out.println("paragraph :: run IsBold::    " + run.isBold());
                System.out.println("paragraph :: run IsItalic::    " + run.isItalic());

            }}

但是 fontFamily(对于我选择的每个 font-family)、fontName、textPosition 始终为空。 我有另一个代码示例可以执行此操作:

            XWPFStyles styles = docx.getStyles();
        for (int i = 0; i < paragraphList.size(); i++) {
            System.out.println("paragraph " + i + " styleID  is::    " + paragraphList.get(i).getStyleID());
            if (paragraphList.get(i).getStyleID() != null) {
                String styleid = paragraphList.get(i).getStyleID();
                XWPFStyle style = styles.getStyle(styleid);
                if (style != null) {
                    System.out.println("style name is::    " + style.getName());
                    if (style.getName().startsWith("heading")) {
                        System.out.println("This part of text is heading!!");
                    }
                }

            }
        }

但除标题外,样式通常为空。

Apache POI 解析 .docx 文件的 document.xml 部分。当您执行 run.getFontFamily() 时,仅当 运行 的 运行 属性中存在该字体系列时,它才会 return 字体系列。否则它将 return 为空。例如,考虑这个示例 运行

<w:r>
    <w:rPr>
        <w:lang w:val="en-US"/>
    </w:rPr>
    <w:t>The quick brown fox jumps over the lazy dog.</w:t>
</w:r>

这确实在 <w:rPr> 运行 属性标签中指定了它的字体系列。在这种情况下,您必须向上层级查看具有此 运行 的段落是否具有样式。如果 <w:Pr> 段落属性没有样式,则应用文档默认的字体系列。文档默认值在 styles.xml 文件中指定。

这是使用 apache POI 从 style.xml 获取样式的示例代码。

     XWPFDocument docx;                                                 // Set the docx
       XWPFRun run;                                                    //get the required run
       String fontFamily= run.getFontFamily();
        if(fontFamily == null){                                         // When the font in the run is null check for the default fonts in styles.xml
            String styleID = run.getParagraph().getStyleID();
            XWPFStyle style = docx.getStyle(styleID);
            CTStyle ctStyle = style.getCTStyle();
            CTRPr ctrPr = ctStyle.getRPr();
            CTFonts ctFonts = ctrPr.getRFonts();
            if(ctFonts!= null){
                fontFamily = ctFonts.getAscii();               // Or you may getCs() , getAnsi() etc.
            }
//                        else {
//                            fontFamily = ctStyle.getPPr().getRPr().getRFonts().getAscii();
//                        }
//                        System.out.println();
        }
        return fontFamily;

希望这会有所帮助。

我发现我想检查的字体不是标准的!!因此,对于另一种标准字体,以上所有代码都可以完美运行。