ToolsBarContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 async void OpenCompressDialog()
  166. {
  167. await viewContentViewModel.SaveBeforeOption();
  168. DialogParameters value = new DialogParameters();
  169. value.Add(ParameterNames.PDFViewer, PDFViewer);
  170. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  171. {
  172. CompressDialogModel compressDialogModel = new CompressDialogModel();
  173. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  174. // OnOpened((Prism.Services.Dialogs.DialogResult)e);
  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. await viewContentViewModel.SaveBeforeOption();
  219. DialogParameters value = new DialogParameters();
  220. value.Add(ParameterNames.PDFViewer, PDFViewer);
  221. value.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  222. value.Add(ParameterNames.Unicode, unicode);
  223. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  224. }
  225. private void OpenSetPasswordDialog()
  226. {
  227. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  228. if (result.IsDiscryptied)
  229. {
  230. if (result.Password != null)
  231. {
  232. PDFViewer.Document.CheckOwnerPassword(result.Password);
  233. }
  234. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  235. DialogParameters value = new DialogParameters();
  236. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  237. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  238. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  239. {
  240. if (e.Result == ButtonResult.OK)
  241. {
  242. SecurityHelper.IsPasswordChanged = true;
  243. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  244. {
  245. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  246. }
  247. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  248. }
  249. });
  250. }
  251. }
  252. private void OpenCancelPasswordDialog()
  253. {
  254. ///无可用安全性设置的原因:
  255. ///文件本身无安全性设置,且没有设置过密码
  256. ///文件安全性已经被重置
  257. ///
  258. if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
  259. {
  260. AlertsMessage alertsMessage = new AlertsMessage();
  261. alertsMessage.ShowDialog("Message", "No security settings available ", "OK");
  262. }
  263. else
  264. {
  265. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  266. if (result.IsDiscryptied)
  267. {
  268. if (result.Password != null)
  269. {
  270. PDFViewer.Document.CheckOwnerPassword(result.Password);
  271. }
  272. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  273. DialogParameters value = new DialogParameters();
  274. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  275. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  276. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e =>
  277. {
  278. if (e.Result == ButtonResult.OK)
  279. {
  280. if (viewContentViewModel.SecurityInfo.IsPasswordRemoved)
  281. {
  282. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  283. }
  284. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
  285. }
  286. });
  287. }
  288. }
  289. }
  290. private async void CropCurrentPageWM()
  291. {
  292. viewContentViewModel.IsLoading = Visibility.Visible;
  293. await WaitCropCurrentPageWM();
  294. viewContentViewModel.IsLoading = Visibility.Collapsed;
  295. }
  296. private async void CropAllPagesWM()
  297. {
  298. viewContentViewModel.IsLoading = Visibility.Visible;
  299. await WaitCropAllPagesWM();
  300. viewContentViewModel.IsLoading = Visibility.Collapsed;
  301. //PDFViewer.UndoManager.AddHistory();
  302. }
  303. private async Task WaitCropCurrentPageWM()
  304. {
  305. //判断本地有没有token,没有显示登录,有再判断登录状态
  306. if (Settings.Default.AppProperties.LoginToken != "")
  307. {
  308. if (ServiceHelper.GetUser() == "401")
  309. {
  310. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  311. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  312. App.mainWindowViewModel.OphVis = Visibility.Visible;
  313. }
  314. }
  315. else
  316. {
  317. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  318. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  319. App.mainWindowViewModel.OphVis = Visibility.Visible;
  320. }
  321. //付费锁
  322. if (!App.IsLogin)
  323. {
  324. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  325. return;
  326. }
  327. await Task.Run(() =>
  328. {
  329. if (PDFViewer.CurrentIndex >= 0)
  330. {
  331. UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  332. }
  333. UndoManager.ADDCropCurrentPageWM();
  334. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  335. PDFViewer.UndoManager.AddHistory(UndoManager);
  336. PDFViewer.UndoManager.CanSave = true;
  337. });
  338. }
  339. private async Task WaitCropAllPagesWM()
  340. {
  341. //判断本地有没有token,没有显示登录,有再判断登录状态
  342. if (Settings.Default.AppProperties.LoginToken != "")
  343. {
  344. if (ServiceHelper.GetUser() == "401")
  345. {
  346. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  347. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  348. App.mainWindowViewModel.OphVis = Visibility.Visible;
  349. }
  350. }
  351. else
  352. {
  353. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  354. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  355. App.mainWindowViewModel.OphVis = Visibility.Visible;
  356. }
  357. //付费锁
  358. if (!App.IsLogin)
  359. {
  360. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  361. return;
  362. }
  363. await Task.Run(() =>
  364. {
  365. List<int> cropPagesList = new List<int>();
  366. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  367. {
  368. cropPagesList.Add(i);
  369. }
  370. UndoManager.cropPageList.AddRange(cropPagesList);
  371. UndoManager.ADDCropAllPagesWM();
  372. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  373. PDFViewer.UndoManager.AddHistory(UndoManager);
  374. PDFViewer.UndoManager.CanSave = true;
  375. });
  376. }
  377. public bool IsNavigationTarget(NavigationContext navigationContext)
  378. {
  379. return true;
  380. }
  381. public void OnNavigatedFrom(NavigationContext navigationContext)
  382. {
  383. }
  384. public void OnNavigatedTo(NavigationContext navigationContext)
  385. {
  386. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  387. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  388. if (PDFViewer != null)
  389. {
  390. UndoManager.setPDFViewer(PDFViewer);
  391. //DFViewer.UndoManager.AddHistory(UndoManager);
  392. isFirstLoad = false;
  393. }
  394. }
  395. }
  396. }