ToolsBarContentViewModel.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. OnOpened((Prism.Services.Dialogs.DialogResult)e);
  96. });
  97. }
  98. private int GetIndex(int pageindex)
  99. {
  100. PageIndex = (pageindex+1).ToString();
  101. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  102. if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;}
  103. return 0;
  104. }
  105. private void CompressClear()
  106. {
  107. tempDocument.CompressFile_Cancel(compressingIntpr);
  108. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  109. }
  110. public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  111. {
  112. if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; }
  113. CPDFDocument doc = null;
  114. string filepath = "";
  115. string password = "";
  116. int compressquality = 45;
  117. doc=e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  118. filepath= e.Parameters.GetValue<string>(ParameterNames.FilePath);
  119. password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  120. compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  121. if (doc != null)
  122. {
  123. PageNumber = doc.PageCount.ToString();
  124. CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  125. document.UnlockWithPassword(password);
  126. indexDelegate += GetIndex;
  127. compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  128. //GC.KeepAlive(indexDelegate);
  129. tempDocument = document;
  130. Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  131. App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  132. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  133. CommonHelper.ShowFileBrowser(filepath);
  134. document.Release();
  135. }
  136. }
  137. private void MergeDialog()
  138. {
  139. DialogParameters value = new DialogParameters();
  140. value.Add(ParameterNames.PDFViewer, PDFViewer);
  141. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  142. }
  143. private void OpenSetPasswordDialog()
  144. {
  145. VerifyPasswordResult result = SecurityHelper.VerifyPasswordForSelectedPermissions(PDFViewer.Document, EnumPermissionsSet.StatusAllowsPrinting|EnumPermissionsSet.StatusAllowsHighQualityPrinting, dialogs);
  146. if (result.IsDiscryptied)
  147. {
  148. if (result.Password != null)
  149. {
  150. string filePath = PDFViewer.Document.FilePath;
  151. PDFViewer.Document.Release();
  152. PDFViewer.InitDocument(filePath);
  153. PDFViewer.Document.UnlockWithPassword(result.Password);
  154. }
  155. DialogParameters value = new DialogParameters();
  156. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  157. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e => { });
  158. }
  159. }
  160. private void OpenCancelPasswordDialog()
  161. {
  162. if (!PDFViewer.Document.IsEncrypted)
  163. {
  164. MessageBoxEx.Show("No security settings available ");
  165. }
  166. else
  167. {
  168. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  169. if (result.IsDiscryptied)
  170. {
  171. if (result.Password != null)
  172. {
  173. string filePath = PDFViewer.Document.FilePath;
  174. PDFViewer.Document.Release();
  175. PDFViewer.InitDocument(filePath);
  176. PDFViewer.Document.UnlockWithPassword(result.Password);
  177. }
  178. DialogParameters value = new DialogParameters();
  179. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  180. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
  181. }
  182. }
  183. }
  184. public bool IsNavigationTarget(NavigationContext navigationContext)
  185. {
  186. return true;
  187. }
  188. public void OnNavigatedFrom(NavigationContext navigationContext)
  189. {
  190. }
  191. public void OnNavigatedTo(NavigationContext navigationContext)
  192. {
  193. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  194. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  195. if (PDFViewer != null)
  196. {
  197. isFirstLoad = false;
  198. }
  199. }
  200. }
  201. }