BookmarkContentViewModel.cs 12 KB

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