CPDFOutlineControl.xaml.cs 6.4 KB

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