HomePageSetPasswordDialogViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Windows;
  14. using System.Windows.Forms;
  15. using DialogResult = Prism.Services.Dialogs.DialogResult;
  16. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing
  17. {
  18. public class HomePageSetPasswordDialogViewModel : BindableBase, INavigationAware
  19. {
  20. private List<string> fileNames;
  21. private int fileNamesIndex = 0;
  22. private HomePageSetPasswordDialogModel setPasswordDialogModel = new HomePageSetPasswordDialogModel();
  23. public List<int> fileNamesView = new List<int>();
  24. private bool _canOpen;
  25. public bool CanOpen
  26. {
  27. get { return _canOpen; }
  28. set
  29. {
  30. _canOpen = value;
  31. RaisePropertyChanged("isChecked");
  32. }
  33. }
  34. private bool _canEdit;
  35. public bool CanEdit
  36. {
  37. get { return _canEdit; }
  38. set
  39. {
  40. _canEdit = value;
  41. RaisePropertyChanged("isChecked");
  42. }
  43. }
  44. private string _changeMod = "0";
  45. ///<value>
  46. ///"0"为ChangeMod.None;
  47. ///"1"为ChangeMod.ChangePage;
  48. ///"2"为ChangeMod.FormAndSignature;
  49. ///"3"为ChangeMod.AnnotAndFormAndSignature;
  50. ///"4"为ChangeMod.ExceptAbstrat;
  51. ///</value>
  52. public string ChangeMod
  53. {
  54. get { return _changeMod; }
  55. set
  56. {
  57. _changeMod = value;
  58. }
  59. }
  60. private string _printMod = "0";
  61. ///<value>
  62. ///"0"为PrintMod.None;
  63. ///"1"为PrintMod.LowDpi;
  64. ///"2"为PrintMod.HighDpi;
  65. ///</value>
  66. public string PrintMod
  67. {
  68. get { return _printMod; }
  69. set
  70. {
  71. _printMod = value;
  72. }
  73. }
  74. private string safetyGridIsEnabled = "True";
  75. public string SafetyGridIsEnabled
  76. {
  77. get
  78. {
  79. return safetyGridIsEnabled;
  80. }
  81. set
  82. {
  83. SetProperty(ref safetyGridIsEnabled, value);
  84. }
  85. }
  86. private string setSafetyGridIsEnabled = "True";
  87. public string SetSafetyGridIsEnabled
  88. {
  89. get
  90. {
  91. return setSafetyGridIsEnabled;
  92. }
  93. set
  94. {
  95. SetProperty(ref setSafetyGridIsEnabled, value);
  96. }
  97. }
  98. private string _isEnableConfirm = "False";
  99. public string IsEnabledConfirm
  100. {
  101. get { return _isEnableConfirm; }
  102. set
  103. {
  104. SetProperty(ref _isEnableConfirm, value);
  105. }
  106. }
  107. private DataTable pdfDataTable = new DataTable();
  108. public DataTable PDFDataTable
  109. {
  110. get { return pdfDataTable; }
  111. set
  112. {
  113. SetProperty(ref pdfDataTable, value);
  114. }
  115. }
  116. private string removeIsEnable = "False";
  117. public string RemoveIsEnable
  118. {
  119. get { return removeIsEnable; }
  120. set
  121. {
  122. SetProperty(ref removeIsEnable, value);
  123. }
  124. }
  125. private string selectFileName = "False";
  126. public string SelectFileName
  127. {
  128. get { return selectFileName; }
  129. set
  130. {
  131. SetProperty(ref selectFileName, value);
  132. }
  133. }
  134. private Visibility addFileVisibility = Visibility.Hidden;
  135. public Visibility AddFileVisibility
  136. {
  137. get { return addFileVisibility; }
  138. set
  139. {
  140. SetProperty(ref addFileVisibility, value);
  141. RaisePropertyChanged();
  142. }
  143. }
  144. public DelegateCommand DelegateSetOpenCommand { get; set; }
  145. public DelegateCommand DelegateSetEditCommand { get; set; }
  146. public DelegateCommand DelegateCanOpenTextChangedCommand { get; set; }
  147. public DelegateCommand DelegateCanEditTextChangedCommand { get; set; }
  148. public DelegateCommand DelegateConfirmEncryptCommand { get; set; }
  149. public DelegateCommand DelegateCancelEncryptCommand { get; set; }
  150. public DelegateCommand ADDPDFCommand { get; set; }
  151. public DelegateCommand RemovePDFFileCommand { get; set; }
  152. public DelegateCommand ADDPDFFilesCommand { get; set; }
  153. public HomePageSetPasswordDialogViewModel()
  154. {
  155. PDFDataTable.Columns.Add("FileName");
  156. PDFDataTable.Columns.Add("FileSize");
  157. PDFDataTable.Columns.Add("FileState");
  158. HomePageSetPasswordDialogModel.PasswordForOpen = "";
  159. HomePageSetPasswordDialogModel.PasswordForEdit = "";
  160. DelegateConfirmEncryptCommand = new DelegateCommand(ConfirmEncrypt);
  161. DelegateSetOpenCommand = new DelegateCommand(SetCanOpen);
  162. DelegateSetEditCommand = new DelegateCommand(SetCanEdit);
  163. DelegateCanOpenTextChangedCommand = new DelegateCommand(CanOpenTextChanged);
  164. DelegateCanEditTextChangedCommand = new DelegateCommand(CanEditTextChanged);
  165. ADDPDFCommand = new DelegateCommand(addpdf);
  166. ADDPDFFilesCommand = new DelegateCommand(addpdffiles);
  167. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  168. }
  169. #region 检查和初始化
  170. private void CheckCanConfirmEncrypt()
  171. {
  172. if (setPasswordDialogModel.CanOpen)
  173. {
  174. if (HomePageSetPasswordDialogModel.PasswordForOpen.Length > 0)
  175. {
  176. IsEnabledConfirm = "True";
  177. return;
  178. }
  179. }
  180. if (setPasswordDialogModel.CanEdit)
  181. {
  182. if (HomePageSetPasswordDialogModel.PasswordForEdit.Length > 0)
  183. {
  184. IsEnabledConfirm = "True";
  185. return;
  186. }
  187. }
  188. IsEnabledConfirm = "False";
  189. }
  190. private void InitPermissionsDictionary(ref Dictionary<string, HomePageSetPasswordDialogModel.PrintMod> GetPrintMod, ref Dictionary<string, HomePageSetPasswordDialogModel.ChangeMod> GetChangeMod)
  191. {
  192. GetPrintMod.Add("0", HomePageSetPasswordDialogModel.PrintMod.None);
  193. GetPrintMod.Add("1", HomePageSetPasswordDialogModel.PrintMod.LowDpi);
  194. GetPrintMod.Add("2", HomePageSetPasswordDialogModel.PrintMod.HighDpi);
  195. GetChangeMod.Add("0", HomePageSetPasswordDialogModel.ChangeMod.None);
  196. GetChangeMod.Add("1", HomePageSetPasswordDialogModel.ChangeMod.ChangePage);
  197. GetChangeMod.Add("2", HomePageSetPasswordDialogModel.ChangeMod.FormAndSignature);
  198. GetChangeMod.Add("3", HomePageSetPasswordDialogModel.ChangeMod.AnnotAndFormAndSignature);
  199. GetChangeMod.Add("4", HomePageSetPasswordDialogModel.ChangeMod.ExceptAbstrat);
  200. }
  201. #endregion
  202. /// <summary>
  203. /// 添加PDF文件
  204. /// </summary>
  205. private void addpdf()
  206. {
  207. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  208. dlg.Multiselect = true;
  209. dlg.Filter = "PDF|*.pdf;*.PDF;";
  210. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  211. {
  212. fileNames.AddRange(dlg.FileNames.ToList());
  213. RemoveExcess(ref fileNames);
  214. SetSafetyGridIsEnabled = "True";
  215. AddFileVisibility = Visibility.Collapsed;
  216. updateListview("待确定");
  217. }
  218. }
  219. /// <summary>
  220. /// 删除重复的文件
  221. /// </summary>
  222. public void RemoveExcess(ref List<string> Filenames)
  223. {
  224. List<string> filenames = new List<string>();
  225. foreach (var fileName in Filenames)
  226. {
  227. if (!filenames.Contains(fileName))
  228. {
  229. filenames.Add(fileName);
  230. }
  231. }
  232. Filenames.Clear();
  233. Filenames = filenames;
  234. }
  235. /// <summary>
  236. /// 添加PDF文件夹
  237. /// </summary>
  238. private void addpdffiles()
  239. {
  240. FolderBrowserDialog dialog = new FolderBrowserDialog();
  241. dialog.Description = "请选择文件路径";
  242. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  243. {
  244. string foldPath = dialog.SelectedPath;
  245. var apps = System.IO.Directory.GetFiles(foldPath);
  246. foreach (string app in apps)
  247. {
  248. var fi = new FileInfo(app);
  249. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  250. {
  251. fileNames.Add(app);
  252. }
  253. }
  254. RemoveExcess(ref fileNames);
  255. updateListview("待确定");
  256. SetSafetyGridIsEnabled = "True";
  257. AddFileVisibility = Visibility.Collapsed;
  258. }
  259. }
  260. /// <summary>
  261. /// 更新listview显示
  262. /// state 状态显示字符串
  263. /// </summary>
  264. public void updateListview(string state)
  265. {
  266. DataTable pdfdatatable = new DataTable();
  267. pdfdatatable.Columns.Add("FileName");
  268. pdfdatatable.Columns.Add("FileSize");
  269. pdfdatatable.Columns.Add("FileState");
  270. foreach (var fileName in fileNames)
  271. {
  272. string file_all = fileName;
  273. FileInfo f = new FileInfo(file_all);
  274. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  275. pdfdatatable.Rows.Add(f.Name, file_size, state);
  276. }
  277. PDFDataTable = pdfdatatable;
  278. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  279. }
  280. public void SetCanOpen()
  281. {
  282. setPasswordDialogModel.CanOpen = CanOpen;
  283. CheckCanConfirmEncrypt();
  284. }
  285. public void SetCanEdit()
  286. {
  287. setPasswordDialogModel.CanEdit = CanEdit;
  288. CheckCanConfirmEncrypt();
  289. }
  290. public void CanOpenTextChanged()
  291. {
  292. CheckCanConfirmEncrypt();
  293. }
  294. public void CanEditTextChanged()
  295. {
  296. CheckCanConfirmEncrypt();
  297. }
  298. public void ConfirmEncrypt()
  299. {
  300. SafetyGridIsEnabled = "False";
  301. string openPassword = "";
  302. string editPassword = "";
  303. CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
  304. if (setPasswordDialogModel.CanOpen)
  305. {
  306. if (!string.IsNullOrEmpty(HomePageSetPasswordDialogModel.PasswordForOpen))
  307. {
  308. openPassword = HomePageSetPasswordDialogModel.PasswordForOpen;
  309. }
  310. }
  311. if (setPasswordDialogModel.CanEdit)
  312. {
  313. if (!string.IsNullOrEmpty(HomePageSetPasswordDialogModel.PasswordForEdit))
  314. {
  315. editPassword = HomePageSetPasswordDialogModel.PasswordForEdit;
  316. Dictionary<string, HomePageSetPasswordDialogModel.PrintMod> GetPrintMod = new Dictionary<string, HomePageSetPasswordDialogModel.PrintMod>();
  317. Dictionary<string, HomePageSetPasswordDialogModel.ChangeMod> GetChangeMod = new Dictionary<string, HomePageSetPasswordDialogModel.ChangeMod>(); ;
  318. InitPermissionsDictionary(ref GetPrintMod, ref GetChangeMod);
  319. permissionsInfo = setPasswordDialogModel.CreatePermissionsInfo(GetPrintMod[PrintMod], GetChangeMod[ChangeMod]);
  320. }
  321. }
  322. fileNamesIndex = 0;
  323. foreach (var filename in fileNames)
  324. {
  325. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "待完成";
  326. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  327. if (document == null)
  328. {
  329. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  330. continue;
  331. }
  332. FileInfo fileinfo = new FileInfo(filename);
  333. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  334. if (document == null)
  335. {
  336. return;
  337. }
  338. document.Encrypt(openPassword, editPassword, permissionsInfo);
  339. document.WriteToFilePath(filename + "SetPassword");
  340. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  341. fileNamesIndex++;
  342. document.Release();
  343. }
  344. SafetyGridIsEnabled = "True";
  345. MessageBoxEx.Show("已完成");
  346. SetSafetyGridIsEnabled = "False";
  347. AddFileVisibility = Visibility.Visible;
  348. fileNames.Clear();
  349. HomePageBatchProcessingDialogModel.FilePaths.Clear();
  350. }
  351. /// <summary>
  352. /// 逆序int类型集合
  353. /// </summary>
  354. public void Reverseorder(ref List<int> Numbers)
  355. {
  356. Numbers = Numbers.OrderBy(a => a).ToList();
  357. Numbers.Reverse();
  358. }
  359. /// <summary>
  360. /// 打开文件PDF
  361. /// </summary>
  362. public void openfiledialog()
  363. {
  364. foreach (int filenamesview in fileNamesView)
  365. {
  366. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  367. }
  368. }
  369. /// <summary>
  370. /// 删除文件PDF
  371. /// </summary>
  372. public void removepdffile()
  373. {
  374. Reverseorder(ref fileNamesView);
  375. foreach (int filenamesview in fileNamesView)
  376. {
  377. //Trace.WriteLine(filenamesview);
  378. fileNames.Remove(fileNames[filenamesview]);
  379. }
  380. if (fileNames.Count < 1)
  381. {
  382. SetSafetyGridIsEnabled = "False";
  383. AddFileVisibility = Visibility.Visible;
  384. }
  385. updateListview("待确定");
  386. }
  387. public void PDFFileCount()
  388. {
  389. if (fileNames.Count == 0)
  390. {
  391. SetSafetyGridIsEnabled = "False";
  392. AddFileVisibility = Visibility.Visible;
  393. }
  394. else
  395. {
  396. SetSafetyGridIsEnabled = "True";
  397. AddFileVisibility = Visibility.Collapsed;
  398. }
  399. }
  400. public void OnNavigatedTo(NavigationContext navigationContext)
  401. {
  402. List<string> filepath = new List<string>();
  403. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  404. if (filepath != null)
  405. {
  406. fileNames = filepath;
  407. PDFFileCount();
  408. updateListview("待确定");
  409. }
  410. }
  411. public bool IsNavigationTarget(NavigationContext navigationContext)
  412. {
  413. return true;
  414. }
  415. public void OnNavigatedFrom(NavigationContext navigationContext)
  416. {
  417. }
  418. }
  419. }