CPDFOutlineControl.xaml.cs 5.4 KB

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