ToolsBarContentViewModel.cs 15 KB

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