AnnotationContent.xaml.cs 24 KB

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