How to read font size and font name in .docx in Ap

2019-07-20 10:20发布

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?

2条回答
【Aperson】
2楼-- · 2019-07-20 10:38

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

查看更多
仙女界的扛把子
3楼-- · 2019-07-20 10:58

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!

查看更多
登录 后发表回答