HomePageSetPasswordDialogViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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)
  266. {
  267. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  268. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  269. continue;
  270. }
  271. FileInfo fileinfo = new FileInfo(filename);
  272. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  273. if (document == null)
  274. {
  275. return;
  276. }
  277. document.Encrypt(openPassword, editPassword, permissionsInfo);
  278. document.WriteToFilePath(filename + "SetPassword");
  279. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  280. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  281. fileNamesIndex++;
  282. document.Release();
  283. }
  284. SafetyGridIsEnabled = "True";
  285. MessageBoxEx.Show("已完成");
  286. }
  287. #endregion
  288. #region 批量处理逻辑函数
  289. /// <summary>
  290. /// 添加PDF文件
  291. /// </summary>
  292. private void addpdf()
  293. {
  294. FileNameNumber = fileNames.Count;
  295. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  296. dlg.Multiselect = true;
  297. dlg.Filter = "PDF|*.pdf;*.PDF;";
  298. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  299. {
  300. fileNames.AddRange(dlg.FileNames.ToList());
  301. RemoveExcess(ref fileNames);
  302. SetSafetyGridIsEnabled = "True";
  303. AddFileVisibility = Visibility.Collapsed;
  304. updateListview("待确定");
  305. }
  306. }
  307. public void addPDFFiles(string filename)
  308. {
  309. FileNameNumber = fileNames.Count;
  310. fileNames.Add(filename);
  311. RemoveExcess(ref fileNames);
  312. SetSafetyGridIsEnabled = "True";
  313. AddFileVisibility = Visibility.Collapsed;
  314. updateListview("待确定");
  315. }
  316. /// <summary>
  317. /// 删除重复的文件
  318. /// </summary>
  319. public void RemoveExcess(ref List<string> Filenames)
  320. {
  321. List<string> filenames = new List<string>();
  322. foreach (var fileName in Filenames)
  323. {
  324. if (!filenames.Contains(fileName))
  325. {
  326. filenames.Add(fileName);
  327. }
  328. }
  329. Filenames.Clear();
  330. Filenames = filenames;
  331. }
  332. /// <summary>
  333. /// 添加PDF文件夹
  334. /// </summary>
  335. private void addpdffiles()
  336. {
  337. FileNameNumber = fileNames.Count;
  338. FolderBrowserDialog dialog = new FolderBrowserDialog();
  339. dialog.Description = "请选择文件路径";
  340. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  341. {
  342. string foldPath = dialog.SelectedPath;
  343. var apps = System.IO.Directory.GetFiles(foldPath);
  344. foreach (string app in apps)
  345. {
  346. var fi = new FileInfo(app);
  347. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  348. {
  349. fileNames.Add(app);
  350. }
  351. }
  352. RemoveExcess(ref fileNames);
  353. updateListview("待确定");
  354. SetSafetyGridIsEnabled = "True";
  355. AddFileVisibility = Visibility.Collapsed;
  356. }
  357. }
  358. /// <summary>
  359. /// 更新listview显示
  360. /// state 状态显示字符串
  361. /// </summary>
  362. public void updateListview(string state)
  363. {
  364. updateCurrentListview();
  365. DataTable pdfdatatable = new DataTable();
  366. pdfdatatable.Columns.Add("FileName");
  367. pdfdatatable.Columns.Add("FileSize");
  368. pdfdatatable.Columns.Add("FileState");
  369. int datatableindex = 0;
  370. foreach (var fileName in fileNames)
  371. {
  372. string file_all = fileName;
  373. FileInfo f = new FileInfo(file_all);
  374. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  375. pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  376. datatableindex++;
  377. }
  378. PDFDataTable = pdfdatatable;
  379. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  380. }
  381. /// <summary>
  382. /// 更新Currentlistview显示
  383. /// state 状态显示字符串
  384. /// </summary>
  385. public void updateCurrentListview( string state = "待完成")
  386. {
  387. if (fileNames.Count >= FileNameNumber)
  388. {
  389. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  390. {
  391. PDFCurrentDataTable.Rows.Add(state);
  392. }
  393. }
  394. else
  395. {
  396. Reverseorder(ref fileNamesView);
  397. foreach (int filenamesview in fileNamesView)
  398. {
  399. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  400. }
  401. }
  402. }
  403. /// <summary>
  404. /// 逆序int类型集合
  405. /// </summary>
  406. public void Reverseorder(ref List<int> Numbers)
  407. {
  408. Numbers = Numbers.OrderBy(a => a).ToList();
  409. Numbers.Reverse();
  410. }
  411. /// <summary>
  412. /// 打开文件PDF
  413. /// </summary>
  414. public void openfiledialog()
  415. {
  416. foreach (int filenamesview in fileNamesView)
  417. {
  418. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  419. }
  420. }
  421. /// <summary>
  422. /// 删除文件PDF
  423. /// </summary>
  424. public void removepdffile()
  425. {
  426. FileNameNumber = fileNames.Count;
  427. Reverseorder(ref fileNamesView);
  428. foreach (int filenamesview in fileNamesView)
  429. {
  430. //Trace.WriteLine(filenamesview);
  431. fileNames.Remove(fileNames[filenamesview]);
  432. }
  433. if (fileNames.Count < 1)
  434. {
  435. SetSafetyGridIsEnabled = "False";
  436. AddFileVisibility = Visibility.Visible;
  437. }
  438. updateListview("待确定");
  439. }
  440. public void PDFFileCount()
  441. {
  442. if (fileNames.Count == 0)
  443. {
  444. SetSafetyGridIsEnabled = "False";
  445. AddFileVisibility = Visibility.Visible;
  446. }
  447. else
  448. {
  449. SetSafetyGridIsEnabled = "True";
  450. AddFileVisibility = Visibility.Collapsed;
  451. }
  452. }
  453. #endregion
  454. public void OnNavigatedTo(NavigationContext navigationContext)
  455. {
  456. List<string> filepath = new List<string>();
  457. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  458. if (filepath != null)
  459. {
  460. fileNames = filepath;
  461. PDFFileCount();
  462. updateListview("待确定");
  463. }
  464. }
  465. public bool IsNavigationTarget(NavigationContext navigationContext)
  466. {
  467. return true;
  468. }
  469. public void OnNavigatedFrom(NavigationContext navigationContext)
  470. {
  471. }
  472. }
  473. }