AnnotationContent.xaml.cs 22 KB

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