CPDFOutlineControl.xaml.cs 5.4 KB

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