Message-ID: <259760271.9807.1711703611963.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_9806_812908355.1711703611963" ------=_Part_9806_812908355.1711703611963 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
In the course of debugging, it can often be difficult to visualize the s= tructure of documents as they are manipulated by WordApplication.
Our documentation has an overview of how WordApplication represents a Word doc= ument and how to insert elements using WordApplicationThis post provides a g= eneralized function for displaying the structure of a document.
The code= below provides a function "UnfoldDocument" which will produce a = formatted text representation of the element hierarchy in a given Document.= Output assumes a monospaced font and is suitable for writing to a plain-te= xt file, printing to a console or inserting in a tag on an ASP.Net page.To make use of this function, pass it a reference to a document object a= s shown:
WordApplication wa =3D new WordApplication(); Document doc =3D wa.Open(chosefile.FileContent); Console.WriteLine(UnfoldDocument(doc));=20=20
public string UnfoldDocument(Document d) { StringBuilder tree =3D new StringBuilder(); RUnfold(d, 0, new List(), tree); return tree.ToString(); } // Helper method that recursively traverses the document tree private void RUnfold(Element e, int tab, List line, StringBuilder ret) { // Handle "leaf element" classes and output meaningful text r= epresentations if (e.ElementType =3D=3D Element.Type.Hyperlink) { string link =3D ((Hyperlink)e).GetUrlString(); ret.AppendFormat("Hyperlink: '{0}' -> ({1})", e.Text, = link); return; } else if (e.ElementType =3D=3D Element.Type.MergeField) { string fname =3D ((MergeField)e).GetFieldName(); ret.AppendFormat("MergeField: '{0}' -> ({1})", e.Text,= fname); return; } else if (e.ElementType =3D=3D Element.Type.InlineImage) { InlineImage image =3D (InlineImage)e; ret.AppendFormat("InlineImage: {0} x {1}", image.Width, i= mage.Height); return; } else if (e.ElementType =3D=3D Element.Type.CharacterRun) { // Escape control sequences and wrap CharacterRun // output to 60 columns for readability purposes string text =3D e.Text; text =3D text.Replace("\r", "\\r"); text =3D text.Replace("\n", "\\n"); text =3D text.Replace("\t", "\\t"); ret.Append("CharacterRun: '"); for (int x =3D 0; x < text.Length; x++) { if ((x % 61) =3D=3D 60) { ret.AppendFormat("\n{0}",tabs(tab,line)); ret.Append(' ', 14); } ret.Append(text[x]); } ret.Append('\''); return; } // Recursively traverse the children of non-leaf elements ret.Append(e.ElementType.ToString()); line.Add(true); for (int x =3D 0; x < e.Children.Length; x++) { Element child =3D e.Children[x]; ret.AppendFormat("\n{0}|\n{0}|--", tabs(tab, line)); if (x =3D=3D e.Children.Length - 1) { line[tab] =3D false; } RUnfold(child, tab + 1, line, ret); } line[tab] =3D true; return; } // Helper method that produces appropriate tabbing for each line of output public string tabs(int n, List line) { char[] ret =3D new char[n*3]; for (int x =3D 0; x < (n * 3); x++) { if ((line[x/3])&&(x%3 =3D=3D 0)) { ret[x] =3D '|'; } else { ret[x] =3D ' '; } } return new String(ret); }=20