CPDFBookmarkControl.xaml.cs 6.3 KB

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