简体   繁体   English

使用 Openxml 读取 word 文档中的文本和格式

[英]Read text and format in a word document using Openxml

I've been trying to solve this my self but it seem that i really need help.我一直试图自己解决这个问题,但似乎我真的需要帮助。

I am reading a Word document using OpenXml.我正在使用 OpenXml 阅读 Word 文档。

And i need the text in the word document and its format.我需要word文档中的文本及其格式。

I have this code for getting the text and attributes我有获取文本和属性的代码

WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true);

            Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
            foreach (var item in body)
            {
                //Console.WriteLine(">>text: " + item.InnerText);
                foreach (var tt in item.GetAttributes())
                {
                    Console.WriteLine(tt.LocalName + " : " + tt.Value);
                }

            }

And the output of the code above is something similar to this上面代码的输出与此类似

rsidR : 0067182C
rsidP : 002A2C9A
rsidRDefault : 004052D2
rsidR : 0067182C
rsidRDefault : 004052D2
rsidR : 0067182C
rsidSect : 0067182C

What i need is the format used in each text in the word document.我需要的是word文档中每个文本使用的格式。 But what are those means但那些是什么意思

And this is the screenshot of my sample Word document.这是我的示例 Word 文档的屏幕截图。 Can i retrieve it like a property Bold.我可以像 Bold 属性一样检索它吗? Font Name.字体名称。 Font size?字体大小?

enter image description here在此处输入图片说明

Regards, Bob问候,鲍勃

Yes.是的。 You can get the formatting information for each text.您可以获得每个文本的格式信息。

I am assuming that you have all the runs.我假设你有所有的运行。 Each run has runProperties which has all the formatting information.每次运行都有包含所有格式信息的 runProperties。

So iterate over each run and get the formatting like below.所以迭代每次运行并获得如下格式。

bool Border = run.RunProperties.Border != null,
bool Bold = run.RunProperties.Bold != null,

Hope this will help.希望这会有所帮助。 !! !!

You can get with Descendants node and make loop on it then you can get what ever you are looking for.您可以使用 Descendants 节点并在其上进行循环,然后您可以获得所需的任何内容。

let me know if you still not able to do it.如果您仍然无法做到,请告诉我。

thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM