CPDFOutlineControl.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFDocument.Action;
  3. using ComPDFKit.Tool;
  4. using Compdfkit_Tools.PDFControlUI;
  5. using ComPDFKitViewer;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Diagnostics;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Xml.Linq;
  13. namespace Compdfkit_Tools.PDFControl
  14. {
  15. public partial class CPDFOutlineControl : UserControl
  16. {
  17. PDFViewControl ViewControl;
  18. public ObservableCollection<CPDFOutlineNode> OutlineList
  19. {
  20. set; get;
  21. }
  22. public CPDFOutlineControl()
  23. {
  24. InitializeComponent();
  25. Loaded += CPDFOutlineControl_Loaded;
  26. }
  27. private void CPDFOutlineControl_Loaded(object sender, RoutedEventArgs e)
  28. {
  29. CPDFOutlineUI.OutlineSelectionChanged -= CPDFOutlineUI_OutlineSelectionChanged;
  30. CPDFOutlineUI.OutlineSelectionChanged += CPDFOutlineUI_OutlineSelectionChanged;
  31. }
  32. private void CPDFOutlineUI_OutlineSelectionChanged(object sender, object e)
  33. {
  34. try
  35. {
  36. TreeViewItem new_item = (TreeViewItem)e;
  37. CPDFOutline outline = (CPDFOutline)new_item.Tag;
  38. CPDFAction action = outline.GetAction();
  39. if (action != null && action.ActionType != C_ACTION_TYPE.ACTION_TYPE_UNKNOWN)
  40. {
  41. ViewControl.PDFViewTool.ActionProcess(action);
  42. }
  43. else
  44. {
  45. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  46. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  47. CPDFDestination dest = outline.GetDestination(pdfDoc);
  48. if (dest != null)
  49. {
  50. pdfViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, dest.Position_Y));
  51. }
  52. }
  53. }
  54. catch(Exception ex)
  55. {
  56. }
  57. }
  58. private int GetIndexFromParent(List<CPDFOutline> parentlist, CPDFOutline outline)
  59. {
  60. for (int i = 0; i < parentlist.Count; i++)
  61. {
  62. if (parentlist[i] == outline)
  63. {
  64. return i;
  65. }
  66. }
  67. return -1;
  68. }
  69. public CPDFOutlineNode FindOutlineFromDto(ObservableCollection<CPDFOutlineNode> list, CPDFOutline outline)
  70. {
  71. foreach (CPDFOutlineNode item in list)
  72. {
  73. CPDFOutline parent = outline.GetParent();
  74. CPDFOutline PARENT = item.PDFOutline.GetParent();
  75. item.CurrentNodeName = outline.Title;
  76. int i = GetIndexFromParent(parent.ChildList, outline);
  77. int I = GetIndexFromParent(PARENT.ChildList, item.PDFOutline);
  78. if (item.PDFOutline.Title == outline.Title && i == I && outline.Level == item.PDFOutline.Level && ((parent.Title == PARENT.Title) || (parent.Title == null && PARENT.Title == null)))
  79. {
  80. return item;
  81. }
  82. else if (item.ChildrenNodeList.Count > 0)
  83. {
  84. CPDFOutlineNode retdto = FindOutlineFromDto(item.ChildrenNodeList, outline);
  85. if (retdto != null)
  86. {
  87. return retdto;
  88. }
  89. }
  90. }
  91. return null;
  92. }
  93. private CPDFOutlineNode ConvertCPDFToOutlineNode(CPDFOutline outline, CPDFOutlineNode parent)
  94. {
  95. CPDFOutlineNode node = new CPDFOutlineNode();
  96. if (outline != null)
  97. {
  98. node.PDFOutline = outline;
  99. if (parent != null)
  100. {
  101. node.ParentNode = parent;
  102. }
  103. if (OutlineList != null && OutlineList.Count > 0)
  104. {
  105. CPDFOutlineNode oldnode = FindOutlineFromDto(OutlineList, node.PDFOutline);
  106. }
  107. node.ChildrenNodeList = new ObservableCollection<CPDFOutlineNode>();
  108. if (outline.ChildList.Count > 0)
  109. {
  110. foreach (CPDFOutline item in outline.ChildList)
  111. {
  112. node.ChildrenNodeList.Add(ConvertCPDFToOutlineNode(item, node));
  113. }
  114. }
  115. }
  116. return node;
  117. }
  118. public ObservableCollection<CPDFOutlineNode> GetOutlineViewDataSource()
  119. {
  120. ObservableCollection<CPDFOutlineNode> data = new ObservableCollection<CPDFOutlineNode>();
  121. if (ViewControl == null && ViewControl.PDFViewTool == null)
  122. {
  123. return data;
  124. }
  125. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  126. CPDFDocument pdfDoc = pdfViewer?.GetDocument();
  127. if(pdfDoc!=null)
  128. {
  129. List<CPDFOutline> datasource = pdfDoc.GetOutlineList();
  130. foreach (CPDFOutline item in datasource)
  131. {
  132. CPDFOutlineNode dto = ConvertCPDFToOutlineNode(item, null);
  133. if (dto != null)
  134. {
  135. data.Add(dto);
  136. }
  137. }
  138. }
  139. return data;
  140. }
  141. public void InitWithPDFViewer(PDFViewControl viewControl)
  142. {
  143. this.ViewControl = viewControl;
  144. if(ViewControl != null && viewControl.PDFViewTool!=null)
  145. {
  146. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  147. CPDFDocument pdfDoc=pdfViewer?.GetDocument();
  148. if(pdfDoc!=null)
  149. {
  150. List<CPDFOutline> outlineList = pdfDoc.GetOutlineList();
  151. CPDFOutlineUI.SetOutlineTree(outlineList);
  152. }
  153. }
  154. }
  155. }
  156. }