ToolsBarContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. if (!viewContentViewModel.IAPBeforeFunction())
  168. {
  169. return;
  170. }
  171. if (await viewContentViewModel.SaveBeforeOption())
  172. {
  173. DialogParameters value = new DialogParameters();
  174. value.Add(ParameterNames.PDFViewer, PDFViewer);
  175. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  176. {
  177. CompressDialogModel compressDialogModel = new CompressDialogModel();
  178. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  179. // OnOpened((Prism.Services.Dialogs.DialogResult)e);
  180. });
  181. }
  182. }
  183. //private int GetIndex(int pageindex)
  184. //{
  185. // PageIndex = (pageindex + 1).ToString();
  186. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  187. // if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  188. // return 0;
  189. //}
  190. //private void CompressClear()
  191. //{
  192. // tempDocument.CompressFile_Cancel(compressingIntpr);
  193. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  194. //}
  195. //public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  196. //{
  197. // if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; }
  198. // CPDFDocument doc = null;
  199. // string filepath = "";
  200. // string password = "";
  201. // int compressquality = 45;
  202. // doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  203. // filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  204. // password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  205. // compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  206. // if (doc != null)
  207. // {
  208. // PageNumber = doc.PageCount.ToString();
  209. // CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  210. // document.UnlockWithPassword(password);
  211. // indexDelegate += GetIndex;
  212. // compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  213. // //GC.KeepAlive(indexDelegate);
  214. // tempDocument = document;
  215. // Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  216. // App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  217. // await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  218. // CommonHelper.ShowFileBrowser(filepath);
  219. // document.Release();
  220. // }
  221. //}
  222. private async void MergeDialog()
  223. {
  224. if (!viewContentViewModel.IAPBeforeFunction())
  225. {
  226. return;
  227. }
  228. if (await viewContentViewModel.SaveBeforeOption())
  229. {
  230. DialogParameters value = new DialogParameters();
  231. value.Add(ParameterNames.PDFViewer, PDFViewer);
  232. value.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  233. value.Add(ParameterNames.Unicode, unicode);
  234. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  235. }
  236. }
  237. private void OpenSetPasswordDialog()
  238. {
  239. if (!viewContentViewModel.IAPBeforeFunction())
  240. {
  241. return;
  242. }
  243. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  244. if (result.IsDiscryptied)
  245. {
  246. if (result.Password != null)
  247. {
  248. PDFViewer.Document.CheckOwnerPassword(result.Password);
  249. }
  250. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  251. DialogParameters value = new DialogParameters();
  252. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  253. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  254. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  255. {
  256. if (e.Result == ButtonResult.OK)
  257. {
  258. SecurityHelper.IsPasswordChanged = true;
  259. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  260. {
  261. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  262. }
  263. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  264. }
  265. });
  266. }
  267. }
  268. private void OpenCancelPasswordDialog()
  269. {
  270. ///无可用安全性设置的原因:
  271. ///文件本身无安全性设置,且没有设置过密码
  272. ///文件安全性已经被重置
  273. ///
  274. if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
  275. {
  276. AlertsMessage alertsMessage = new AlertsMessage();
  277. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("NoPasswrod_Waring"), App.ServiceLoader.GetString("Text_ok"));
  278. }
  279. else
  280. {
  281. if (!viewContentViewModel.IAPBeforeFunction())
  282. {
  283. return;
  284. }
  285. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  286. if (result.IsDiscryptied)
  287. {
  288. if (result.Password != null)
  289. {
  290. PDFViewer.Document.CheckOwnerPassword(result.Password);
  291. }
  292. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  293. DialogParameters value = new DialogParameters();
  294. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  295. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  296. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e =>
  297. {
  298. if (e.Result == ButtonResult.OK)
  299. {
  300. if (viewContentViewModel.SecurityInfo.IsPasswordRemoved)
  301. {
  302. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  303. }
  304. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
  305. }
  306. });
  307. }
  308. }
  309. }
  310. private async void CropCurrentPageWM()
  311. {
  312. viewContentViewModel.IsLoading = Visibility.Visible;
  313. await WaitCropCurrentPageWM();
  314. viewContentViewModel.IsLoading = Visibility.Collapsed;
  315. }
  316. private async void CropAllPagesWM()
  317. {
  318. viewContentViewModel.IsLoading = Visibility.Visible;
  319. await WaitCropAllPagesWM();
  320. viewContentViewModel.IsLoading = Visibility.Collapsed;
  321. //PDFViewer.UndoManager.AddHistory();
  322. }
  323. private async Task WaitCropCurrentPageWM()
  324. {
  325. if (!viewContentViewModel.IAPBeforeFunction())
  326. {
  327. return;
  328. }
  329. await Task.Run(() =>
  330. {
  331. if (PDFViewer.CurrentIndex >= 0)
  332. {
  333. UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  334. }
  335. UndoManager.ADDCropCurrentPageWM();
  336. //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  337. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  338. PDFViewer.UndoManager.AddHistory(UndoManager);
  339. PDFViewer.UndoManager.CanSave = true;
  340. });
  341. }
  342. private async Task WaitCropAllPagesWM()
  343. {
  344. if (!viewContentViewModel.IAPBeforeFunction())
  345. {
  346. return;
  347. }
  348. await Task.Run(() =>
  349. {
  350. List<int> cropPagesList = new List<int>();
  351. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  352. {
  353. cropPagesList.Add(i);
  354. }
  355. UndoManager.cropPageList.AddRange(cropPagesList);
  356. UndoManager.ADDCropAllPagesWM();
  357. //此法不会直接保存,只是设置是否被裁减(只对pdfview裁剪,不是真正的裁剪),需要保存后调用PdfViewer?.CropPage进行真正裁剪
  358. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  359. PDFViewer.UndoManager.AddHistory(UndoManager);
  360. PDFViewer.UndoManager.CanSave = true;
  361. });
  362. }
  363. public bool IsNavigationTarget(NavigationContext navigationContext)
  364. {
  365. return true;
  366. }
  367. public void OnNavigatedFrom(NavigationContext navigationContext)
  368. {
  369. }
  370. public void OnNavigatedTo(NavigationContext navigationContext)
  371. {
  372. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  373. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  374. if (PDFViewer != null)
  375. {
  376. UndoManager.setPDFViewer(PDFViewer);
  377. //DFViewer.UndoManager.AddHistory(UndoManager);
  378. isFirstLoad = false;
  379. }
  380. }
  381. }
  382. }