ToolsBarContentViewModel.cs 9.9 KB

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