如何使用 OpenXML 在 Microsoft Office Word 中检索评论或添加书签的文本
How to retrieve text commented or bookmarked in Microsoft Office Word, with OpenXML
我一直在尝试使用 OpenXML 在 .docx 文档中检索用户评论或添加书签的文本。我尝试使用每个 comments/bookmarks 的开始和结束标记构建字典和数组,并尝试浏览 XML 树节点以获取文本,但我没有获取所有文本(只是first child,也就是第一个字)。
IDictionary<String, BookmarkStart> bookmarkMapS = new Dictionary<String, BookmarkStart>();
IDictionary<String, BookmarkEnd> bookmarkMapE = new Dictionary<String, BookmarkEnd>();
var _bkms = doc.MainDocumentPart.RootElement.Descendants<BookmarkStart>();
var _bkme = doc.MainDocumentPart.RootElement.Descendants<BookmarkEnd>();
foreach (BookmarkStart item in _bkms)
{
Run bookmarkText = item.NextSibling<Run>();
if (bookmarkText != null)
{
try
{
for (int i = 0; i < bookmarkText.ChildElements.Count(); i++)
{
Console.WriteLine(bookmarkText.ChildElements.ElementAt(i).InnerText);
}
}
catch (Exception)
{
}
}
}
可能有更好的方法,但这是我想到的。
List<OpenXmlElement> elems =new List<OpenXmlElement>(doc.MainDocumentPart.Document.Body.Descendants());
var crs=doc.MainDocumentPart.RootElement.Descendants<CommentRangeStart>();
var cre=doc.MainDocumentPart.RootElement.Descendants<CommentRangeEnd>();
var dic_cr=new Dictionary<CommentRangeStart, CommentRangeEnd>();
for (int i = 0; i < crs.Count(); i++)
{
dic_cr.Add(crs.ElementAt(i), cre.ElementAt(i));
}
for (int i = 0; i < elems.Count; i++)
if (elems.ElementAt(i).LocalName.Equals("t"))
if (isInsideAnyComment(dic_cr, elems.ElementAt(i)))
for (int j = 0; j < dic_cr.Count; j++)
if (isInsideAComment(dic_cr.ElementAt(j), elems.ElementAt(i)))
String text = ((Text)elems.ElementAt(i)).InnerText;
public bool isInsideAnyComment(IDictionary<CommentRangeStart, CommentRangeEnd> dic_cr, OpenXmlElement item)
{
foreach (var i in dic_cr)
if (item.IsAfter(i.Key) && item.IsBefore(i.Value))
return true;
return false;
}
public bool isInsideAComment(KeyValuePair<CommentRangeStart, CommentRangeEnd> dic_cr_elem, OpenXmlElement item)
{
if (item.IsAfter(dic_cr_elem.Key) && item.IsBefore(dic_cr_elem.Value))
return true;
return false;
}
我一直在尝试使用 OpenXML 在 .docx 文档中检索用户评论或添加书签的文本。我尝试使用每个 comments/bookmarks 的开始和结束标记构建字典和数组,并尝试浏览 XML 树节点以获取文本,但我没有获取所有文本(只是first child,也就是第一个字)。
IDictionary<String, BookmarkStart> bookmarkMapS = new Dictionary<String, BookmarkStart>();
IDictionary<String, BookmarkEnd> bookmarkMapE = new Dictionary<String, BookmarkEnd>();
var _bkms = doc.MainDocumentPart.RootElement.Descendants<BookmarkStart>();
var _bkme = doc.MainDocumentPart.RootElement.Descendants<BookmarkEnd>();
foreach (BookmarkStart item in _bkms)
{
Run bookmarkText = item.NextSibling<Run>();
if (bookmarkText != null)
{
try
{
for (int i = 0; i < bookmarkText.ChildElements.Count(); i++)
{
Console.WriteLine(bookmarkText.ChildElements.ElementAt(i).InnerText);
}
}
catch (Exception)
{
}
}
}
可能有更好的方法,但这是我想到的。
List<OpenXmlElement> elems =new List<OpenXmlElement>(doc.MainDocumentPart.Document.Body.Descendants());
var crs=doc.MainDocumentPart.RootElement.Descendants<CommentRangeStart>();
var cre=doc.MainDocumentPart.RootElement.Descendants<CommentRangeEnd>();
var dic_cr=new Dictionary<CommentRangeStart, CommentRangeEnd>();
for (int i = 0; i < crs.Count(); i++)
{
dic_cr.Add(crs.ElementAt(i), cre.ElementAt(i));
}
for (int i = 0; i < elems.Count; i++)
if (elems.ElementAt(i).LocalName.Equals("t"))
if (isInsideAnyComment(dic_cr, elems.ElementAt(i)))
for (int j = 0; j < dic_cr.Count; j++)
if (isInsideAComment(dic_cr.ElementAt(j), elems.ElementAt(i)))
String text = ((Text)elems.ElementAt(i)).InnerText;
public bool isInsideAnyComment(IDictionary<CommentRangeStart, CommentRangeEnd> dic_cr, OpenXmlElement item)
{
foreach (var i in dic_cr)
if (item.IsAfter(i.Key) && item.IsBefore(i.Value))
return true;
return false;
}
public bool isInsideAComment(KeyValuePair<CommentRangeStart, CommentRangeEnd> dic_cr_elem, OpenXmlElement item)
{
if (item.IsAfter(dic_cr_elem.Key) && item.IsBefore(dic_cr_elem.Value))
return true;
return false;
}