使用 Apache poi 从 docx 获取文本样式 [英] Getting text style from docx using Apache poi

查看:61
本文介绍了使用 Apache poi 从 docx 获取文本样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 MS docx 文件中获取样式信息,我可以使用粗体、斜体等添加样式编写文件内容.字体大小等,但读取文件内容和获取样式信息不是那么清楚.我试过使用 XWPFDocument,这个 API 似乎没有读取样式的能力.我现在正在尝试 XWPFWordExtractor,它看起来更有希望,但我仍然无法获取文本的样式信息.

I'm trying to get the style information from an MS docx file, I have no problem writing file content with added styles like bold, italic. font size etc, but reading the file content and getting the style information is not so clear. I've tried using XWPFDocument, this API does not seem to have the ability to read the styles. I'm now trying XWPFWordExtractor which seems a bit more promising but I'm still stuck getting the style information for the text.

我阅读的内容类型类似于以下内容.

The type of content I reading looks similar to the following.

您好,这是粗体文本,这是斜体文本以及这是粗斜体文本"

"Hello, this is bold text and this is italic text abd this is bold-italic text"

任何指向示例的指针都会很棒.

Any pointers to an example would be great.

推荐答案

好的,根据 Gagravarr 的评论,解决方案如下,正是我想要的.所以基本上 Gagravarr 回答了这个问题,但我不知道除了说它听到给他信任之外还有什么.

Okay, so based on the comments from Gagravarr, the solution is below, exactly as I wanted. So basically Gagravarr answered the question but I'm not sure how apart from saying it hear to give him credit.

for (XWPFParagraph paragraph : docx.getParagraphs()) {
                int pos = 0;
                for (XWPFRun run : paragraph.getRuns()) {
                    System.out.println("Current run IsBold : " + run.isBold());
                    System.out.println("Current run IsItalic : " + run.isItalic());
                    for (char c : run.text().toCharArray()) {

                        System.out.print(c);
                        pos++;
                    }
                    System.out.println();
                }
            }

`

输出如下

<代码>当前运行 IsBold : false当前运行 IsItalic : false你好,这是当前运行 IsBold : true当前运行 IsItalic : false粗体当前运行 IsBold : false当前运行 IsItalic : false这是当前运行 IsBold : false当前运行 IsItalic : true斜体文字当前运行 IsBold : false当前运行 IsItalic : false一种当前运行 IsBold : false当前运行 IsItalic : falsen当前运行 IsBold : false当前运行 IsItalic : falsed 这是当前运行 IsBold : true当前运行 IsItalic : true粗斜体文本当前运行 IsBold : false当前运行 IsItalic : false"

这篇关于使用 Apache poi 从 docx 获取文本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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