BookmarkContentViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. else
  111. {
  112. IsEmptyPanelVisibility = Visibility.Visible;
  113. }
  114. }
  115. /// <summary>
  116. /// 文本框按下Enter键
  117. /// </summary>
  118. /// <param name="obj"></param>
  119. private void KeyDownEvent(object obj)
  120. {
  121. if (obj is KeyEventArgs)
  122. {
  123. KeyEventArgs keyEventArgs = (KeyEventArgs)obj;
  124. if (keyEventArgs.Key == Key.Enter)
  125. {
  126. if (keyEventArgs.OriginalSource is TextBox)
  127. {
  128. TextBox textBox = (TextBox)keyEventArgs.OriginalSource;
  129. ListViewItem listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  130. UpdateTitle(listViewItem, textBox);
  131. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
  132. textBlock.Text = textBox.Text;
  133. textBlock.Visibility = Visibility.Visible;
  134. }
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 重新定位
  140. /// </summary>
  141. /// <param name="obj"></param>
  142. private void EditPageIndexEvent(object obj)
  143. {
  144. CPDFBookmark bookmark = obj as CPDFBookmark;
  145. if (bookmark != null)
  146. {
  147. AlertsMessage alertsMessage = new AlertsMessage();
  148. alertsMessage.ShowDialog("提示", "确定要将选定大纲的目标位置设置为当前位置吗?", "Cancel", "OK");
  149. if (alertsMessage.result == ContentResult.Ok)
  150. {
  151. if (RemoveBookMark(bookmark))
  152. {
  153. bookmarklist.Remove(bookmark);
  154. CPDFBookmark cPDFBookmark = new CPDFBookmark();
  155. bookmark.Title = bookmark.Title;
  156. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  157. bookmark.PageIndex = PDFViewer.CurrentIndex;
  158. if (PDFViewer.Document.AddBookmark(bookmark))
  159. {
  160. PDFViewer.UndoManager.CanSave = true;
  161. Bookmarklist.Add(bookmark);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// 右键菜单-删除
  169. /// </summary>
  170. /// <param name="obj"></param>
  171. private void DelegateEvent(object obj)
  172. {
  173. CPDFBookmark bookmark = obj as CPDFBookmark;
  174. if (bookmark != null)
  175. {
  176. if (RemoveBookMark(bookmark))
  177. {
  178. Bookmarklist.Remove(bookmark);
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// 删除书签
  184. /// </summary>
  185. /// <param name="item"></param>
  186. /// <returns></returns>
  187. private bool RemoveBookMark(CPDFBookmark item)
  188. {
  189. var data = item;
  190. if (data == null)
  191. {
  192. return false;
  193. }
  194. return PDFViewer.Document.RemoveBookmark(data.PageIndex);
  195. }
  196. /// <summary>
  197. /// ListViewItem失去焦点
  198. /// </summary>
  199. /// <param name="obj"></param>
  200. private void LostFocusEvent(object obj)
  201. {
  202. TextBox textBox = null;
  203. ListViewItem listViewItem = null;
  204. if (obj is CompositeCommandParameter)
  205. {
  206. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  207. if (parameter.Parameter is TextBox)
  208. {
  209. textBox = (TextBox)parameter.Parameter;
  210. listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  211. }
  212. }
  213. if (obj is ListBoxItem)
  214. {
  215. listViewItem = (ListViewItem)(obj);
  216. textBox = CommonHelper.FindVisualChild<TextBox>(listViewItem);
  217. }
  218. UpdateTitle(listViewItem, textBox);
  219. }
  220. /// <summary>
  221. /// 修改书签标题
  222. /// </summary>
  223. /// <param name="listViewItem"></param>
  224. /// <param name="textBox"></param>
  225. private void UpdateTitle(ListViewItem listViewItem, TextBox textBox)
  226. {
  227. if (listViewItem != null)
  228. {
  229. var data = listViewItem.DataContext as CPDFBookmark;
  230. if (data == null)
  231. {
  232. return;
  233. }
  234. var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
  235. if (result)
  236. {
  237. data.Title = textBox.Text.Trim();
  238. PDFViewer.UndoManager.CanSave = true;
  239. }
  240. }
  241. }
  242. /// <summary>
  243. /// ListBoxItem左键单击,页面跳转
  244. /// </summary>
  245. /// <param name="obj"></param>
  246. private void ListViewItemMouseLeftButtonDownEvent(object obj)
  247. {
  248. ListBoxItem listBoxItem = (obj as ListBoxItem);
  249. if (listBoxItem != null)
  250. {
  251. if (!(listBoxItem.DataContext is CPDFBookmark))
  252. {
  253. return;
  254. }
  255. int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
  256. PDFViewer.GoToPage(index);
  257. }
  258. }
  259. /// <summary>
  260. /// 添加书签
  261. /// </summary>
  262. /// <param name="obj"></param>
  263. private void AddBookmarkEvent(object obj)
  264. {
  265. int index = PDFViewer.CurrentIndex;
  266. string mark = string.Format($"第{index + 1}页");
  267. //检测是否已存在相同数据
  268. var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
  269. if (list.Count > 0)
  270. {
  271. System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView;
  272. if (listView != null)
  273. {
  274. ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  275. if (myListBoxItem.IsSelected == false)
  276. {
  277. myListBoxItem.IsSelected = true;
  278. myListBoxItem.Focus();
  279. }
  280. return;
  281. }
  282. }
  283. DialogParameters value = new DialogParameters();
  284. value.Add(ParameterNames.Bookmark, mark);
  285. value.Add(ParameterNames.Title, "创建一个新的书签");
  286. dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e =>
  287. {
  288. if (e.Result == ButtonResult.OK && e.Parameters != null)
  289. {
  290. if (e.Parameters.ContainsKey(ParameterNames.Bookmark))
  291. {
  292. mark = e.Parameters.GetValue<string>(ParameterNames.Bookmark).ToString();
  293. CPDFBookmark bookmark = new CPDFBookmark();
  294. bookmark.Title = mark;
  295. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  296. bookmark.PageIndex = PDFViewer.CurrentIndex;
  297. if (PDFViewer.Document.AddBookmark(bookmark))
  298. {
  299. PDFViewer.UndoManager.CanSave = true;
  300. Bookmarklist.Add(bookmark);
  301. }
  302. }
  303. }
  304. });
  305. }
  306. public void OnNavigatedTo(NavigationContext navigationContext)
  307. {
  308. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  309. if (PDFViewer == null)
  310. {
  311. IsEmptyPanelVisibility = Visibility.Visible;
  312. return;
  313. }
  314. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  315. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  316. if (Bookmarklist.Count < 1)
  317. {
  318. IsEmptyPanelVisibility = Visibility.Visible;
  319. return;
  320. }
  321. else
  322. {
  323. IsEmptyPanelVisibility = Visibility.Hidden;
  324. return;
  325. }
  326. }
  327. public bool IsNavigationTarget(NavigationContext navigationContext)
  328. {
  329. return true;
  330. }
  331. public void OnNavigatedFrom(NavigationContext navigationContext)
  332. {
  333. }
  334. }
  335. }