|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionOpenXML is a new standard used by all of Microsoft's new Office suites (Office 2007)! Because it is an open standard and based on XML, we can easily parse documents created with these products. The topic of this article will be how to parse a Word document (using XLINQ) and then render the document in a WPF control called the Parsing an OpenXML Document using XLINQSystem.IO.PackagingThe first problem is to extract or unpack the *.docx file into its containing *.xml files. Package package = Package.Open(filename);
Uri documentUri = new Uri("/word/document.xml", UriKind.Relative);
PackagePart documentPart = package.GetPart(documentUri);
XElement wordDoc = XElement.Load(new StreamReader(documentPart.GetStream()));
The snippet above opens a Word document, extracts the document.xml into an Using XLINQ to Get All the ParagraphsAll Word documents require the XNamespace w = http://schemas.openxmlformats.org/wordprocessingml/2006/main;
Now we can make a simple XLINQ query to get all the paragraphs (please note: to keep this simple, I am only looking at the var paragraphs = from p in wordDoc.Descendants(w + "p")
select p;
Next, we iterate over the collection of FlowDocumentSacha has a nice article on the basics of foreach (var p in paragraphs)
{
var style = 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();
Paragraph par = new Paragraph();
Run r = new Run(p.Value);
if (font != null)
{
FontFamilyConverter converter = new FontFamilyConverter();
r.FontFamily = (FontFamily)converter.ConvertFrom(font.Value);
}
if (size != null)
{
r.FontSize = double.Parse(size.Value);
}
par.Inlines.Add(r);
flowDoc.Blocks.Add(par);
}
For each And that is it! All that is left now is to find a cool way to extend the normal To load a Word document into a flow document, you first create a <FlowDocumentPageViewer x:Name="flowDocViewer" Zoom="80" />
And load the Word document... FlowDocument flowDoc = new FlowDocument();
flowDoc.loadWordML("DisciplesBios.docx");
flowDocViewer.Document = flowDoc;
I created a simple Word document with all the WPF Disciples bios. Here is how it looks in a
Simple and easy... If you like the article, please vote for it and also visit my blog: http://dotnet.org.za/rudi History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||