如何在Apache POI(java)中读取.docx中的字体大小和字体名称

时间:2022-11-25 22:15:46

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.

当它读取.docx文件时,它有时会返回字体大小为-1而字体系列为null。

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.

我知道这是因为它们是默认值但我不想-1和null我只想要该默认值的名称。

How can I read it?

我怎么读呢?

2 个解决方案

#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

XWPFRun上的字体/字符属性仅返回覆盖详细信息,因此运行到周围文本的设置不同

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.

如果运行使用默认样式,则需要向上移动到它所属的XWPFParagraph(或表等),然后调用getStyleId()以获取适用于段落的样式。

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

然后,在文档上,调用XWPFDocument.getStyles()。getStyle(styleId)以获取适用于感兴趣段落的XWPFStyle对象。

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!

目前,XWPFStyle对象有点低级别,所以要感谢补丁以获得改进!

#2


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

我有一个类似的问题,我用document.getStyles()修复它.getDefaultRunStyle()。getFontSize();

#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

XWPFRun上的字体/字符属性仅返回覆盖详细信息,因此运行到周围文本的设置不同

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.

如果运行使用默认样式,则需要向上移动到它所属的XWPFParagraph(或表等),然后调用getStyleId()以获取适用于段落的样式。

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

然后,在文档上,调用XWPFDocument.getStyles()。getStyle(styleId)以获取适用于感兴趣段落的XWPFStyle对象。

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!

目前,XWPFStyle对象有点低级别,所以要感谢补丁以获得改进!

#2


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

我有一个类似的问题,我用document.getStyles()修复它.getDefaultRunStyle()。getFontSize();