HomePageSetPasswordDialogViewModel.cs 18 KB

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