标签:
使用OpenXML SDK实现按页读取内容:
以下内容采用WPF+RichTextBox+FlowDocment展示,测试文档共3页内容,图中展示的是获取第一页内容,效果图后附上代码;效果图如下:
1 /// <summary> 2 /// 按页加载Word 3 /// </summary> 4 /// <param name="flowDoc"></param> 5 /// <param name="filename"></param> 6 /// <param name="page"></param> 7 public static void loadWordML(this FlowDocument flowDoc, string filename,int page) 8 { 9 XElement wordDoc = null; 10 try 11 { 12 Package package = Package.Open(filename); 13 Uri documentUri = new Uri("/word/document.xml", UriKind.Relative); 14 PackagePart documentPart = package.GetPart(documentUri); 15 wordDoc = XElement.Load(new StreamReader(documentPart.GetStream())); 16 } 17 catch (Exception) 18 { 19 flowDoc.Blocks.Add(new System.Windows.Documents.Paragraph(new System.Windows.Documents.Run("File not found or not in correct format (Word 2007)"))); 20 return; 21 } 22 23 XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; 24 25 var paragraphs = from p in wordDoc.Descendants(w + "p") 26 select p; 27 int pagecount=0; 28 foreach (var p in paragraphs) 29 { 30 var PageTag = from s in p.Descendants(w + "lastRenderedPageBreak") select s; 31 if (PageTag.Count()>0) 32 { 33 pagecount++; 34 if (pagecount == page) 35 { 36 break; 37 } 38 } 39 var style = from s in p.Descendants(w + "pPr") 40 select s; 41 42 var font = (from f in style.Descendants(w + "rFonts") 43 select f.FirstAttribute).FirstOrDefault(); 44 var size = (from s in style.Descendants(w + "sz") 45 select s.FirstAttribute).FirstOrDefault(); 46 47 System.Windows.Documents.Paragraph par = new System.Windows.Documents.Paragraph(); 48 System.Windows.Documents.Run r = new System.Windows.Documents.Run(p.Value); 49 if (font != null) 50 { 51 FontFamilyConverter converter = new FontFamilyConverter(); 52 r.FontFamily = (System.Windows.Media.FontFamily)converter.ConvertFrom(font.Value); 53 } 54 if (size != null) 55 { 56 r.FontSize = double.Parse(size.Value); 57 } 58 par.Inlines.Add(r); 59 60 flowDoc.Blocks.Add(par); 61 } 62 }
WPF RichTextBox Flowdocment如何实现自动分页?(二)
标签:
原文地址:http://www.cnblogs.com/A1AA/p/4991224.html