private static void AddStructure(IXpsFixedDocumentReader fixedDocumentReader, IXpsFixedDocumentWriter fixedDocumentWriter) { XpsStructure Structure = fixedDocumentReader.DocumentStructure; if (Structure != null) { XpsStructure myStructure = fixedDocumentWriter.AddDocumentStructure(); using (Stream destStream = myStructure.GetStream()) { using (Stream sourceStream = Structure.GetStream()) { CopyStream(sourceStream, destStream); myStructure.Commit(); } } } }
string[] pages = null; // 用于存放目录文档各节点OutlineTarget值,页码信息 // 读取导航目录 private void ReadDoc(XpsDocument xpsDoc) { IXpsFixedDocumentSequenceReader docSeq = xpsDoc.FixedDocumentSequenceReader; IXpsFixedDocumentReader docReader = docSeq.FixedDocuments[0]; XpsStructure xpsStructure = docReader.DocumentStructure; Stream stream = xpsStructure.GetStream(); XmlDocument doc = new XmlDocument(); doc.Load(stream); //获取节点列表 XmlNodeList nodeList = doc.ChildNodes.Item(0).FirstChild.FirstChild.ChildNodes; if (nodeList.Count <= 0)//判断是否存在目录节点 { //tvTree.Visibility = System.Windows.Visibility.Hidden; tvTree.Items.Add(new TreeViewItem { Header = "没有导航目录" }); return; } tvTree.Visibility = System.Windows.Visibility.Visible; array = new int[nodeList.Count]; array1 = new string[nodeList.Count]; arrayName = new string[nodeList.Count]; pages = new string[nodeList.Count]; for (int i = 0; i < nodeList.Count; i++) { array[i] = Convert.ToInt32(nodeList[i].Attributes["OutlineLevel"].Value); array1[i] = nodeList[i].Attributes["OutlineLevel"].Value.ToString(); arrayName[i] = nodeList[i].Attributes["Description"].Value.ToString(); pages[i] = nodeList[i].Attributes["OutlineTarget"].Value.ToString(); } for (int i = 0; i < array.Length - 1; i++) { //对array进行转换组装成可读的树形结构,通过ASCII值进行增加、转换 array1[0] = "A"; if (array[i + 1] - array[i] == 1) { array1[i + 1] = array1[i] + 'A'; } if (array[i + 1] == array[i]) { char s = Convert.ToChar(array1[i].Substring((array1[i].Length - 1), 1)); array1[i + 1] = array1[i].Substring(0, array1[i].Length - 1) + (char)(s + 1); } if (array[i + 1] < array[i]) { int m = array[i + 1]; char s = Convert.ToChar(array1[i].Substring(0, m).Substring(m - 1, 1)); array1[i + 1] = array1[i].Substring(0, m - 1) + (char)(s + 1); } } //添加一个节点作为根节点 TreeViewItem parent = new TreeViewItem(); TreeViewItem parent1 = null; parent.Header = "目录导航"; Boolean flag = false; for (int i = 0; i < array.Length; i++) { if (array[i] == 1) { flag = true; } if (flag) //如果找到实际根节点,加载树 { parent1 = new TreeViewItem(); parent1.Header = arrayName[i]; parent1.Tag = array1[i]; parent.Items.Add(parent1); parent.IsExpanded = true; parent1.IsExpanded = true; FillTree(parent1, array1, arrayName); flag = false; } } tvTree.Items.Clear(); tvTree.Items.Add(parent); }