CPDFBookmarkControl.xaml.cs 6.8 KB

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