AnnotationsControl.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using Microsoft.Win32;
  7. using System.Windows.Controls.Primitives;
  8. using ComPDFKitViewer.PdfViewer;
  9. using ComPDFKitViewer.AnnotEvent;
  10. namespace ComPDFKitDemo.BOTA
  11. {
  12. public partial class AnnotationsControl : UserControl
  13. {
  14. public CPDFViewer pdfViewer;
  15. private bool selectChangeEnable = true;
  16. public AnnotationsControl()
  17. {
  18. InitializeComponent();
  19. ContextMenu popMenu = new ContextMenu();
  20. MenuItem copyItem = new MenuItem();
  21. copyItem.Header = "Copy";
  22. copyItem.Click += CopyItem_Click;
  23. popMenu.Items.Add(copyItem);
  24. MenuItem delItem = new MenuItem();
  25. delItem.Header = "Delete";
  26. delItem.Click += DelItem_Click;
  27. popMenu.Items.Add(delItem);
  28. this.ContextMenu = popMenu;
  29. }
  30. private void DelItem_Click(object sender, RoutedEventArgs e)
  31. {
  32. if (AnnotListBox.SelectedItem != null && pdfViewer != null)
  33. {
  34. AnnotCommentControl AnnotControl = (AnnotCommentControl)AnnotListBox.SelectedItem;
  35. AnnotHandlerEventArgs comment = AnnotControl.GetBindData();
  36. if (comment != null)
  37. {
  38. pdfViewer.RemovePageAnnot(comment.PageIndex, comment.AnnotIndex);
  39. }
  40. AnnotListBox.Items.Remove(AnnotControl);
  41. }
  42. }
  43. private void CopyItem_Click(object sender, RoutedEventArgs e)
  44. {
  45. if (AnnotListBox.SelectedItem != null && pdfViewer != null)
  46. {
  47. if(!pdfViewer.ExecCopyCommand())
  48. {
  49. Debug.WriteLine("Copy Faild");
  50. }
  51. }
  52. }
  53. private void ExportAnnots_Click(object sender, RoutedEventArgs e)
  54. {
  55. ToggleButton exportBtn = sender as ToggleButton;
  56. if (exportBtn != null)
  57. {
  58. exportBtn.IsEnabled = false;
  59. SaveFileDialog dlg = new SaveFileDialog();
  60. dlg.Filter = "PDF|*.xfdf";
  61. dlg.DefaultExt = ".xfdf";
  62. dlg.FileName = pdfViewer.Document.FileName;
  63. if (dlg.ShowDialog() == true)
  64. {
  65. string fileName = dlg.FileName;
  66. }
  67. exportBtn.IsEnabled = true;
  68. }
  69. }
  70. private void ImportAnnots_Click(object sender, RoutedEventArgs e)
  71. {
  72. ToggleButton importBtn = sender as ToggleButton;
  73. if (importBtn != null)
  74. {
  75. importBtn.IsEnabled = false;
  76. OpenFileDialog dlg = new OpenFileDialog();
  77. dlg.Filter = "PDF|*.xfdf";
  78. dlg.DefaultExt = ".xfdf";
  79. dlg.FileName = pdfViewer.Document.FileName;
  80. if (dlg.ShowDialog() == true)
  81. {
  82. }
  83. importBtn.IsEnabled = true;
  84. }
  85. }
  86. public void LoadAnnots()
  87. {
  88. AnnotListBox.Items.Clear();
  89. if (pdfViewer != null && pdfViewer.Document != null)
  90. {
  91. int pageCount = pdfViewer.Document.PageCount;
  92. Task job = new Task(() =>
  93. {
  94. for (int i = 0; i < pageCount; i++)
  95. {
  96. List<AnnotHandlerEventArgs> commentsList = pdfViewer.GetAnnotCommentList(i, pdfViewer.Document);
  97. if (commentsList != null)
  98. {
  99. Dispatcher.Invoke(() =>
  100. {
  101. foreach (AnnotHandlerEventArgs comment in commentsList)
  102. {
  103. AnnotCommentControl AnnotControl = new AnnotCommentControl();
  104. AnnotControl.BindData(comment);
  105. AnnotListBox.Items.Add(AnnotControl);
  106. }
  107. });
  108. }
  109. }
  110. });
  111. job.Start();
  112. }
  113. }
  114. private void AnnotListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  115. {
  116. if(selectChangeEnable)
  117. {
  118. if (AnnotListBox.SelectedItems.Count > 0 && pdfViewer != null)
  119. {
  120. Dictionary<int, List<int>> selectAnnotsDict = new Dictionary<int, List<int>>();
  121. foreach (AnnotCommentControl AnnotControl in AnnotListBox.SelectedItems)
  122. {
  123. AnnotHandlerEventArgs comment = AnnotControl.GetBindData();
  124. if (comment != null)
  125. {
  126. if (selectAnnotsDict.ContainsKey(comment.PageIndex) == false)
  127. {
  128. selectAnnotsDict[comment.PageIndex] = new List<int>();
  129. }
  130. selectAnnotsDict[comment.PageIndex].Add(comment.AnnotIndex);
  131. }
  132. }
  133. pdfViewer.SelectAnnotation(selectAnnotsDict);
  134. }
  135. }
  136. AnnotListBox.ScrollIntoView(AnnotListBox.SelectedItem);
  137. }
  138. public void SetSelectAnnot(Dictionary<int, List<int>> selectAnnotDicts)
  139. {
  140. int selectIndex = -1;
  141. selectChangeEnable = false;
  142. AnnotListBox.SelectedItems.Clear();
  143. foreach (AnnotCommentControl Item in AnnotListBox.Items)
  144. {
  145. AnnotHandlerEventArgs commentData = Item.GetBindData();
  146. selectIndex++;
  147. if(selectAnnotDicts.ContainsKey(commentData.PageIndex) && selectAnnotDicts[commentData.PageIndex].Contains(commentData.AnnotIndex))
  148. {
  149. AnnotListBox.SelectedItems.Add(Item);
  150. }
  151. }
  152. selectChangeEnable = true;
  153. }
  154. }
  155. }