How to read font size and font name in .docx in Apache POI(java)

狂风中的少年 提交于 2020-01-05 01:42:47

问题


Here is my code.

XWPFRun run = runlist.get(0);
double fontsize = (double)(run.getFontSize());
String fontfamily = (String)run.getFontFamily();

When it read the .docx file it will sometimes return -1 in font size and null in font family.

I know that it's because they are default value but I don't want -1 and null I just want the name of that default value.

How can I read it?


回答1:


The font/character properties on a XWPFRun only return the override details, so settings which are different on that run to the surrounding text

If the run is using the default stylings, you'll need to move up to the XWPFParagraph it belongs to (or table etc), then call getStyleId() to get the style which applies to the paragraph.

Then, on the document, call XWPFDocument.getStyles().getStyle(styleId) to get the XWPFStyle object that applies to the paragraph of interest.

From the style, you can fetch the properties defined in that style such as the font. You may also need to fetch the parent, if the style inherits from one.

Currently, the XWPFStyle object is a bit low level, so patches to improve it would be gratefully received!




回答2:


I had a similar problem and I got it fixed with document.getStyles().getDefaultRunStyle().getFontSize();



来源:https://stackoverflow.com/questions/30094667/how-to-read-font-size-and-font-name-in-docx-in-apache-poijava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!