簡體   English   中英

使用 Openxml 讀取 word 文檔中的文本和格式

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

我一直試圖自己解決這個問題,但似乎我真的需要幫助。

我正在使用 OpenXml 閱讀 Word 文檔。

我需要word文檔中的文本及其格式。

我有獲取文本和屬性的代碼

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);
                }

            }

上面代碼的輸出與此類似

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

我需要的是word文檔中每個文本使用的格式。 但那些是什么意思

這是我的示例 Word 文檔的屏幕截圖。 我可以像 Bold 屬性一樣檢索它嗎? 字體名稱。 字體大小?

在此處輸入圖片說明

問候,鮑勃

是的。 您可以獲得每個文本的格式信息。

我假設你有所有的運行。 每次運行都有包含所有格式信息的 runProperties。

所以迭代每次運行並獲得如下格式。

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

希望這會有所幫助。 !!

您可以使用 Descendants 節點並在其上進行循環,然后您可以獲得所需的任何內容。

如果您仍然無法做到,請告訴我。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM