public void OpenDocxFile(string fileName) { XElement wordDoc = null; try { Package package = Package.Open(fileName); Uri documentUri = new Uri("/word/document.xml", UriKind.Relative); PackagePart documentPart = package.GetPart(documentUri); wordDoc = XElement.Load(new StreamReader(documentPart.GetStream())); } catch (Exception) { this.rtbDocument.Document.Blocks.Add(new Paragraph(new Run("Cannot open file!"))); return; } XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; var paragraphs = from p in wordDoc.Descendants(w + "p") select p; foreach (var p in paragraphs) { var style = from s in p.Descendants(w + "rPr") select s; var parStyle = from s in p.Descendants(w + "pPr") select s; var font = (from f in style.Descendants(w + "rFonts") select f.FirstAttribute).FirstOrDefault(); var size = (from s in style.Descendants(w + "sz") select s.FirstAttribute).FirstOrDefault(); var rgbColor = (from c in style.Descendants(w + "color") select c.FirstAttribute).FirstOrDefault(); var bold = (from b in style.Descendants(w + "b") select b).FirstOrDefault(); var italic = (from i in style.Descendants(w + "i") select i).FirstOrDefault(); var underline = (from u in style.Descendants(w + "u") select u).FirstOrDefault(); var alignment = (from a in parStyle.Descendants(w + "jc") select a).FirstOrDefault(); var textElements = from t in p.Descendants(w + "t") select t; var list = (from l in parStyle.Descendants(w + "numPr") select l).FirstOrDefault(); StringBuilder text = new StringBuilder(); foreach (var element in textElements) { text.Append(element.Value); } Paragraph par = new Paragraph(); Run run = new Run(text.ToString()); List items = new List(); if (list != null) { //List items = new List(); items.ListItems.Add(new ListItem(par)); var markerStyleElement = (from l in list.Descendants(w + "numId") select l).FirstOrDefault(); TextMarkerStyle markerStyle = new TextMarkerStyle(); if(markerStyleElement != null) { int value = int.Parse(markerStyleElement.Attribute(w + "val").Value); switch (value) { case 1: markerStyle = TextMarkerStyle.Disc; break; case 2: markerStyle = TextMarkerStyle.Decimal; break; } } items.MarkerStyle = markerStyle; } if(font != null) { FontFamilyConverter converter = new FontFamilyConverter(); run.FontFamily = (FontFamily) converter.ConvertFrom(font.Value); } if(size != null) { run.FontSize = double.Parse(size.Value); } if(rgbColor != null) { Color color = ConvertRgbToColor(rgbColor.Value); run.Foreground = new SolidColorBrush(color); } if(bold != null) { if (bold.Attribute(w + "val") != null) { if (bold.Attribute(w + "val").Value == "off") { run.FontWeight = FontWeights.Normal; } else { run.FontWeight = FontWeights.Bold; } } else { run.FontWeight = FontWeights.Bold; } } if(italic != null) { if (italic.Attribute(w + "val") != null) { if (italic.Attribute(w + "val").Value == "off") { run.FontStyle = FontStyles.Normal; } else { run.FontStyle = FontStyles.Italic; } } else { run.FontStyle = FontStyles.Italic; } } if(underline != null) { run.TextDecorations.Add(TextDecorations.Underline); } if(alignment != null) { TextAlignment textAlignment = new TextAlignment(); string value = alignment.Attribute(w + "val").Value; switch (value) { case "left": textAlignment = TextAlignment.Left; break; case "center": textAlignment = TextAlignment.Center; break; case "right": textAlignment = TextAlignment.Right; break; case "justify": textAlignment = TextAlignment.Justify; break; } par.TextAlignment = textAlignment; } else { par.TextAlignment = TextAlignment.Left; } par.Inlines.Add(run); if(list != null) { this.rtbDocument.Document.Blocks.Add(items); } else { this.rtbDocument.Document.Blocks.Add(par); } } }