gpt4 book ai didi

java - 对于使用 Apache POI 的 .docx 文件,run.getFontFamily() 返回 null

转载 作者:行者123 更新时间:2023-11-30 02:32:07 26 4
gpt4 key购买 nike

我想逐段阅读 .docx 文件,并检查每个段落的字体系列、字体大小、边距、对齐方式、颜色等。这是我的 .docx 文件的示例:

Sample of .docx file

这是我的代码:

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(对于我选择的每个字体系列)、fontName、textPosition 始终为 null。我有另一个代码示例来执行此操作:

            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 从 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;

希望这会有所帮助。

关于java - 对于使用 Apache POI 的 .docx 文件,run.getFontFamily() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44040822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com