如何用POI来读取每个字的字体大小在Word文档中? [英] How to read font size of each word in a word document using POI?

查看:2438
本文介绍了如何用POI来读取每个字的字体大小在Word文档中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否有有2的字体不过在Word文档中存在的任何东西,我一直没能做到这一点。首先,我试图读取每个字的字体,只有一条线和7万字的样本word文档英寸我没有得到正确的结果。

I am trying to find out whether there exist anything in the word document that has a font of 2. However, I have not been able to do this. To begin with, I've tried to read the font of each word in a sample word document that only has one line and 7 words. I am not getting the correct results.

下面是我的code:

HWPFDocument doc = new HWPFDocument (fileStream);
WordExtractor we = new WordExtractor(doc);
Range range = doc.getRange()
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
  Paragraph pr = range.getParagraph(i);
  int k = 0
  while (true) {
     CharacterRun run = pr.getCharacterRun(k++);
     System.out.println("Color: " + run.getColor());
     System.out.println("Font: " + run.getFontName());
     System.out.println("Font Size: " + run.getFontSize());
     if (run.getEndOffSet() == pr.getEndOffSet())
       break;
  }
}

不过,上述code总是双打的字体大小。即,如果在文档中的实际字体大小为12然后将其输出24,如果实际的字体是8然后将其输出16

However, the above code always doubles the font size. i.e. if the actual font size in the document is 12 then it outputs 24 and if actual font is 8 then it outputs 16.

这是从word文档??

Is this the correct way to read font size from a word document ??

推荐答案

是的,这是正确的做法;测量是对半分。

Yes, that's the correct way; the measurement is in half points.

在一个的docx,你必须是这样的:

In a docx, you'd have something like:

<w:rPr>

  <w:sz w:val="28" /> 

</w:rPr>

ECMA 376规格上@sz 单位定义为<一个HREF =htt​​p://webapp.docx4java.org/OnlineDemo/ecma376/WordML/ST_HpsMeasure.html相对=nofollow> ST_HpsMeasure(以半分的测量)

及其与二进制文档格式,它HWPF支持相同。如果你看一下 [MS-DOC] ,你会看到它也规定了半分文本的大小。

Its the same with the binary doc format, which HWPF supports. If you look at [MS-DOC], you'll see it also specifies the size of text in half-points.

这篇关于如何用POI来读取每个字的字体大小在Word文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆