我正在使用 docx4j 和他们为零件列表构建的非常有用的网络应用程序:http://webapp.docx4java.org/OnlineDemo/PartsList.html
我有一个包含五个单词的示例文档。前四个字体大小为 12,最后一个字体大小为 8。
我想阅读文档中使用的所有不同字体大小。所以在本例中:12 和 8
我在网络应用程序上上传了示例文档,我认为此信息将存储在 document.xml
中,但我不确定,因为我只看到 16
而不是xml 中的 24
。另外,我不确定如何提取这些信息。
问题
如何提取docx4j中word内容的字体大小?
如何提取每个单词的字体颜色和整个word文档的背景颜色?
最佳答案
如果运行时未设置字体大小,并且正在使用样式,则需要检查样式层次结构。如果未在那里设置,它将返回默认值。
正如 ECMA 4ed 第 1 部分将其放入 17.7.2(样式层次结构)中:
This process can be described as follows:
- First, the document defaults are applied to all runs and paragraphs in the document.
- Next, the table style properties are applied to each table in the document, following the conditional formatting inclusions and exclusions specified per table.
- Next, numbered item and paragraph properties are applied to each paragraph formatted with a numbering style.
- Next, paragraph and run properties are applied to each paragraph as defined by the paragraph style.
- Next, run properties are applied to each run with a specific character style applied.
- Finally, we apply direct formatting (paragraph or run properties not from styles). If this direct formatting includes numbering, that numbering + the associated paragraph properties are applied.
If the value of the rFonts element (§17.3.2.26) references a font which is not available, applications determine a suitable alternative font via a process called font substitution, which is defined in §17.8.2.
docx4j 做了类似的事情 - 例如参见 https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/PropertyResolver.java 中的第 430 行和 ff
类似的原则适用于字体颜色。
这里我不讨论如何逐字迭代文档(或者更确切地说,逐个运行),除了说 google traversalutil
在运行中显式设置字体大小的示例
<w:r>
<w:rPr>
<w:sz w:val="36"/>
</w:rPr>
<w:t>this is 18 points</w:t>
</w:r>
您可以在 Microsoft Word 中或使用 docx4j 进行设置。要了解如何在 docx4j 中执行此操作,您可以使用 webapp 从示例 docx 生成代码,但本质是:
org.docx4j.wml.R yourRun;
yourRun.getRPr().setSz(an HpsMeasure);
关于java - 如何读取 docx 中使用的所有字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690672/