ToolsBarContentViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Interop.Word;
  4. using PDF_Master.CustomControl;
  5. using PDF_Master.EventAggregators;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using Prism.Services.Dialogs;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Linq;
  18. using System.Security.Cryptography.X509Certificates;
  19. using System.Windows;
  20. using System.Threading.Tasks;
  21. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  22. using Task = System.Threading.Tasks.Task;
  23. using PDF_Master.Model.Dialog.ToolsDialogs;
  24. using ComPDFKitViewer;
  25. using PDF_Master.Properties;
  26. using PDFReader_WPF.Helper;
  27. namespace PDF_Master.ViewModels.Tools
  28. {
  29. public class ToolsBarContentViewModel : BindableBase, INavigationAware
  30. {
  31. #region 文案
  32. private string T_setPassWord;
  33. public string T_SetPassWord
  34. {
  35. get { return T_setPassWord; }
  36. set
  37. {
  38. SetProperty(ref T_setPassWord, value);
  39. }
  40. }
  41. private string T_removePassword;
  42. public string T_RemovePassword
  43. {
  44. get { return T_removePassword; }
  45. set
  46. {
  47. SetProperty(ref T_removePassword, value);
  48. }
  49. }
  50. private string T_compress;
  51. public string T_Compress
  52. {
  53. get { return T_compress; }
  54. set
  55. {
  56. SetProperty(ref T_compress, value);
  57. }
  58. }
  59. private string T_security;
  60. public string T_Security
  61. {
  62. get { return T_security; }
  63. set
  64. {
  65. SetProperty(ref T_security, value);
  66. }
  67. }
  68. private string T_merge;
  69. public string T_Merge
  70. {
  71. get { return T_merge; }
  72. set
  73. {
  74. SetProperty(ref T_merge, value);
  75. }
  76. }
  77. private string T_crop;
  78. public string T_Crop
  79. {
  80. get { return T_crop; }
  81. set
  82. {
  83. SetProperty(ref T_crop, value);
  84. }
  85. }
  86. private void InitString()
  87. {
  88. T_SetPassWord = App.MainPageLoader.GetString("Security_SetPassword");
  89. T_RemovePassword = App.MainPageLoader.GetString("Security_RemovePassword");
  90. T_Compress = App.MainPageLoader.GetString("ToolsBar_Compress");
  91. T_Security = App.MainPageLoader.GetString("ToolsBar_Security");
  92. T_Merge = App.MainPageLoader.GetString("ToolsBar_Merge");
  93. T_Crop = App.MainPageLoader.GetString("ToolsBar_Crop");
  94. }
  95. #endregion
  96. private CPDFViewer PDFViewer;
  97. private ViewContentViewModel viewContentViewModel;
  98. public IDialogService dialogs;
  99. public CropPageUndoManager UndoManager = new CropPageUndoManager();
  100. public IEventAggregator eventAggregator;
  101. List<int> cropCurrentPageList = new List<int>();
  102. public string unicode = null;
  103. ///// <value>
  104. ///// 当前页
  105. ///// </value>
  106. //public string PageIndex
  107. //{
  108. // get { return _pageIndex; }
  109. // set
  110. // {
  111. // SetProperty(ref _pageIndex, value);
  112. // App.mainWindowViewModel.Value = int.Parse(PageIndex);
  113. // }
  114. //}
  115. //private CPDFDocument tempDocument;
  116. //private IntPtr compressingIntpr = IntPtr.Zero;
  117. //private string _pageNumber = "";
  118. ///// <value>
  119. ///// 总页数
  120. ///// </value>
  121. //public string PageNumber
  122. //{
  123. // get { return _pageNumber; }
  124. // set
  125. // {
  126. // SetProperty(ref _pageNumber, value);
  127. // App.mainWindowViewModel.MaxValue = int.Parse(PageNumber);
  128. // }
  129. //}
  130. public DelegateCommand CompressCommand { get; set; }
  131. public DelegateCommand MergeCommand { get; set; }
  132. public DelegateCommand SetPasswordCommand { get; set; }
  133. public DelegateCommand CancelPasswordCommand { get; set; }
  134. public DelegateCommand<object> SetEditToolsCommand { get; set; }
  135. public DelegateCommand SetWatermarkCommand { get; set; }
  136. public DelegateCommand CropCurrentPageWMCommand { get; set; }
  137. public DelegateCommand CropAllPagesWMCommand { get; set; }
  138. public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  139. {
  140. InitString();
  141. dialogs = dialogService;
  142. this.eventAggregator = eventAggregator;
  143. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  144. CompressCommand = new DelegateCommand(OpenCompressDialog);
  145. MergeCommand = new DelegateCommand(MergeDialog);
  146. SetPasswordCommand = new DelegateCommand(OpenSetPasswordDialog);
  147. CancelPasswordCommand = new DelegateCommand(OpenCancelPasswordDialog);
  148. SetEditToolsCommand = new DelegateCommand<object>(SetEditTools);
  149. CropCurrentPageWMCommand = new DelegateCommand(CropCurrentPageWM);
  150. CropAllPagesWMCommand = new DelegateCommand(CropAllPagesWM);
  151. }
  152. private void SetEditTools(object e)
  153. {
  154. var args = e as System.Windows.Controls.Button;
  155. if (args != null)
  156. {
  157. this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
  158. }
  159. }
  160. private async void OpenCompressDialog()
  161. {
  162. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  163. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Compress);
  164. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Compress");
  165. if (!ServiceHelper.IAPBeforeFunction())
  166. {
  167. DataTrackingHelper.EntryPath = "";
  168. return;
  169. }
  170. if (await viewContentViewModel.SaveBeforeOption())
  171. {
  172. DialogParameters value = new DialogParameters();
  173. value.Add(ParameterNames.PDFViewer, PDFViewer);
  174. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  175. {
  176. CompressDialogModel compressDialogModel = new CompressDialogModel();
  177. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  178. // OnOpened((Prism.Services.Dialogs.DialogResult)e);
  179. });
  180. }
  181. }
  182. //private int GetIndex(int pageindex)
  183. //{
  184. // PageIndex = (pageindex + 1).ToString();
  185. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  186. // if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  187. // return 0;
  188. //}
  189. //private void CompressClear()
  190. //{
  191. // tempDocument.CompressFile_Cancel(compressingIntpr);
  192. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  193. //}
  194. //public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  195. //{
  196. // if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; }
  197. // CPDFDocument doc = null;
  198. // string filepath = "";
  199. // string password = "";
  200. // int compressquality = 45;
  201. // doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  202. // filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  203. // password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  204. // compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  205. // if (doc != null)
  206. // {
  207. // PageNumber = doc.PageCount.ToString();
  208. // CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  209. // document.UnlockWithPassword(password);
  210. // indexDelegate += GetIndex;
  211. // compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  212. // //GC.KeepAlive(indexDelegate);
  213. // tempDocument = document;
  214. // Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  215. // App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  216. // await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  217. // CommonHelper.ShowFileBrowser(filepath);
  218. // document.Release();
  219. // }
  220. //}
  221. private async void MergeDialog()
  222. {
  223. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  224. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Merge);
  225. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Merge");
  226. if (!ServiceHelper.IAPBeforeFunction())
  227. {
  228. DataTrackingHelper.EntryPath = "";
  229. return;
  230. }
  231. if (await viewContentViewModel.SaveBeforeOption())
  232. {
  233. DialogParameters value = new DialogParameters();
  234. value.Add(ParameterNames.PDFViewer, PDFViewer);
  235. value.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  236. value.Add(ParameterNames.Unicode, unicode);
  237. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  238. }
  239. }
  240. private void OpenSetPasswordDialog()
  241. {
  242. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  243. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.SetPassword);
  244. DataTrackingHelper.IsClearEntryPath = true;
  245. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_SetPassword");
  246. if (!ServiceHelper.IAPBeforeFunction())
  247. {
  248. return;
  249. }
  250. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  251. if (result.IsDiscryptied)
  252. {
  253. if (result.Password != null)
  254. {
  255. PDFViewer.Document.CheckOwnerPassword(result.Password);
  256. }
  257. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  258. DialogParameters value = new DialogParameters();
  259. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  260. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  261. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  262. {
  263. if (e.Result == ButtonResult.OK)
  264. {
  265. SecurityHelper.IsPasswordChanged = true;
  266. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  267. {
  268. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  269. }
  270. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  271. }
  272. });
  273. }
  274. }
  275. private void OpenCancelPasswordDialog()
  276. {
  277. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  278. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.RemovePassword);
  279. DataTrackingHelper.IsClearEntryPath = true;
  280. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_RemovePassword");
  281. ///无可用安全性设置的原因:
  282. ///文件本身无安全性设置,且没有设置过密码
  283. ///文件安全性已经被重置
  284. ///
  285. if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
  286. {
  287. AlertsMessage alertsMessage = new AlertsMessage();
  288. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("NoPasswrod_Waring"), App.ServiceLoader.GetString("Text_ok"));
  289. }
  290. else
  291. {
  292. if (!ServiceHelper.IAPBeforeFunction())
  293. {
  294. return;
  295. }
  296. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  297. if (result.IsDiscryptied)
  298. {
  299. if (result.Password != null)
  300. {
  301. PDFViewer.Document.CheckOwnerPassword(result.Password);
  302. }
  303. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  304. DialogParameters value = new DialogParameters();
  305. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  306. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  307. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e =>
  308. {
  309. if (e.Result == ButtonResult.OK)
  310. {
  311. if (viewContentViewModel.SecurityInfo.IsPasswordRemoved)
  312. {
  313. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  314. }
  315. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
  316. }
  317. });
  318. }
  319. }
  320. }
  321. private void CropCurrentPageWM()
  322. {
  323. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  324. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Crop);
  325. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Crop");
  326. viewContentViewModel.IsLoading = Visibility.Visible;
  327. WaitCropCurrentPageWM();
  328. viewContentViewModel.IsLoading = Visibility.Collapsed;
  329. DataTrackingHelper.EntryPath = "";
  330. }
  331. private void CropAllPagesWM()
  332. {
  333. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  334. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Crop);
  335. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Crop");
  336. viewContentViewModel.IsLoading = Visibility.Visible;
  337. WaitCropAllPagesWM();
  338. viewContentViewModel.IsLoading = Visibility.Collapsed;
  339. DataTrackingHelper.EntryPath = "";
  340. //PDFViewer.UndoManager.AddHistory();
  341. }
  342. private void WaitCropCurrentPageWM()
  343. {
  344. if (!ServiceHelper.IAPBeforeFunction())
  345. {
  346. return;
  347. }
  348. App.Current.Dispatcher.Invoke(new Action(() => {
  349. if (PDFViewer.CurrentIndex >= 0)
  350. {
  351. UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  352. }
  353. UndoManager.ADDCropCurrentPageWM();
  354. //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  355. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  356. PDFViewer.UndoManager.AddHistory(UndoManager);
  357. PDFViewer.UndoManager.CanSave = true;
  358. }));
  359. //5.22 SDK更新后剪切线程异常
  360. // await Task.Run(() =>
  361. //{
  362. // if (PDFViewer.CurrentIndex >= 0)
  363. // {
  364. // UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  365. // }
  366. // UndoManager.ADDCropCurrentPageWM();
  367. // //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  368. // PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  369. // PDFViewer.UndoManager.AddHistory(UndoManager);
  370. // PDFViewer.UndoManager.CanSave = true;
  371. //});
  372. }
  373. private void WaitCropAllPagesWM()
  374. {
  375. if (!ServiceHelper.IAPBeforeFunction())
  376. {
  377. return;
  378. }
  379. App.Current.Dispatcher.Invoke(new Action(() => {
  380. List<int> cropPagesList = new List<int>();
  381. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  382. {
  383. cropPagesList.Add(i);
  384. }
  385. UndoManager.cropPageList.AddRange(cropPagesList);
  386. UndoManager.ADDCropAllPagesWM();
  387. //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  388. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  389. PDFViewer.UndoManager.AddHistory(UndoManager);
  390. PDFViewer.UndoManager.CanSave = true;
  391. }));
  392. //5.22 SDK更新后剪切线程异常
  393. // await Task.Run(() =>
  394. //{
  395. //List<int> cropPagesList = new List<int>();
  396. //for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  397. //{
  398. // cropPagesList.Add(i);
  399. //}
  400. //UndoManager.cropPageList.AddRange(cropPagesList);
  401. //UndoManager.ADDCropAllPagesWM();
  402. ////此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  403. //PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  404. //PDFViewer.UndoManager.AddHistory(UndoManager);
  405. //PDFViewer.UndoManager.CanSave = true;
  406. //});
  407. }
  408. public bool IsNavigationTarget(NavigationContext navigationContext)
  409. {
  410. return true;
  411. }
  412. public void OnNavigatedFrom(NavigationContext navigationContext)
  413. {
  414. }
  415. public void OnNavigatedTo(NavigationContext navigationContext)
  416. {
  417. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  418. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  419. if (PDFViewer != null)
  420. {
  421. UndoManager.setPDFViewer(PDFViewer);
  422. //DFViewer.UndoManager.AddHistory(UndoManager);
  423. }
  424. }
  425. }
  426. }