ToolsBarContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  102. public string unicode = null;
  103. /// <summary>
  104. /// 是否是第一次加载
  105. /// </summary>
  106. private bool isFirstLoad = true;
  107. private string _pageIndex = "0";
  108. ///// <value>
  109. ///// 当前页
  110. ///// </value>
  111. //public string PageIndex
  112. //{
  113. // get { return _pageIndex; }
  114. // set
  115. // {
  116. // SetProperty(ref _pageIndex, value);
  117. // App.mainWindowViewModel.Value = int.Parse(PageIndex);
  118. // }
  119. //}
  120. //private CPDFDocument tempDocument;
  121. //private IntPtr compressingIntpr = IntPtr.Zero;
  122. //private string _pageNumber = "";
  123. ///// <value>
  124. ///// 总页数
  125. ///// </value>
  126. //public string PageNumber
  127. //{
  128. // get { return _pageNumber; }
  129. // set
  130. // {
  131. // SetProperty(ref _pageNumber, value);
  132. // App.mainWindowViewModel.MaxValue = int.Parse(PageNumber);
  133. // }
  134. //}
  135. public DelegateCommand CompressCommand { get; set; }
  136. public DelegateCommand MergeCommand { get; set; }
  137. public DelegateCommand SetPasswordCommand { get; set; }
  138. public DelegateCommand CancelPasswordCommand { get; set; }
  139. public DelegateCommand<object> SetEditToolsCommand { get; set; }
  140. public DelegateCommand SetWatermarkCommand { get; set; }
  141. public DelegateCommand CropCurrentPageWMCommand { get; set; }
  142. public DelegateCommand CropAllPagesWMCommand { get; set; }
  143. public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  144. {
  145. InitString();
  146. dialogs = dialogService;
  147. this.eventAggregator = eventAggregator;
  148. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  149. CompressCommand = new DelegateCommand(OpenCompressDialog);
  150. MergeCommand = new DelegateCommand(MergeDialog);
  151. SetPasswordCommand = new DelegateCommand(OpenSetPasswordDialog);
  152. CancelPasswordCommand = new DelegateCommand(OpenCancelPasswordDialog);
  153. SetEditToolsCommand = new DelegateCommand<object>(SetEditTools);
  154. CropCurrentPageWMCommand = new DelegateCommand(CropCurrentPageWM);
  155. CropAllPagesWMCommand = new DelegateCommand(CropAllPagesWM);
  156. }
  157. private void SetEditTools(object e)
  158. {
  159. var args = e as System.Windows.Controls.Button;
  160. if (args != null)
  161. {
  162. this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
  163. }
  164. }
  165. private void OpenCompressDialog()
  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. //private int GetIndex(int pageindex)
  177. //{
  178. // PageIndex = (pageindex + 1).ToString();
  179. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  180. // if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  181. // return 0;
  182. //}
  183. //private void CompressClear()
  184. //{
  185. // tempDocument.CompressFile_Cancel(compressingIntpr);
  186. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  187. //}
  188. //public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  189. //{
  190. // if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; }
  191. // CPDFDocument doc = null;
  192. // string filepath = "";
  193. // string password = "";
  194. // int compressquality = 45;
  195. // doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  196. // filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  197. // password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  198. // compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  199. // if (doc != null)
  200. // {
  201. // PageNumber = doc.PageCount.ToString();
  202. // CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  203. // document.UnlockWithPassword(password);
  204. // indexDelegate += GetIndex;
  205. // compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  206. // //GC.KeepAlive(indexDelegate);
  207. // tempDocument = document;
  208. // Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  209. // App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  210. // await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  211. // CommonHelper.ShowFileBrowser(filepath);
  212. // document.Release();
  213. // }
  214. //}
  215. private void MergeDialog()
  216. {
  217. DialogParameters value = new DialogParameters();
  218. value.Add(ParameterNames.PDFViewer, PDFViewer);
  219. value.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  220. value.Add(ParameterNames.Unicode, unicode);
  221. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  222. }
  223. private void OpenSetPasswordDialog()
  224. {
  225. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  226. if (result.IsDiscryptied)
  227. {
  228. if (result.Password != null)
  229. {
  230. PDFViewer.Document.CheckOwnerPassword(result.Password);
  231. }
  232. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  233. DialogParameters value = new DialogParameters();
  234. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  235. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  236. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  237. {
  238. if (e.Result == ButtonResult.OK)
  239. {
  240. SecurityHelper.IsPasswordChanged = true;
  241. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  242. {
  243. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  244. }
  245. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  246. }
  247. });
  248. }
  249. }
  250. private void OpenCancelPasswordDialog()
  251. {
  252. ///无可用安全性设置的原因:
  253. ///文件本身无安全性设置,且没有设置过密码
  254. ///文件安全性已经被重置
  255. ///
  256. if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
  257. {
  258. AlertsMessage alertsMessage = new AlertsMessage();
  259. alertsMessage.ShowDialog("Message", "No security settings available ", "OK");
  260. }
  261. else
  262. {
  263. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  264. if (result.IsDiscryptied)
  265. {
  266. if (result.Password != null)
  267. {
  268. PDFViewer.Document.CheckOwnerPassword(result.Password);
  269. }
  270. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  271. DialogParameters value = new DialogParameters();
  272. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  273. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  274. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e =>
  275. {
  276. if (e.Result == ButtonResult.OK)
  277. {
  278. if (viewContentViewModel.SecurityInfo.IsPasswordRemoved)
  279. {
  280. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  281. }
  282. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
  283. }
  284. });
  285. }
  286. }
  287. }
  288. private async void CropCurrentPageWM()
  289. {
  290. viewContentViewModel.IsLoading = Visibility.Visible;
  291. await WaitCropCurrentPageWM();
  292. viewContentViewModel.IsLoading = Visibility.Collapsed;
  293. }
  294. private async void CropAllPagesWM()
  295. {
  296. viewContentViewModel.IsLoading = Visibility.Visible;
  297. await WaitCropAllPagesWM();
  298. viewContentViewModel.IsLoading = Visibility.Collapsed;
  299. //PDFViewer.UndoManager.AddHistory();
  300. }
  301. private async Task WaitCropCurrentPageWM()
  302. {
  303. //判断本地有没有token,没有显示登录,有再判断登录状态
  304. if (Settings.Default.AppProperties.LoginToken != "")
  305. {
  306. if (ServiceHelper.GetUser() == "401")
  307. {
  308. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  309. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  310. App.mainWindowViewModel.OphVis = Visibility.Visible;
  311. }
  312. }
  313. else
  314. {
  315. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  316. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  317. App.mainWindowViewModel.OphVis = Visibility.Visible;
  318. }
  319. //付费锁
  320. if (!App.IsLogin)
  321. {
  322. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  323. return;
  324. }
  325. await Task.Run(() =>
  326. {
  327. if (PDFViewer.CurrentIndex >= 0)
  328. {
  329. UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  330. }
  331. UndoManager.ADDCropCurrentPageWM();
  332. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  333. PDFViewer.UndoManager.AddHistory(UndoManager);
  334. PDFViewer.UndoManager.CanSave = true;
  335. });
  336. }
  337. private async Task WaitCropAllPagesWM()
  338. {
  339. //判断本地有没有token,没有显示登录,有再判断登录状态
  340. if (Settings.Default.AppProperties.LoginToken != "")
  341. {
  342. if (ServiceHelper.GetUser() == "401")
  343. {
  344. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  345. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  346. App.mainWindowViewModel.OphVis = Visibility.Visible;
  347. }
  348. }
  349. else
  350. {
  351. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  352. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  353. App.mainWindowViewModel.OphVis = Visibility.Visible;
  354. }
  355. //付费锁
  356. if (!App.IsLogin)
  357. {
  358. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  359. return;
  360. }
  361. await Task.Run(() =>
  362. {
  363. List<int> cropPagesList = new List<int>();
  364. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  365. {
  366. cropPagesList.Add(i);
  367. }
  368. UndoManager.cropPageList.AddRange(cropPagesList);
  369. UndoManager.ADDCropAllPagesWM();
  370. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  371. PDFViewer.UndoManager.AddHistory(UndoManager);
  372. PDFViewer.UndoManager.CanSave = true;
  373. });
  374. }
  375. public bool IsNavigationTarget(NavigationContext navigationContext)
  376. {
  377. return true;
  378. }
  379. public void OnNavigatedFrom(NavigationContext navigationContext)
  380. {
  381. }
  382. public void OnNavigatedTo(NavigationContext navigationContext)
  383. {
  384. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  385. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  386. if (PDFViewer != null)
  387. {
  388. UndoManager.setPDFViewer(PDFViewer);
  389. //DFViewer.UndoManager.AddHistory(UndoManager);
  390. isFirstLoad = false;
  391. }
  392. }
  393. }
  394. }