HomePageSetPasswordDialogViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 ADDOpenedPDFCommand { get; set; }
  165. public DelegateCommand RemovePDFFileCommand { get; set; }
  166. public DelegateCommand ADDPDFFilesCommand { get; set; }
  167. #endregion
  168. public HomePageSetPasswordDialogViewModel()
  169. {
  170. PDFCurrentDataTable.Columns.Add("FileState");
  171. PDFDataTable.Columns.Add("FileName");
  172. PDFDataTable.Columns.Add("FileSize");
  173. PDFDataTable.Columns.Add("FileState");
  174. HomePageSetPasswordDialogModel.PasswordForOpen = "";
  175. HomePageSetPasswordDialogModel.PasswordForEdit = "";
  176. DelegateConfirmEncryptCommand = new DelegateCommand(ConfirmEncrypt);
  177. DelegateSetOpenCommand = new DelegateCommand(SetCanOpen);
  178. DelegateSetEditCommand = new DelegateCommand(SetCanEdit);
  179. DelegateCanOpenTextChangedCommand = new DelegateCommand(CanOpenTextChanged);
  180. DelegateCanEditTextChangedCommand = new DelegateCommand(CanEditTextChanged);
  181. ADDPDFCommand = new DelegateCommand(addpdf);
  182. ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles);
  183. ADDPDFFilesCommand = new DelegateCommand(addpdffiles);
  184. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  185. }
  186. #region 检查和初始化
  187. private void CheckCanConfirmEncrypt()
  188. {
  189. if (setPasswordDialogModel.CanOpen)
  190. {
  191. if (HomePageSetPasswordDialogModel.PasswordForOpen.Length > 0)
  192. {
  193. IsEnabledConfirm = "True";
  194. return;
  195. }
  196. }
  197. if (setPasswordDialogModel.CanEdit)
  198. {
  199. if (HomePageSetPasswordDialogModel.PasswordForEdit.Length > 0)
  200. {
  201. IsEnabledConfirm = "True";
  202. return;
  203. }
  204. }
  205. IsEnabledConfirm = "False";
  206. }
  207. private void InitPermissionsDictionary(ref Dictionary<string, HomePageSetPasswordDialogModel.PrintMod> GetPrintMod, ref Dictionary<string, HomePageSetPasswordDialogModel.ChangeMod> GetChangeMod)
  208. {
  209. GetPrintMod.Add("0", HomePageSetPasswordDialogModel.PrintMod.None);
  210. GetPrintMod.Add("1", HomePageSetPasswordDialogModel.PrintMod.LowDpi);
  211. GetPrintMod.Add("2", HomePageSetPasswordDialogModel.PrintMod.HighDpi);
  212. GetChangeMod.Add("0", HomePageSetPasswordDialogModel.ChangeMod.None);
  213. GetChangeMod.Add("1", HomePageSetPasswordDialogModel.ChangeMod.ChangePage);
  214. GetChangeMod.Add("2", HomePageSetPasswordDialogModel.ChangeMod.FormAndSignature);
  215. GetChangeMod.Add("3", HomePageSetPasswordDialogModel.ChangeMod.AnnotAndFormAndSignature);
  216. GetChangeMod.Add("4", HomePageSetPasswordDialogModel.ChangeMod.ExceptAbstrat);
  217. }
  218. #endregion
  219. #region 逻辑函数
  220. public void SetCanOpen()
  221. {
  222. setPasswordDialogModel.CanOpen = CanOpen;
  223. CheckCanConfirmEncrypt();
  224. }
  225. public void SetCanEdit()
  226. {
  227. setPasswordDialogModel.CanEdit = CanEdit;
  228. CheckCanConfirmEncrypt();
  229. }
  230. public void CanOpenTextChanged()
  231. {
  232. CheckCanConfirmEncrypt();
  233. }
  234. public void CanEditTextChanged()
  235. {
  236. CheckCanConfirmEncrypt();
  237. }
  238. public void ConfirmEncrypt()
  239. {
  240. SafetyGridIsEnabled = "False";
  241. string openPassword = "";
  242. string editPassword = "";
  243. CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
  244. if (setPasswordDialogModel.CanOpen)
  245. {
  246. if (!string.IsNullOrEmpty(HomePageSetPasswordDialogModel.PasswordForOpen))
  247. {
  248. openPassword = HomePageSetPasswordDialogModel.PasswordForOpen;
  249. }
  250. }
  251. if (setPasswordDialogModel.CanEdit)
  252. {
  253. if (!string.IsNullOrEmpty(HomePageSetPasswordDialogModel.PasswordForEdit))
  254. {
  255. editPassword = HomePageSetPasswordDialogModel.PasswordForEdit;
  256. Dictionary<string, HomePageSetPasswordDialogModel.PrintMod> GetPrintMod = new Dictionary<string, HomePageSetPasswordDialogModel.PrintMod>();
  257. Dictionary<string, HomePageSetPasswordDialogModel.ChangeMod> GetChangeMod = new Dictionary<string, HomePageSetPasswordDialogModel.ChangeMod>(); ;
  258. InitPermissionsDictionary(ref GetPrintMod, ref GetChangeMod);
  259. permissionsInfo = setPasswordDialogModel.CreatePermissionsInfo(GetPrintMod[PrintMod], GetChangeMod[ChangeMod]);
  260. }
  261. }
  262. fileNamesIndex = 0;
  263. foreach (var filename in fileNames)
  264. {
  265. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  266. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  267. if (document == null || document.IsEncrypted)
  268. {
  269. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  270. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  271. fileNamesIndex++;
  272. continue;
  273. }
  274. FileInfo fileinfo = new FileInfo(filename);
  275. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  276. if (document == null)
  277. {
  278. return;
  279. }
  280. document.Encrypt(openPassword, editPassword, permissionsInfo);
  281. document.WriteToFilePath(filename + "SetPassword");
  282. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  283. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  284. fileNamesIndex++;
  285. document.Release();
  286. }
  287. SafetyGridIsEnabled = "True";
  288. }
  289. #endregion
  290. #region 批量处理逻辑函数
  291. /// <summary>
  292. /// 添加PDF文件
  293. /// </summary>
  294. private void addpdf()
  295. {
  296. FileNameNumber = fileNames.Count;
  297. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  298. dlg.Multiselect = true;
  299. dlg.Filter = "PDF|*.pdf;*.PDF;";
  300. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  301. {
  302. fileNames.AddRange(dlg.FileNames.ToList());
  303. RemoveExcess(ref fileNames);
  304. SetSafetyGridIsEnabled = "True";
  305. AddFileVisibility = Visibility.Collapsed;
  306. updateListview("wait");
  307. }
  308. }
  309. public void addPDFFiles(string filename)
  310. {
  311. FileNameNumber = fileNames.Count;
  312. fileNames.Add(filename);
  313. RemoveExcess(ref fileNames);
  314. SetSafetyGridIsEnabled = "True";
  315. AddFileVisibility = Visibility.Collapsed;
  316. updateListview("wait");
  317. }
  318. /// <summary>
  319. /// 添加已打开PDF文件
  320. /// </summary>
  321. public void addOpenedPDFFiles()
  322. {
  323. FileNameNumber = fileNames.Count;
  324. foreach (var filename in App.OpenedFileList)
  325. {
  326. fileNames.Add(filename);
  327. }
  328. RemoveExcess(ref fileNames);
  329. SetSafetyGridIsEnabled = "True";
  330. AddFileVisibility = Visibility.Collapsed;
  331. updateListview("wait");
  332. }
  333. /// <summary>
  334. /// 删除重复的文件
  335. /// </summary>
  336. public void RemoveExcess(ref List<string> Filenames)
  337. {
  338. List<string> filenames = new List<string>();
  339. foreach (var fileName in Filenames)
  340. {
  341. if (!filenames.Contains(fileName))
  342. {
  343. filenames.Add(fileName);
  344. }
  345. }
  346. Filenames.Clear();
  347. Filenames = filenames;
  348. }
  349. /// <summary>
  350. /// 添加PDF文件夹
  351. /// </summary>
  352. private void addpdffiles()
  353. {
  354. FileNameNumber = fileNames.Count;
  355. FolderBrowserDialog dialog = new FolderBrowserDialog();
  356. dialog.Description = "请选择文件路径";
  357. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  358. {
  359. string foldPath = dialog.SelectedPath;
  360. var apps = System.IO.Directory.GetFiles(foldPath);
  361. foreach (string app in apps)
  362. {
  363. var fi = new FileInfo(app);
  364. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  365. {
  366. fileNames.Add(app);
  367. }
  368. }
  369. RemoveExcess(ref fileNames);
  370. updateListview("wait");
  371. SetSafetyGridIsEnabled = "True";
  372. AddFileVisibility = Visibility.Collapsed;
  373. }
  374. }
  375. /// <summary>
  376. /// 更新listview显示
  377. /// state 状态显示字符串
  378. /// </summary>
  379. public void updateListview(string state)
  380. {
  381. updateCurrentListview();
  382. DataTable pdfdatatable = new DataTable();
  383. pdfdatatable.Columns.Add("FileName");
  384. pdfdatatable.Columns.Add("FileSize");
  385. pdfdatatable.Columns.Add("FileState");
  386. int datatableindex = 0;
  387. foreach (var fileName in fileNames)
  388. {
  389. string file_all = fileName;
  390. FileInfo f = new FileInfo(file_all);
  391. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  392. pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  393. datatableindex++;
  394. }
  395. PDFDataTable = pdfdatatable;
  396. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  397. }
  398. /// <summary>
  399. /// 更新Currentlistview显示
  400. /// state 状态显示字符串
  401. /// </summary>
  402. public void updateCurrentListview( string state = "wait")
  403. {
  404. if (fileNames.Count >= FileNameNumber)
  405. {
  406. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  407. {
  408. PDFCurrentDataTable.Rows.Add(state);
  409. }
  410. }
  411. else
  412. {
  413. Reverseorder(ref fileNamesView);
  414. foreach (int filenamesview in fileNamesView)
  415. {
  416. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// 逆序int类型集合
  422. /// </summary>
  423. public void Reverseorder(ref List<int> Numbers)
  424. {
  425. Numbers = Numbers.OrderBy(a => a).ToList();
  426. Numbers.Reverse();
  427. }
  428. /// <summary>
  429. /// 打开文件PDF
  430. /// </summary>
  431. public void openfiledialog()
  432. {
  433. foreach (int filenamesview in fileNamesView)
  434. {
  435. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  436. }
  437. }
  438. /// <summary>
  439. /// 删除文件PDF
  440. /// </summary>
  441. public void removepdffile()
  442. {
  443. FileNameNumber = fileNames.Count;
  444. Reverseorder(ref fileNamesView);
  445. foreach (int filenamesview in fileNamesView)
  446. {
  447. //Trace.WriteLine(filenamesview);
  448. fileNames.Remove(fileNames[filenamesview]);
  449. }
  450. if (fileNames.Count < 1)
  451. {
  452. SetSafetyGridIsEnabled = "False";
  453. AddFileVisibility = Visibility.Visible;
  454. }
  455. updateListview("wait");
  456. }
  457. public void removepdffile(int index)
  458. {
  459. PDFCurrentDataTable.Rows.RemoveAt(index);
  460. fileNames.Remove(fileNames[index]);
  461. if (fileNames.Count < 1)
  462. {
  463. SetSafetyGridIsEnabled = "False";
  464. AddFileVisibility = Visibility.Visible;
  465. }
  466. updateListview("wait");
  467. }
  468. public void PDFFileCount()
  469. {
  470. if (fileNames.Count == 0)
  471. {
  472. SetSafetyGridIsEnabled = "False";
  473. AddFileVisibility = Visibility.Visible;
  474. }
  475. else
  476. {
  477. SetSafetyGridIsEnabled = "True";
  478. AddFileVisibility = Visibility.Collapsed;
  479. }
  480. }
  481. #endregion
  482. public void OnNavigatedTo(NavigationContext navigationContext)
  483. {
  484. List<string> filepath = new List<string>();
  485. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  486. if (filepath != null)
  487. {
  488. fileNames = filepath;
  489. PDFFileCount();
  490. updateListview("wait");
  491. }
  492. }
  493. public bool IsNavigationTarget(NavigationContext navigationContext)
  494. {
  495. return true;
  496. }
  497. public void OnNavigatedFrom(NavigationContext navigationContext)
  498. {
  499. }
  500. }
  501. }