ToolsBarContentViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. /// <summary>
  131. /// 防止自动保存异步导致弹窗出现两次
  132. /// </summary>
  133. private bool _isPopupInCompressProgress;
  134. public bool IsPopupInCompressProgress
  135. {
  136. get { return _isPopupInCompressProgress; }
  137. set { SetProperty(ref _isPopupInCompressProgress, value); }
  138. }
  139. /// <summary>
  140. /// 防止自动保存异步导致弹窗出现两次
  141. /// </summary>
  142. private bool _isPopupInMergeProgress;
  143. public bool IsPopupInMergeProgress
  144. {
  145. get { return _isPopupInMergeProgress; }
  146. set { SetProperty(ref _isPopupInMergeProgress, value); }
  147. }
  148. public DelegateCommand CompressCommand { get; set; }
  149. public DelegateCommand MergeCommand { get; set; }
  150. public DelegateCommand SetPasswordCommand { get; set; }
  151. public DelegateCommand CancelPasswordCommand { get; set; }
  152. public DelegateCommand<object> SetEditToolsCommand { get; set; }
  153. public DelegateCommand SetWatermarkCommand { get; set; }
  154. public DelegateCommand CropCurrentPageWMCommand { get; set; }
  155. public DelegateCommand CropAllPagesWMCommand { get; set; }
  156. public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  157. {
  158. InitString();
  159. dialogs = dialogService;
  160. this.eventAggregator = eventAggregator;
  161. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  162. CompressCommand = new DelegateCommand(OpenCompressDialog);
  163. MergeCommand = new DelegateCommand(MergeDialog);
  164. SetPasswordCommand = new DelegateCommand(OpenSetPasswordDialog);
  165. CancelPasswordCommand = new DelegateCommand(OpenCancelPasswordDialog);
  166. SetEditToolsCommand = new DelegateCommand<object>(SetEditTools);
  167. CropCurrentPageWMCommand = new DelegateCommand(CropCurrentPageWM);
  168. CropAllPagesWMCommand = new DelegateCommand(CropAllPagesWM);
  169. }
  170. private void SetEditTools(object e)
  171. {
  172. var args = e as System.Windows.Controls.Button;
  173. if (args != null)
  174. {
  175. this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
  176. }
  177. }
  178. private async void OpenCompressDialog()
  179. {
  180. if (IsPopupInCompressProgress)
  181. {
  182. return;
  183. }
  184. IsPopupInCompressProgress = true;
  185. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  186. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Compress);
  187. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Compress");
  188. if (!ServiceHelper.IAPBeforeFunction())
  189. {
  190. DataTrackingHelper.EntryPath = "";
  191. IsPopupInCompressProgress = false;
  192. return;
  193. }
  194. if (await viewContentViewModel.SaveBeforeOption())
  195. {
  196. IsPopupInCompressProgress = false;
  197. DialogParameters value = new DialogParameters();
  198. value.Add(ParameterNames.PDFViewer, PDFViewer);
  199. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  200. {
  201. CompressDialogModel compressDialogModel = new CompressDialogModel();
  202. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  203. // OnOpened((Prism.Services.Dialogs.DialogResult)e);
  204. });
  205. }
  206. IsPopupInCompressProgress = false;
  207. }
  208. //private int GetIndex(int pageindex)
  209. //{
  210. // PageIndex = (pageindex + 1).ToString();
  211. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  212. // if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  213. // return 0;
  214. //}
  215. //private void CompressClear()
  216. //{
  217. // tempDocument.CompressFile_Cancel(compressingIntpr);
  218. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  219. //}
  220. //public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  221. //{
  222. // if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; }
  223. // CPDFDocument doc = null;
  224. // string filepath = "";
  225. // string password = "";
  226. // int compressquality = 45;
  227. // doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  228. // filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  229. // password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  230. // compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  231. // if (doc != null)
  232. // {
  233. // PageNumber = doc.PageCount.ToString();
  234. // CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  235. // document.UnlockWithPassword(password);
  236. // indexDelegate += GetIndex;
  237. // compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  238. // //GC.KeepAlive(indexDelegate);
  239. // tempDocument = document;
  240. // Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  241. // App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  242. // await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  243. // CommonHelper.ShowFileBrowser(filepath);
  244. // document.Release();
  245. // }
  246. //}
  247. private async void MergeDialog()
  248. {
  249. if (IsPopupInMergeProgress)
  250. {
  251. return;
  252. }
  253. IsPopupInMergeProgress = true;
  254. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  255. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Merge);
  256. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Merge");
  257. if (!ServiceHelper.IAPBeforeFunction())
  258. {
  259. DataTrackingHelper.EntryPath = "";
  260. IsPopupInMergeProgress = false;
  261. return;
  262. }
  263. if (await viewContentViewModel.SaveBeforeOption())
  264. {
  265. IsPopupInMergeProgress = false;
  266. DialogParameters value = new DialogParameters();
  267. value.Add(ParameterNames.PDFViewer, PDFViewer);
  268. value.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  269. value.Add(ParameterNames.Unicode, unicode);
  270. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  271. }
  272. IsPopupInMergeProgress = false;
  273. }
  274. private void OpenSetPasswordDialog()
  275. {
  276. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  277. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.SetPassword);
  278. DataTrackingHelper.IsClearEntryPath = true;
  279. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_SetPassword");
  280. if (!ServiceHelper.IAPBeforeFunction())
  281. {
  282. return;
  283. }
  284. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  285. if (result.IsDiscryptied)
  286. {
  287. if (result.Password != null)
  288. {
  289. PDFViewer.Document.CheckOwnerPassword(result.Password);
  290. }
  291. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  292. DialogParameters value = new DialogParameters();
  293. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  294. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  295. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  296. {
  297. if (e.Result == ButtonResult.OK)
  298. {
  299. SecurityHelper.IsPasswordChanged = true;
  300. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  301. {
  302. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  303. }
  304. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  305. }
  306. });
  307. }
  308. }
  309. private void OpenCancelPasswordDialog()
  310. {
  311. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  312. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.RemovePassword);
  313. DataTrackingHelper.IsClearEntryPath = true;
  314. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_RemovePassword");
  315. ///无可用安全性设置的原因:
  316. ///文件本身无安全性设置,且没有设置过密码
  317. ///文件安全性已经被重置
  318. ///
  319. if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
  320. {
  321. AlertsMessage alertsMessage = new AlertsMessage();
  322. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("NoPasswrod_Waring"), App.ServiceLoader.GetString("Text_ok"));
  323. }
  324. else
  325. {
  326. if (!ServiceHelper.IAPBeforeFunction())
  327. {
  328. return;
  329. }
  330. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  331. if (result.IsDiscryptied)
  332. {
  333. if (result.Password != null)
  334. {
  335. PDFViewer.Document.CheckOwnerPassword(result.Password);
  336. }
  337. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  338. DialogParameters value = new DialogParameters();
  339. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  340. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  341. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e =>
  342. {
  343. if (e.Result == ButtonResult.OK)
  344. {
  345. if (viewContentViewModel.SecurityInfo.IsPasswordRemoved)
  346. {
  347. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  348. }
  349. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
  350. }
  351. });
  352. }
  353. }
  354. }
  355. private void CropCurrentPageWM()
  356. {
  357. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  358. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Crop);
  359. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Crop");
  360. viewContentViewModel.IsLoading = Visibility.Visible;
  361. WaitCropCurrentPageWM();
  362. viewContentViewModel.IsLoading = Visibility.Collapsed;
  363. DataTrackingHelper.EntryPath = "";
  364. }
  365. private void CropAllPagesWM()
  366. {
  367. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_Tools, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  368. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Crop);
  369. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_Tools, "SubTbr_Btn", "Btn_SubTbr_Crop");
  370. viewContentViewModel.IsLoading = Visibility.Visible;
  371. WaitCropAllPagesWM();
  372. viewContentViewModel.IsLoading = Visibility.Collapsed;
  373. DataTrackingHelper.EntryPath = "";
  374. //PDFViewer.UndoManager.AddHistory();
  375. }
  376. private void WaitCropCurrentPageWM()
  377. {
  378. if (!ServiceHelper.IAPBeforeFunction())
  379. {
  380. return;
  381. }
  382. App.Current.Dispatcher.Invoke(new Action(() => {
  383. if (PDFViewer.CurrentIndex >= 0)
  384. {
  385. UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  386. }
  387. UndoManager.ADDCropCurrentPageWM();
  388. //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  389. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  390. PDFViewer.UndoManager.AddHistory(UndoManager);
  391. PDFViewer.UndoManager.CanSave = true;
  392. }));
  393. //5.22 SDK更新后剪切线程异常
  394. // await Task.Run(() =>
  395. //{
  396. // if (PDFViewer.CurrentIndex >= 0)
  397. // {
  398. // UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  399. // }
  400. // UndoManager.ADDCropCurrentPageWM();
  401. // //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  402. // PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  403. // PDFViewer.UndoManager.AddHistory(UndoManager);
  404. // PDFViewer.UndoManager.CanSave = true;
  405. //});
  406. }
  407. private void WaitCropAllPagesWM()
  408. {
  409. if (!ServiceHelper.IAPBeforeFunction())
  410. {
  411. return;
  412. }
  413. App.Current.Dispatcher.Invoke(new Action(() => {
  414. List<int> cropPagesList = new List<int>();
  415. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  416. {
  417. cropPagesList.Add(i);
  418. }
  419. UndoManager.cropPageList.AddRange(cropPagesList);
  420. UndoManager.ADDCropAllPagesWM();
  421. //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  422. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  423. PDFViewer.UndoManager.AddHistory(UndoManager);
  424. PDFViewer.UndoManager.CanSave = true;
  425. }));
  426. //5.22 SDK更新后剪切线程异常
  427. // await Task.Run(() =>
  428. //{
  429. //List<int> cropPagesList = new List<int>();
  430. //for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  431. //{
  432. // cropPagesList.Add(i);
  433. //}
  434. //UndoManager.cropPageList.AddRange(cropPagesList);
  435. //UndoManager.ADDCropAllPagesWM();
  436. ////此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  437. //PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  438. //PDFViewer.UndoManager.AddHistory(UndoManager);
  439. //PDFViewer.UndoManager.CanSave = true;
  440. //});
  441. }
  442. public bool IsNavigationTarget(NavigationContext navigationContext)
  443. {
  444. return true;
  445. }
  446. public void OnNavigatedFrom(NavigationContext navigationContext)
  447. {
  448. }
  449. public void OnNavigatedTo(NavigationContext navigationContext)
  450. {
  451. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  452. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  453. if (PDFViewer != null)
  454. {
  455. UndoManager.setPDFViewer(PDFViewer);
  456. //DFViewer.UndoManager.AddHistory(UndoManager);
  457. }
  458. }
  459. }
  460. }