ToolsBarContentViewModel.cs 12 KB

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