CPDFBookmarkControl.xaml.cs 6.0 KB

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