ToolsBarContentViewModel.cs 18 KB

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