ToolsBarContentViewModel.cs 16 KB

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