AnnotationContent.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ImTools;
  6. using PDF_Master.DataConvert;
  7. using PDF_Master.EventAggregators;
  8. using PDF_Master.Helper;
  9. using PDF_Master.Model.BOTA;
  10. using PDF_Master.ViewModels;
  11. using PDF_Master.ViewModels.BOTA;
  12. using Prism.Events;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel;
  16. using System.Diagnostics;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Annotations;
  22. using System.Windows.Controls;
  23. using System.Windows.Controls.Primitives;
  24. using System.Windows.Data;
  25. using System.Windows.Documents;
  26. using System.Windows.Forms;
  27. using System.Windows.Input;
  28. using System.Windows.Media;
  29. using System.Windows.Media.Imaging;
  30. using System.Windows.Navigation;
  31. using System.Windows.Shapes;
  32. using static Dropbox.Api.Paper.ListPaperDocsSortBy;
  33. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  34. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  35. using ListBox = System.Windows.Controls.ListBox;
  36. using MenuItem = System.Windows.Controls.MenuItem;
  37. using UserControl = System.Windows.Controls.UserControl;
  38. namespace PDF_Master.Views.BOTA
  39. {
  40. /// <summary>
  41. /// AnnotationContent.xaml 的交互逻辑
  42. /// </summary>
  43. public partial class AnnotationContent : UserControl
  44. {
  45. private AnnotationContentViewModel viewModel;
  46. private string unicode;
  47. public AnnotationContent(IEventAggregator eventAggregator)
  48. {
  49. InitializeComponent();
  50. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  51. viewModel = this.DataContext as AnnotationContentViewModel;
  52. if (AnnotationList.Items.Count < 0)
  53. {
  54. MenuExpandAll.IsEnabled = false;
  55. }
  56. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  57. eventAggregator.GetEvent<RefreshAnnotationEvent>().Subscribe(ExpandGroupHeader, e => e.Unicode == unicode);
  58. }
  59. private async void ExpandGroupHeader(RefreshAnnotationArgs obj)
  60. {
  61. if (obj.IsAll)
  62. {
  63. try
  64. {
  65. foreach (var item in AnnotationList.Items)
  66. {
  67. var groups = AnnotationList.Items.Groups;
  68. for (int i = 0; i < groups.Count; i++)
  69. {
  70. var group = groups[i] as CollectionViewGroup;
  71. if (group.Items.Contains(item))
  72. {
  73. var scroller = GetScrollHost(AnnotationList);
  74. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  75. //int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  76. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  77. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  78. if (g != null)
  79. {
  80. await Task.Delay(5);
  81. g.IsExpanded = true;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. catch { }
  88. }
  89. else
  90. {
  91. if (obj.annotHandlerArgs != null)
  92. {
  93. try
  94. {
  95. var groups = AnnotationList.Items.Groups;
  96. if (groups != null && groups.Count > 0)
  97. {
  98. for (int i = 0; i < groups.Count; i++)
  99. {
  100. var l = groups[i] as CollectionViewGroup;
  101. if (l.Items.Contains(obj.annotHandlerArgs))
  102. {
  103. var scroller = GetScrollHost(AnnotationList);
  104. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  105. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  106. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  107. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  108. if (g != null)
  109. {
  110. await Task.Delay(5);
  111. g.IsExpanded = true;
  112. }
  113. }
  114. }
  115. }
  116. if (obj.IsSelect && obj.annotHandlerArgs != null)
  117. {
  118. if (viewModel.ViewContentViewModel.IsMultiSelected == false)
  119. {
  120. if (AnnotationList.SelectedItems.IndexOf(obj.annotHandlerArgs) == -1)
  121. //if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
  122. {
  123. viewModel.IsModified = false;
  124. AnnotationList.SelectedItem = obj.annotHandlerArgs;
  125. }
  126. }
  127. }
  128. await Task.Delay(2);//不加延时 会有不滚动的现象
  129. AnnotationList.ScrollIntoView(obj.annotHandlerArgs);
  130. }
  131. catch { }
  132. }
  133. }
  134. }
  135. private void CleanSelectAll(CleanSelectAllArgs obj)
  136. {
  137. AnnotationList.SelectedIndex = -1;
  138. }
  139. private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  140. {
  141. if (e.ClickCount == 1)
  142. {
  143. //if (Mouse.LeftButton == e.ButtonState)
  144. //{
  145. // viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  146. //}
  147. if (Mouse.RightButton == e.ButtonState)
  148. {
  149. var pos = e.GetPosition(AnnotationList);
  150. var result = VisualTreeHelper.HitTest(AnnotationList, pos);
  151. if (result != null)
  152. {
  153. ListBoxItem myListBoxItem = sender as System.Windows.Controls.ListBoxItem;
  154. MenuItem copyText = myListBoxItem.ContextMenu.Items[0] as MenuItem;
  155. MenuItem import = myListBoxItem.ContextMenu.Items[1] as MenuItem;
  156. MenuItem export = myListBoxItem.ContextMenu.Items[2] as MenuItem;
  157. if (AnnotationList.SelectedItems.Count > 1)
  158. {
  159. import.IsEnabled = false;
  160. export.IsEnabled = true;
  161. export.Command = viewModel.ExportCommentsCommand;
  162. copyText.IsEnabled = false;
  163. }
  164. else
  165. {
  166. //直接右键菜单不需要选中Viewer中注释
  167. //viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  168. if (myListBoxItem.DataContext is AnnotationHandlerEventArgs annotation)
  169. {
  170. //文本、高亮、下划线、删除线、便签
  171. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  172. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  173. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  174. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  175. annotation.EventType == AnnotArgsType.AnnotSticky)
  176. {
  177. copyText.IsEnabled = true;
  178. }
  179. else
  180. {
  181. copyText.IsEnabled = false;
  182. }
  183. }
  184. import.IsEnabled = true;
  185. import.Command = viewModel.ImportCommentsCommand;
  186. export.IsEnabled = true;
  187. export.Command = viewModel.ExportCommentsCommand;
  188. }
  189. }
  190. //拦截响应选中事件
  191. e.Handled = true;
  192. }
  193. }
  194. }
  195. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  196. {
  197. if (Mouse.LeftButton == e.ButtonState)
  198. {
  199. viewModel.AddNotesCommand.Execute(sender);
  200. }
  201. }
  202. private async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
  203. {
  204. SetAllExpander(true);
  205. await Task.Delay(1);
  206. for (int i = 0; i < AnnotationList.Items.Count; i++)
  207. {
  208. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  209. ToggleButton button = GetExpandButton(item);
  210. if (button != null && button.Visibility == Visibility.Visible)
  211. {
  212. button.IsChecked = true;
  213. }
  214. }
  215. }
  216. private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
  217. {
  218. for (int i = 0; i < AnnotationList.Items.Count; i++)
  219. {
  220. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  221. ToggleButton button = GetExpandButton(item);
  222. if (button != null && button.Visibility == Visibility.Visible)
  223. {
  224. button.IsChecked = false;
  225. }
  226. }
  227. SetAllExpander(false);
  228. }
  229. private void SetAllExpander(bool isexpand)
  230. {
  231. AnnotationList.Dispatcher.Invoke(() =>
  232. {
  233. //开启虚拟化之后 全部展开和折叠会有问题
  234. var scroller = GetScrollHost(AnnotationList);
  235. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  236. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  237. for (int i = 0; i < count; i++)
  238. {
  239. var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  240. var g = CommonHelper.FindVisualChild<Expander>(item);
  241. if (g != null)
  242. g.IsExpanded = isexpand;
  243. }
  244. });
  245. }
  246. private ScrollViewer GetScrollHost(ListBox listBox)
  247. {
  248. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  249. {
  250. int s = VisualTreeHelper.GetChildrenCount(listBox);
  251. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  252. if (border != null)
  253. {
  254. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  255. }
  256. }
  257. return null;
  258. }
  259. private ToggleButton GetExpandButton(ListBoxItem item)
  260. {
  261. if (item == null) return null;
  262. Border border = VisualTreeHelper.GetChild(item, 0) as Border;
  263. var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
  264. return btn;
  265. }
  266. private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
  267. {
  268. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  269. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  270. v.GroupDescriptions.Clear();
  271. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  272. v.SortDescriptions.Clear();
  273. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  274. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  275. }
  276. private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
  277. {
  278. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  279. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  280. v.GroupDescriptions.Clear();
  281. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  282. v.SortDescriptions.Clear();
  283. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  284. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  285. }
  286. private void MenuPageSort_Click(object sender, RoutedEventArgs e)
  287. {
  288. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  289. v.GroupDescriptions.Clear();
  290. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  291. v.SortDescriptions.Clear();
  292. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  293. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  294. }
  295. private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
  296. {
  297. //没有权限解密时,拦截复制文本操作
  298. if (!viewModel.ViewContentViewModel.CheckPermissionBeforeOption())
  299. {
  300. return;
  301. }
  302. if (AnnotationList.SelectedItems.Count == 1)
  303. {
  304. if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
  305. {
  306. //文本、高亮、下划线、删除线、便签
  307. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  308. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  309. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  310. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  311. annotation.EventType == AnnotArgsType.AnnotSticky)
  312. {
  313. StringBuilder Copystr = new StringBuilder();
  314. if (annotation.MarkupContent != null)
  315. {
  316. Copystr.Append(annotation.MarkupContent);
  317. Copystr.Append(Environment.NewLine);
  318. }
  319. if (annotation.Content != null)
  320. {
  321. Copystr.Append(annotation.Content);
  322. }
  323. System.Windows.Clipboard.SetText(Copystr.ToString());
  324. //CustomControl.MessageBoxEx.Show("数据复制成功");
  325. }
  326. }
  327. }
  328. }
  329. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  330. {
  331. List<int> pagelist = new List<int>();
  332. if (AnnotationList.SelectedItems.Count > 1)
  333. {
  334. for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
  335. {
  336. AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
  337. pagelist.Add(AnnotationList.Items.IndexOf(annotation));
  338. }
  339. pagelist.Sort();
  340. for (int i = 0; i < pagelist.Count; i++)
  341. {
  342. AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  343. if (annotation == null)
  344. {
  345. continue;
  346. }
  347. viewModel.DeleteCommand.Execute(annotation);
  348. }
  349. }
  350. else
  351. {
  352. AnnotationHandlerEventArgs annotation = (AnnotationHandlerEventArgs)(sender as MenuItem).DataContext;
  353. if (annotation == null)
  354. {
  355. return;
  356. }
  357. viewModel.DeleteCommand.Execute(annotation);
  358. }
  359. }
  360. private void BtnMore_Initialized(object sender, EventArgs e)
  361. {
  362. BtnMore.ContextMenu = null;
  363. }
  364. private void BtnMore_Click(object sender, RoutedEventArgs e)
  365. {
  366. MenuMore.PlacementTarget = BtnMore;
  367. MenuMore.IsOpen = true;
  368. }
  369. private void AnnotationList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  370. {
  371. if (AnnotationList.SelectedItems.Count > 1)
  372. {
  373. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItems);
  374. }
  375. else if (AnnotationList.SelectedItems.Count == 1)
  376. {
  377. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItem);
  378. }
  379. }
  380. private void AnnotationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  381. {
  382. var a = AnnotationList.SelectedItems.Count;
  383. var a1 = (sender as ListBox).SelectedItems.Count;
  384. var a2 = (e.OriginalSource as ListBox).SelectedItems.Count;
  385. var a3 = (e.Source as ListBox).SelectedItems.Count;
  386. Trace.WriteLine("a:" + a + " a1:" + a1 + " a2:" + a2 + " a3:" + a3);
  387. //AnnotationList.ScrollIntoView(AnnotationList.Items[0]);
  388. if (a1 == 1)
  389. {
  390. //if(viewModel.ViewContentViewModel.IsPropertyOpen==false)
  391. if (viewModel.IsModified)
  392. {
  393. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItems[0]);
  394. }
  395. viewModel.IsModified = true;
  396. }
  397. else if (a1 > 1)
  398. {
  399. #region 阅读视图 多选
  400. Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
  401. List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
  402. foreach (var item in (sender as ListBox).SelectedItems)
  403. {
  404. if (item is AnnotationHandlerEventArgs annotation)
  405. {
  406. if (eventArgs.Contains(annotation) == false)
  407. {
  408. eventArgs.Add(annotation);
  409. }
  410. }
  411. }
  412. var result = eventArgs.GroupBy(d => d.PageIndex);
  413. foreach (var item in result)
  414. {
  415. if (selectedItemDics.ContainsKey(item.Key) == false)
  416. {
  417. var ai = item.Select(w => w.AnnotIndex).ToList();
  418. selectedItemDics.Add(item.Key, ai);
  419. }
  420. }
  421. #endregion 阅读视图 多选
  422. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(selectedItemDics);
  423. }
  424. }
  425. private void AnnotationList_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  426. {
  427. if (AnnotationList.SelectedItems == null)
  428. {
  429. return;
  430. }
  431. if (e.Key == Key.Escape)
  432. {
  433. AnnotationList.SelectedItems.Clear();
  434. }
  435. }
  436. private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  437. {
  438. var pos = e.GetPosition(this);
  439. var result = VisualTreeHelper.HitTest(this, pos);
  440. if (result != null)
  441. {
  442. //点击按钮和listboxitem时 不清空多选 其余区域取消多选
  443. var item = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  444. if (item != null)
  445. {
  446. return;
  447. }
  448. var btn = CommonHelper.FindVisualParent<System.Windows.Controls.Button>(result.VisualHit);
  449. if (btn != null)
  450. {
  451. return;
  452. }
  453. }
  454. }
  455. private void ListBox_MouseDown(object sender, MouseButtonEventArgs e)
  456. {
  457. AnnotationList.SelectedIndex = -1;
  458. }
  459. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  460. {
  461. var menu = sender as System.Windows.Controls.ContextMenu;
  462. if (menu != null)
  463. {
  464. (menu.Items[0] as MenuItem).Header = viewModel.T_CopyText;
  465. (menu.Items[1] as MenuItem).Header = viewModel.T_Import;
  466. (menu.Items[2] as MenuItem).Header = viewModel.T_Export;
  467. (menu.Items[3] as MenuItem).Header = viewModel.T_Delete;
  468. if (AnnotationList.SelectedItems.Count > 1)
  469. {
  470. (menu.Items[3] as MenuItem).Header = viewModel.T_DeleteAll;
  471. }
  472. }
  473. }
  474. }
  475. }