BookmarkContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using ImTools;
  4. using PDF_Office.CustomControl;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.PageEdit;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Controls.Primitives;
  20. using System.Windows.Forms;
  21. using System.Windows.Input;
  22. using System.Windows.Markup;
  23. using System.Windows.Media;
  24. using static Dropbox.Api.Files.SearchMatchType;
  25. using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum;
  26. using static System.Net.Mime.MediaTypeNames;
  27. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  28. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  29. using ListViewItem = System.Windows.Controls.ListViewItem;
  30. using TextBox = System.Windows.Controls.TextBox;
  31. using Task = System.Threading.Tasks.Task;
  32. using Microsoft.Office.Interop.Word;
  33. using System.Threading.Tasks;
  34. using static Dropbox.Api.TeamLog.TimeUnit;
  35. using System.Threading;
  36. using static Dropbox.Api.Files.SaveUrlError;
  37. using System.Windows.Documents;
  38. using System.Runtime.Remoting.Messaging;
  39. using ListView = System.Windows.Controls.ListView;
  40. namespace PDF_Office.ViewModels.BOTA
  41. {
  42. public class BookmarkContentViewModel : BindableBase, INavigationAware
  43. {
  44. #region 文案
  45. private string T_emptyTitle;
  46. public string T_EmptyTitle
  47. {
  48. get { return T_emptyTitle; }
  49. set
  50. {
  51. SetProperty(ref T_emptyTitle, value);
  52. }
  53. }
  54. private string T_emptyContext;
  55. public string T_EmptyContext
  56. {
  57. get { return T_emptyContext; }
  58. set
  59. {
  60. SetProperty(ref T_emptyContext, value);
  61. }
  62. }
  63. private void InitString()
  64. {
  65. T_EmptyTitle = App.MainPageLoader.GetString("Bookmark_EmptyTitle");
  66. T_EmptyContext = App.MainPageLoader.GetString("Bookmark_EmptyContext");
  67. }
  68. #endregion
  69. #region 属性
  70. public bool isAddBookMark = false;
  71. private IRegionManager region;
  72. private IDialogService dialogs;
  73. public CPDFViewer PDFViewer;
  74. /// <summary>
  75. /// 书签ItemSouce
  76. /// </summary>
  77. private ObservableCollection<CPDFBookmark> bookmarklist;
  78. public ObservableCollection<CPDFBookmark> Bookmarklist
  79. {
  80. get
  81. {
  82. return bookmarklist;
  83. }
  84. set
  85. {
  86. SetProperty(ref bookmarklist, value);
  87. }
  88. }
  89. /// <summary>
  90. /// 书签列表为空时,显示状态
  91. /// </summary>
  92. private Visibility isEmpty;
  93. public Visibility IsEmptyPanelVisibility
  94. {
  95. get
  96. {
  97. return isEmpty;
  98. }
  99. set
  100. {
  101. SetProperty(ref isEmpty, value);
  102. }
  103. }
  104. public CPDFBookmark AddCPDFBookmark = null;
  105. public bool IsHasBookmark = true;
  106. #endregion 属性
  107. #region 命令
  108. public DelegateCommand<object> KeyDownCommand { get; set; }
  109. public DelegateCommand<object> LostFocusCommand { get; set; }
  110. public DelegateCommand<object> AddBookmarkCommand { get; set; }
  111. public DelegateCommand<object> ListViewItemMouseDownCommand { get; set; }
  112. public DelegateCommand<object> EditPageIndexCommand { get; set; }
  113. public DelegateCommand<object> DeleteCommand { get; set; }
  114. #endregion 命令
  115. public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  116. {
  117. region = regionManager;
  118. dialogs = dialogService;
  119. Bookmarklist = new ObservableCollection<CPDFBookmark>();
  120. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  121. AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
  122. ListViewItemMouseDownCommand = new DelegateCommand<object>(ListViewItemMouseLeftButtonDownEvent);
  123. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  124. EditPageIndexCommand = new DelegateCommand<object>(EditPageIndexEvent);
  125. KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
  126. InitString();
  127. }
  128. /// <summary>
  129. /// 检测ObservableCollection的数据变更
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void Bookmarklist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  134. {
  135. ObservableCollection<CPDFBookmark> obsSender = sender as ObservableCollection<CPDFBookmark>;
  136. if (obsSender != null)
  137. {
  138. if (obsSender.Count < 1)
  139. {
  140. IsEmptyPanelVisibility = Visibility.Visible;
  141. }
  142. else
  143. {
  144. IsEmptyPanelVisibility = Visibility.Collapsed;
  145. }
  146. }
  147. else
  148. {
  149. IsEmptyPanelVisibility = Visibility.Visible;
  150. }
  151. }
  152. /// <summary>
  153. /// 文本框按下Enter键
  154. /// </summary>
  155. /// <param name="obj"></param>
  156. private void KeyDownEvent(object obj)
  157. {
  158. if (obj is KeyEventArgs)
  159. {
  160. KeyEventArgs keyEventArgs = (KeyEventArgs)obj;
  161. if (keyEventArgs.Key == Key.Enter)
  162. {
  163. if (keyEventArgs.OriginalSource is TextBox)
  164. {
  165. TextBox textBox = (TextBox)keyEventArgs.OriginalSource;
  166. ListViewItem listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  167. UpdateTitle(listViewItem, textBox);
  168. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
  169. textBlock.Text = textBox.Text;
  170. textBlock.Visibility = Visibility.Visible;
  171. }
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// 重新定位
  177. /// </summary>
  178. /// <param name="obj"></param>
  179. private void EditPageIndexEvent(object obj)
  180. {
  181. CPDFBookmark bookmark = obj as CPDFBookmark;
  182. if (bookmark != null)
  183. {
  184. AlertsMessage alertsMessage = new AlertsMessage();
  185. alertsMessage.ShowDialog("提示", "确定要将选定大纲的目标位置设置为当前位置吗?", "Cancel", "OK");
  186. if (alertsMessage.result == ContentResult.Ok)
  187. {
  188. if (RemoveBookMark(bookmark))
  189. {
  190. bookmarklist.Remove(bookmark);
  191. CPDFBookmark cPDFBookmark = new CPDFBookmark();
  192. bookmark.Title = bookmark.Title;
  193. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  194. bookmark.PageIndex = PDFViewer.CurrentIndex;
  195. if (PDFViewer.Document.AddBookmark(bookmark))
  196. {
  197. PDFViewer.UndoManager.CanSave = true;
  198. Bookmarklist.Add(bookmark);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 右键菜单-删除
  206. /// </summary>
  207. /// <param name="obj"></param>
  208. private void DelegateEvent(object obj)
  209. {
  210. CPDFBookmark bookmark = obj as CPDFBookmark;
  211. if (bookmark != null)
  212. {
  213. if (RemoveBookMark(bookmark))
  214. {
  215. Bookmarklist.Remove(bookmark);
  216. isAddBookMark = true;
  217. PDFViewer.UndoManager.CanSave = true;
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// 删除书签
  223. /// </summary>
  224. /// <param name="item"></param>
  225. /// <returns></returns>
  226. private bool RemoveBookMark(CPDFBookmark item)
  227. {
  228. var data = item;
  229. if (data == null)
  230. {
  231. return false;
  232. }
  233. return PDFViewer.Document.RemoveBookmark(data.PageIndex);
  234. }
  235. /// <summary>
  236. /// ListViewItem失去焦点
  237. /// </summary>
  238. /// <param name="obj"></param>
  239. private void LostFocusEvent(object obj)
  240. {
  241. TextBox textBox = null;
  242. ListViewItem listViewItem = null;
  243. if (obj is CompositeCommandParameter)
  244. {
  245. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  246. if (parameter.Parameter is TextBox)
  247. {
  248. textBox = (TextBox)parameter.Parameter;
  249. listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  250. }
  251. }
  252. if (obj is ListBoxItem)
  253. {
  254. listViewItem = (ListViewItem)(obj);
  255. textBox = CommonHelper.FindVisualChild<TextBox>(listViewItem);
  256. }
  257. CPDFBookmark bookmark = (CPDFBookmark)listViewItem.DataContext;
  258. UpdateTitle(listViewItem, textBox);
  259. }
  260. /// <summary>
  261. /// 修改书签标题
  262. /// </summary>
  263. /// <param name="listViewItem"></param>
  264. /// <param name="textBox"></param>
  265. private void UpdateTitle(ListViewItem listViewItem, TextBox textBox)
  266. {
  267. if (listViewItem != null)
  268. {
  269. var data = listViewItem.DataContext as CPDFBookmark;
  270. if (data == null)
  271. {
  272. return;
  273. }
  274. var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
  275. if (result)
  276. {
  277. data.Title = textBox.Text.Trim();
  278. PDFViewer.UndoManager.CanSave = true;
  279. }
  280. }
  281. }
  282. /// <summary>
  283. /// ListBoxItem左键单击,页面跳转
  284. /// </summary>
  285. /// <param name="obj"></param>
  286. private void ListViewItemMouseLeftButtonDownEvent(object obj)
  287. {
  288. ListBoxItem listBoxItem = (obj as ListBoxItem);
  289. if (listBoxItem != null)
  290. {
  291. if (!(listBoxItem.DataContext is CPDFBookmark))
  292. {
  293. return;
  294. }
  295. int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
  296. PDFViewer.GoToPage(index);
  297. }
  298. }
  299. /// <summary>
  300. /// 添加书签
  301. /// </summary>
  302. /// <param name="obj"></param>
  303. private void AddBookmarkEvent(object obj)
  304. {
  305. string info = App.MainPageLoader.GetString("Bookmark_PageN");
  306. int index = PDFViewer.CurrentIndex;
  307. string mark = string.Format(info, (index + 1).ToString());
  308. System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView;
  309. listView.SelectedItems.Clear();
  310. IsHasBookmark = IsExistBookmark(listView);// await Task.Run(() => IsExistBookmark(listView));
  311. if (IsHasBookmark == false)
  312. {
  313. CPDFBookmark bookmark = new CPDFBookmark();
  314. bookmark.Title = mark;
  315. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  316. bookmark.PageIndex = PDFViewer.CurrentIndex;
  317. AddCPDFBookmark = bookmark;
  318. if (PDFViewer.Document.AddBookmark(bookmark))
  319. {
  320. PDFViewer.UndoManager.CanSave = true;
  321. Bookmarklist.Add(bookmark);
  322. Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  323. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  324. isAddBookMark = false;
  325. }
  326. //DialogParameters value = new DialogParameters();
  327. //value.Add(ParameterNames.Bookmark, mark);
  328. //value.Add(ParameterNames.Title, "创建一个新的书签");
  329. //dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e =>
  330. //{
  331. // if (e.Result == ButtonResult.OK && e.Parameters != null)
  332. // {
  333. // if (e.Parameters.ContainsKey(ParameterNames.Bookmark))
  334. // {
  335. // mark = e.Parameters.GetValue<string>(ParameterNames.Bookmark).ToString();
  336. // CPDFBookmark bookmark = new CPDFBookmark();
  337. // bookmark.Title = mark;
  338. // bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  339. // bookmark.PageIndex = PDFViewer.CurrentIndex;
  340. // AddCPDFBookmark = bookmark;
  341. // if (PDFViewer.Document.AddBookmark(bookmark))
  342. // {
  343. // PDFViewer.UndoManager.CanSave = true;
  344. // Bookmarklist.Add(bookmark);
  345. // Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  346. // Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  347. // }
  348. // }
  349. // isAddBookMark = false;
  350. // }
  351. // if (e.Result == ButtonResult.Cancel)
  352. // {
  353. // isAddBookMark = true;
  354. // }
  355. //});
  356. }
  357. }
  358. private bool IsExistBookmark(System.Windows.Controls.ListView listView)
  359. {
  360. bool isExistBookmark = false;
  361. int index = PDFViewer.CurrentIndex;
  362. if (PDFViewer.Document == null)
  363. {
  364. isExistBookmark = true;
  365. return isExistBookmark;
  366. }
  367. //检测是否已存在相同数据
  368. var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
  369. if (list.Count > 0)
  370. {
  371. int i = PDFViewer.Document.GetBookmarkList().FindIndex(q => q.PageIndex == index);
  372. isExistBookmark = true;
  373. if (listView != null)
  374. {
  375. listView.ScrollIntoView(list[0]);
  376. //ListBoxItem myListBoxItem = await Task.Run(() => GetListViewItem(listView, list[0]));
  377. ListViewItem myListBoxItem = (ListViewItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  378. List<ListViewItem> listItems = new List<ListViewItem>();
  379. foreach (var item in listView.SelectedItems)
  380. {
  381. listView.ScrollIntoView(item);
  382. ListViewItem listViewItem = (ListViewItem)(listView.ItemContainerGenerator.ContainerFromItem(item));
  383. if (listItems.IndexOf(listViewItem) == -1)
  384. {
  385. listItems.Add(listViewItem);
  386. }
  387. }
  388. if (myListBoxItem.IsSelected == false)
  389. {
  390. myListBoxItem.IsSelected = true;
  391. myListBoxItem.Focus();
  392. isAddBookMark = false;
  393. foreach (var item in listItems)
  394. {
  395. item.IsSelected = false;
  396. }
  397. }
  398. return isExistBookmark;
  399. }
  400. }
  401. return isExistBookmark;
  402. }
  403. public void OnNavigatedTo(NavigationContext navigationContext)
  404. {
  405. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  406. if (PDFViewer == null)
  407. {
  408. IsEmptyPanelVisibility = Visibility.Visible;
  409. return;
  410. }
  411. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.PageIndex));
  412. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  413. if (Bookmarklist.Count < 1)
  414. {
  415. IsEmptyPanelVisibility = Visibility.Visible;
  416. return;
  417. }
  418. else
  419. {
  420. IsEmptyPanelVisibility = Visibility.Hidden;
  421. return;
  422. }
  423. }
  424. public bool IsNavigationTarget(NavigationContext navigationContext)
  425. {
  426. return true;
  427. }
  428. public void OnNavigatedFrom(NavigationContext navigationContext)
  429. {
  430. }
  431. }
  432. }