HomePageSetPasswordDialogViewModel.cs 16 KB

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