ToolsBarContentViewModel.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Interop.Word;
  4. using PDF_Office.CustomControl;
  5. using PDF_Office.EventAggregators;
  6. using PDF_Office.Helper;
  7. using PDF_Office.Model;
  8. using PDF_Office.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_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  22. using Task = System.Threading.Tasks.Task;
  23. namespace PDF_Office.ViewModels.Tools
  24. {
  25. public class ToolsBarContentViewModel : BindableBase, INavigationAware
  26. {
  27. private CPDFViewer PDFViewer;
  28. private ViewContentViewModel viewContentViewModel;
  29. public IDialogService dialogs;
  30. public IEventAggregator eventAggregator;
  31. private IntPtr compressingIntpr = IntPtr.Zero;
  32. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  33. private CPDFDocument tempDocument;
  34. public string unicode = null;
  35. /// <summary>
  36. /// 是否是第一次加载
  37. /// </summary>
  38. private bool isFirstLoad = true;
  39. private string _pageIndex = "0";
  40. /// <value>
  41. /// 当前页
  42. /// </value>
  43. public string PageIndex
  44. {
  45. get { return _pageIndex; }
  46. set
  47. {
  48. SetProperty(ref _pageIndex, value);
  49. App.mainWindowViewModel.Value = int.Parse(PageIndex);
  50. }
  51. }
  52. private string _pageNumber = "";
  53. /// <value>
  54. /// 总页数
  55. /// </value>
  56. public string PageNumber
  57. {
  58. get { return _pageNumber; }
  59. set
  60. {
  61. SetProperty(ref _pageNumber, value);
  62. App.mainWindowViewModel.MaxValue = int.Parse(PageNumber);
  63. }
  64. }
  65. public DelegateCommand CompressCommand { get; set; }
  66. public DelegateCommand MergeCommand { get; set; }
  67. public DelegateCommand SetPasswordCommand { get; set; }
  68. public DelegateCommand CancelPasswordCommand { get; set; }
  69. public DelegateCommand<object> SetEditToolsCommand { get; set; }
  70. public DelegateCommand SetWatermarkCommand { get; set; }
  71. public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  72. {
  73. dialogs = dialogService;
  74. this.eventAggregator = eventAggregator;
  75. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  76. CompressCommand = new DelegateCommand(OpenCompressDialog);
  77. MergeCommand = new DelegateCommand(MergeDialog);
  78. SetPasswordCommand = new DelegateCommand(OpenSetPasswordDialog);
  79. CancelPasswordCommand = new DelegateCommand(OpenCancelPasswordDialog);
  80. SetEditToolsCommand = new DelegateCommand<object>(SetEditTools);
  81. }
  82. private void SetEditTools(object e)
  83. {
  84. var args = e as System.Windows.Controls.Button;
  85. if (args != null)
  86. {
  87. this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
  88. }
  89. }
  90. private void OpenCompressDialog()
  91. {
  92. DialogParameters value = new DialogParameters();
  93. value.Add(ParameterNames.PDFViewer, PDFViewer);
  94. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  95. {
  96. OnOpened((Prism.Services.Dialogs.DialogResult)e);
  97. });
  98. }
  99. private int GetIndex(int pageindex)
  100. {
  101. PageIndex = (pageindex + 1).ToString();
  102. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  103. if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  104. return 0;
  105. }
  106. private void CompressClear()
  107. {
  108. tempDocument.CompressFile_Cancel(compressingIntpr);
  109. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  110. }
  111. public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  112. {
  113. if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; }
  114. CPDFDocument doc = null;
  115. string filepath = "";
  116. string password = "";
  117. int compressquality = 45;
  118. doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  119. filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  120. password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  121. compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  122. if (doc != null)
  123. {
  124. PageNumber = doc.PageCount.ToString();
  125. CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  126. document.UnlockWithPassword(password);
  127. indexDelegate += GetIndex;
  128. compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  129. //GC.KeepAlive(indexDelegate);
  130. tempDocument = document;
  131. Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  132. App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  133. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  134. CommonHelper.ShowFileBrowser(filepath);
  135. document.Release();
  136. }
  137. }
  138. private void MergeDialog()
  139. {
  140. DialogParameters value = new DialogParameters();
  141. value.Add(ParameterNames.PDFViewer, PDFViewer);
  142. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  143. }
  144. private void OpenSetPasswordDialog()
  145. {
  146. if (!viewContentViewModel.SecurityInfo.IsPasswordChanged)
  147. {
  148. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  149. if (result.IsDiscryptied)
  150. {
  151. if (result.Password != null)
  152. {
  153. string filePath = PDFViewer.Document.FilePath;
  154. PDFViewer.Document.Release();
  155. PDFViewer.InitDocument(filePath);
  156. PDFViewer.Document.UnlockWithPassword(result.Password);
  157. }
  158. }
  159. }
  160. DialogParameters value = new DialogParameters();
  161. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  162. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  163. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  164. {
  165. if (e.Result == ButtonResult.OK)
  166. {
  167. SecurityHelper.IsPasswordChanged = true;
  168. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  169. {
  170. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  171. }
  172. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  173. }
  174. });
  175. }
  176. private void OpenCancelPasswordDialog()
  177. {
  178. if (!PDFViewer.Document.IsEncrypted)
  179. {
  180. MessageBoxEx.Show("No security settings available ");
  181. }
  182. else
  183. {
  184. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  185. if (result.IsDiscryptied)
  186. {
  187. if (result.Password != null)
  188. {
  189. string filePath = PDFViewer.Document.FilePath;
  190. PDFViewer.Document.Release();
  191. PDFViewer.InitDocument(filePath);
  192. PDFViewer.Document.UnlockWithPassword(result.Password);
  193. }
  194. DialogParameters value = new DialogParameters();
  195. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  196. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
  197. }
  198. }
  199. }
  200. public bool IsNavigationTarget(NavigationContext navigationContext)
  201. {
  202. return true;
  203. }
  204. public void OnNavigatedFrom(NavigationContext navigationContext)
  205. {
  206. }
  207. public void OnNavigatedTo(NavigationContext navigationContext)
  208. {
  209. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  210. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  211. if (PDFViewer != null)
  212. {
  213. isFirstLoad = false;
  214. }
  215. }
  216. }
  217. }