HomePageSetPasswordDialogViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. /// <summary>
  294. /// 删除重复的文件
  295. /// </summary>
  296. public void RemoveExcess(ref List<string> Filenames)
  297. {
  298. List<string> filenames = new List<string>();
  299. foreach (var fileName in Filenames)
  300. {
  301. if (!filenames.Contains(fileName))
  302. {
  303. filenames.Add(fileName);
  304. }
  305. }
  306. Filenames.Clear();
  307. Filenames = filenames;
  308. }
  309. /// <summary>
  310. /// 添加PDF文件夹
  311. /// </summary>
  312. private void addpdffiles()
  313. {
  314. FolderBrowserDialog dialog = new FolderBrowserDialog();
  315. dialog.Description = "请选择文件路径";
  316. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  317. {
  318. string foldPath = dialog.SelectedPath;
  319. var apps = System.IO.Directory.GetFiles(foldPath);
  320. foreach (string app in apps)
  321. {
  322. var fi = new FileInfo(app);
  323. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  324. {
  325. fileNames.Add(app);
  326. }
  327. }
  328. RemoveExcess(ref fileNames);
  329. updateListview("待确定");
  330. SetSafetyGridIsEnabled = "True";
  331. AddFileVisibility = Visibility.Collapsed;
  332. }
  333. }
  334. /// <summary>
  335. /// 更新listview显示
  336. /// state 状态显示字符串
  337. /// </summary>
  338. public void updateListview(string state)
  339. {
  340. DataTable pdfdatatable = new DataTable();
  341. pdfdatatable.Columns.Add("FileName");
  342. pdfdatatable.Columns.Add("FileSize");
  343. pdfdatatable.Columns.Add("FileState");
  344. foreach (var fileName in fileNames)
  345. {
  346. string file_all = fileName;
  347. FileInfo f = new FileInfo(file_all);
  348. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  349. pdfdatatable.Rows.Add(f.Name, file_size, state);
  350. }
  351. PDFDataTable = pdfdatatable;
  352. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  353. }
  354. /// <summary>
  355. /// 逆序int类型集合
  356. /// </summary>
  357. public void Reverseorder(ref List<int> Numbers)
  358. {
  359. Numbers = Numbers.OrderBy(a => a).ToList();
  360. Numbers.Reverse();
  361. }
  362. /// <summary>
  363. /// 打开文件PDF
  364. /// </summary>
  365. public void openfiledialog()
  366. {
  367. foreach (int filenamesview in fileNamesView)
  368. {
  369. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  370. }
  371. }
  372. /// <summary>
  373. /// 删除文件PDF
  374. /// </summary>
  375. public void removepdffile()
  376. {
  377. Reverseorder(ref fileNamesView);
  378. foreach (int filenamesview in fileNamesView)
  379. {
  380. //Trace.WriteLine(filenamesview);
  381. fileNames.Remove(fileNames[filenamesview]);
  382. }
  383. if (fileNames.Count < 1)
  384. {
  385. SetSafetyGridIsEnabled = "False";
  386. AddFileVisibility = Visibility.Visible;
  387. }
  388. updateListview("待确定");
  389. }
  390. public void PDFFileCount()
  391. {
  392. if (fileNames.Count == 0)
  393. {
  394. SetSafetyGridIsEnabled = "False";
  395. AddFileVisibility = Visibility.Visible;
  396. }
  397. else
  398. {
  399. SetSafetyGridIsEnabled = "True";
  400. AddFileVisibility = Visibility.Collapsed;
  401. }
  402. }
  403. #endregion
  404. public void OnNavigatedTo(NavigationContext navigationContext)
  405. {
  406. List<string> filepath = new List<string>();
  407. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  408. if (filepath != null)
  409. {
  410. fileNames = filepath;
  411. PDFFileCount();
  412. updateListview("待确定");
  413. }
  414. }
  415. public bool IsNavigationTarget(NavigationContext navigationContext)
  416. {
  417. return true;
  418. }
  419. public void OnNavigatedFrom(NavigationContext navigationContext)
  420. {
  421. }
  422. }
  423. }