i've been trying while retrieve text commented or bookmarked user, in .docx document, using openxml. tried building dictionaries , arrays start , end tag of each comments/bookmarks, , tried navigate through xml tree nodes, obtain text i'm not obtaining of (just first child, first word).
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 { (int = 0; < bookmarktext.childelements.count(); i++) { console.writeline(bookmarktext.childelements.elementat(i).innertext); } } catch (exception) { } } }
there better way this, came with.
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>(); (int = 0; < crs.count(); i++) { dic_cr.add(crs.elementat(i), cre.elementat(i)); } (int = 0; < elems.count; i++) if (elems.elementat(i).localname.equals("t")) if (isinsideanycomment(dic_cr, elems.elementat(i))) (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 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; }
Comments
Post a Comment