CPDFBookmarkControl.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.Controls.PDFControlUI;
  3. using ComPDFKitViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  10. namespace ComPDFKit.Controls.PDFControl
  11. {
  12. public partial class CPDFBookmarkControl : UserControl
  13. {
  14. /// <summary>
  15. /// PDFViewer
  16. /// </summary>
  17. private PDFViewControl ViewControl;
  18. public CPDFBookmarkControl()
  19. {
  20. InitializeComponent();
  21. Loaded += CPDFBookmarkControl_Loaded;
  22. Unloaded += CPDFBookmarkControl_Unloaded;
  23. }
  24. private void CPDFBookmarkControl_Loaded(object sender, RoutedEventArgs e)
  25. {
  26. BookmarkAddUI.BookmarkAddEvent -= BookmarkAddUI_BookmarkAddEvent;
  27. BookmarkAddUI.BookmarkInputExpandEvent -= BookmarkAddUI_BookmarkInputExpandEvent;
  28. BookmarkResultUI.SelectionChanged -= BookmarkResultUI_SelectionChanged;
  29. BookmarkResultUI.BookmarkDelete -= BookmarkResultUI_BookmarkDelete;
  30. BookmarkResultUI.BookmarkClicked -= BookmarkResultUI_BookmarkClicked;
  31. BookmarkResultUI.BookmarkEdit -= BookmarkResultUI_BookmarkEdit;
  32. BookmarkAddUI.BookmarkAddEvent += BookmarkAddUI_BookmarkAddEvent;
  33. BookmarkAddUI.BookmarkInputExpandEvent += BookmarkAddUI_BookmarkInputExpandEvent;
  34. BookmarkResultUI.SelectionChanged += BookmarkResultUI_SelectionChanged;
  35. BookmarkResultUI.BookmarkDelete += BookmarkResultUI_BookmarkDelete;
  36. BookmarkResultUI.BookmarkClicked += BookmarkResultUI_BookmarkClicked;
  37. BookmarkResultUI.BookmarkEdit += BookmarkResultUI_BookmarkEdit;
  38. }
  39. private void CPDFBookmarkControl_Unloaded(object sender, RoutedEventArgs e)
  40. {
  41. BookmarkAddUI.BookmarkAddEvent -= BookmarkAddUI_BookmarkAddEvent;
  42. BookmarkAddUI.BookmarkInputExpandEvent -= BookmarkAddUI_BookmarkInputExpandEvent;
  43. BookmarkResultUI.SelectionChanged -= BookmarkResultUI_SelectionChanged;
  44. BookmarkResultUI.BookmarkDelete -= BookmarkResultUI_BookmarkDelete;
  45. BookmarkResultUI.BookmarkClicked -= BookmarkResultUI_BookmarkClicked;
  46. BookmarkResultUI.BookmarkEdit -= BookmarkResultUI_BookmarkEdit;
  47. }
  48. private void BookmarkResultUI_BookmarkEdit(object sender, BookmarkChangeData e)
  49. {
  50. BookmarkAddUI.HideInputUI(false);
  51. BookmarkAddUI.SetBookmarkChangeData(e);
  52. }
  53. private void BookmarkAddUI_BookmarkInputExpandEvent(object sender, EventArgs e)
  54. {
  55. if(ViewControl!=null && ViewControl.PDFViewTool!=null)
  56. {
  57. CPDFViewer pdfViewer=ViewControl.PDFViewTool.GetCPDFViewer();
  58. if(pdfViewer!=null && pdfViewer.CurrentRenderFrame!=null)
  59. {
  60. BookmarkAddUI.SetBookmarkChangeData(new BookmarkChangeData()
  61. {
  62. PageIndex = pdfViewer.CurrentRenderFrame.PageIndex,
  63. });
  64. }
  65. }
  66. }
  67. private void BookmarkResultUI_BookmarkClicked(object sender, int e)
  68. {
  69. GotoBookmarkPage(e);
  70. }
  71. private void BookmarkResultUI_BookmarkDelete(object sender, BookmarkChangeData e)
  72. {
  73. if(ViewControl!=null && ViewControl.PDFViewTool!=null)
  74. {
  75. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  76. CPDFDocument pdfDoc = pdfViewer?.GetDocument();
  77. if(pdfDoc!=null)
  78. {
  79. pdfDoc.RemoveBookmark(e.PageIndex);
  80. }
  81. }
  82. }
  83. private void BookmarkResultUI_SelectionChanged(object sender, int e)
  84. {
  85. if (e >= 0)
  86. {
  87. BindBookmarkResult bindResult = BookmarkResultUI.GetItem(e);
  88. if (bindResult != null)
  89. {
  90. GotoBookmarkPage(bindResult.PageIndex);
  91. }
  92. }
  93. }
  94. private void BookmarkAddUI_BookmarkAddEvent(object sender, BookmarkChangeData newData)
  95. {
  96. CPDFDocument pdfDoc = null;
  97. if (ViewControl != null && ViewControl.PDFViewTool != null)
  98. {
  99. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  100. pdfDoc = pdfViewer?.GetDocument();
  101. }
  102. if (pdfDoc == null || newData == null)
  103. {
  104. return;
  105. }
  106. if (newData.PageIndex >= 0 && newData.PageIndex < pdfDoc.PageCount)
  107. {
  108. if (string.IsNullOrEmpty(newData.NewTitle) == false && string.IsNullOrEmpty(newData.BookmarkTitle))
  109. {
  110. bool addState = pdfDoc.AddBookmark(new CPDFBookmark()
  111. {
  112. PageIndex = newData.PageIndex,
  113. Title = newData.NewTitle
  114. });
  115. if (addState)
  116. {
  117. LoadBookmark();
  118. }
  119. else
  120. {
  121. MessageBox.Show("Bookmark existed, add failed");
  122. }
  123. return;
  124. }
  125. if (!string.IsNullOrEmpty(newData.NewTitle) && !string.IsNullOrEmpty(newData.BookmarkTitle))
  126. {
  127. pdfDoc.EditBookmark(newData.PageIndex, newData.NewTitle);
  128. BookmarkBindData bindUiData = newData.BindData as BookmarkBindData;
  129. if (bindUiData != null)
  130. {
  131. bindUiData.BindProperty.BookmarkTitle = newData.NewTitle;
  132. }
  133. }
  134. }
  135. }
  136. private void GotoBookmarkPage(int pageIndex)
  137. {
  138. if (ViewControl != null && ViewControl.PDFViewTool != null)
  139. {
  140. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  141. if (pageIndex >= 0)
  142. {
  143. pdfViewer.GoToPage(pageIndex,new Point(0,0));
  144. }
  145. }
  146. }
  147. public void InitWithPDFViewer(PDFViewControl viewControl)
  148. {
  149. ViewControl = viewControl;
  150. }
  151. public void LoadBookmark()
  152. {
  153. CPDFDocument pdfDoc = null;
  154. if (ViewControl != null && ViewControl.PDFViewTool != null)
  155. {
  156. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  157. pdfDoc = pdfViewer?.GetDocument();
  158. }
  159. if (pdfDoc == null)
  160. {
  161. return;
  162. }
  163. List<CPDFBookmark> bookmarkList = pdfDoc.GetBookmarkList();
  164. List<BindBookmarkResult> bindBookmarkList = new List<BindBookmarkResult>();
  165. if (bookmarkList != null && bookmarkList.Count>0)
  166. {
  167. foreach (CPDFBookmark bookmark in bookmarkList.AsEnumerable().OrderBy(x=>x.PageIndex))
  168. {
  169. bindBookmarkList.Add(new BindBookmarkResult()
  170. {
  171. PageIndex=bookmark.PageIndex,
  172. BookmarkTitle=bookmark.Title
  173. });
  174. }
  175. }
  176. BookmarkResultUI?.SetBookmarkResult(bindBookmarkList);
  177. }
  178. }
  179. }