HomePageRemoveDialogViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  7. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Windows;
  18. using System.Windows.Forms;
  19. using DataTable = System.Data.DataTable;
  20. using DialogResult = Prism.Services.Dialogs.DialogResult;
  21. using Task = System.Threading.Tasks.Task;
  22. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing
  23. {
  24. public class HomePageRemoveDialogViewModel : BindableBase, INavigationAware
  25. {
  26. #region 参数和属性
  27. public IDialogService dialogs;
  28. private List<string> fileNames;
  29. public List<int> fileNamesView = new List<int>();
  30. private int fileNamesIndex = 0;
  31. private int FileNameNumber = 0;
  32. HomePageCompressDialogModel homePageCompressDialogModel = new HomePageCompressDialogModel();
  33. private DataTable pdfDataTable = new DataTable();
  34. public DataTable PDFDataTable
  35. {
  36. get { return pdfDataTable; }
  37. set
  38. {
  39. SetProperty(ref pdfDataTable, value);
  40. }
  41. }
  42. private DataTable pdfCurrentDataTable = new DataTable();
  43. public DataTable PDFCurrentDataTable
  44. {
  45. get { return pdfCurrentDataTable; }
  46. set
  47. {
  48. SetProperty(ref pdfCurrentDataTable, value);
  49. }
  50. }
  51. private string optionsRemoveGridIsEnabled = "True";
  52. public string OptionsRemoveGridIsEnabled
  53. {
  54. get
  55. {
  56. return optionsRemoveGridIsEnabled;
  57. }
  58. set
  59. {
  60. SetProperty(ref optionsRemoveGridIsEnabled, value);
  61. }
  62. }
  63. private string setOptionsRemoveGridIsEnabled = "True";
  64. public string SetOptionsRemoveGridIsEnabled
  65. {
  66. get
  67. {
  68. return setOptionsRemoveGridIsEnabled;
  69. }
  70. set
  71. {
  72. SetProperty(ref setOptionsRemoveGridIsEnabled, value);
  73. }
  74. }
  75. private string removeIsEnable = "False";
  76. public string RemoveIsEnable
  77. {
  78. get { return removeIsEnable; }
  79. set
  80. {
  81. SetProperty(ref removeIsEnable, value);
  82. }
  83. }
  84. private string selectFileName = "False";
  85. public string SelectFileName
  86. {
  87. get { return selectFileName; }
  88. set
  89. {
  90. SetProperty(ref selectFileName, value);
  91. }
  92. }
  93. private Visibility addFileVisibility = Visibility.Hidden;
  94. public Visibility AddFileVisibility
  95. {
  96. get { return addFileVisibility; }
  97. set
  98. {
  99. SetProperty(ref addFileVisibility, value);
  100. RaisePropertyChanged();
  101. }
  102. }
  103. private bool securityIsRemove = false;
  104. public bool SecurityIsRemove
  105. {
  106. get { return securityIsRemove; }
  107. set
  108. {
  109. SetProperty(ref securityIsRemove, value);
  110. }
  111. }
  112. private bool watermarkIsRemove = false;
  113. public bool WatermarkIsRemove
  114. {
  115. get { return watermarkIsRemove; }
  116. set
  117. {
  118. SetProperty(ref watermarkIsRemove, value);
  119. }
  120. }
  121. private bool backgroundIsRemove = false;
  122. public bool BackgroundIsRemove
  123. {
  124. get { return backgroundIsRemove; }
  125. set
  126. {
  127. SetProperty(ref backgroundIsRemove, value);
  128. }
  129. }
  130. private bool headerFooterIsRemove = false;
  131. public bool HeaderFooterIsRemove
  132. {
  133. get { return headerFooterIsRemove; }
  134. set
  135. {
  136. SetProperty(ref headerFooterIsRemove, value);
  137. }
  138. }
  139. private bool batesIsRemove = false;
  140. public bool BatesIsRemove
  141. {
  142. get { return batesIsRemove; }
  143. set
  144. {
  145. SetProperty(ref batesIsRemove, value);
  146. }
  147. }
  148. #endregion
  149. #region 委托声明
  150. public DelegateCommand ADDPDFCommand { get; set; }
  151. public DelegateCommand ADDOpenedPDFCommand { get; set; }
  152. public DelegateCommand RemovePDFFileCommand { get; set; }
  153. public DelegateCommand ADDPDFFilesCommand { get; set; }
  154. public DelegateCommand OptionsRemoveCommand { get; set; }
  155. #endregion
  156. public HomePageRemoveDialogViewModel(IDialogService dialogService)
  157. {
  158. dialogs = dialogService;
  159. PDFCurrentDataTable.Columns.Add("FileState");
  160. PDFDataTable.Columns.Add("FileName");
  161. PDFDataTable.Columns.Add("FileSize");
  162. PDFDataTable.Columns.Add("FileState");
  163. ADDPDFCommand = new DelegateCommand(addpicture);
  164. ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles);
  165. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  166. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  167. OptionsRemoveCommand = new DelegateCommand(OptionsRemove);
  168. }
  169. #region 逻辑函数
  170. private bool CheckHaveAllPermissions(CPDFDocument document)
  171. {
  172. CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
  173. if (permissionsInfo.AllowsDocumentChanges &&
  174. permissionsInfo.AllowsPrinting &&
  175. permissionsInfo.AllowsHighQualityPrinting &&
  176. permissionsInfo.AllowsCopying &&
  177. //permissionsInfo.AllowsDocumentAssembly&&
  178. //TODO: 这个权限有问题
  179. permissionsInfo.AllowsCommenting)
  180. {
  181. return true;
  182. }
  183. else
  184. {
  185. return false;
  186. }
  187. }
  188. private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword(CPDFDocument document)
  189. {
  190. if (document.IsLocked)
  191. {
  192. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  193. }
  194. else { }
  195. if (document.IsEncrypted)
  196. {
  197. if (CheckHaveAllPermissions(document))
  198. {
  199. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
  200. }
  201. else
  202. {
  203. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  204. }
  205. }
  206. else
  207. {
  208. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt;
  209. }
  210. }
  211. private void OptionsRemove()
  212. {
  213. if (!(WatermarkIsRemove || BackgroundIsRemove || HeaderFooterIsRemove || BatesIsRemove || SecurityIsRemove)) {
  214. MessageBoxEx.Show("请选择Option");
  215. return;
  216. }
  217. FolderBrowserDialog dlg = new FolderBrowserDialog();
  218. string saveSelectedPath = "";
  219. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  220. {
  221. saveSelectedPath = dlg.SelectedPath.Trim();
  222. saveSelectedPath = saveSelectedPath + "\\";
  223. }
  224. else { return; }
  225. OptionsRemoveGridIsEnabled = "False";
  226. //GC.KeepAlive(indexDelegate);
  227. fileNamesIndex = 0;
  228. HomePageBatchProcessingDialogModel.closeDialog = true;
  229. foreach (var filename in fileNames)
  230. {
  231. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  232. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  233. if (document == null)
  234. {
  235. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  236. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  237. fileNamesIndex++;
  238. continue;
  239. }
  240. if (document.IsLocked)
  241. {
  242. DialogParameters value = new DialogParameters();
  243. value.Add(ParameterNames.PDFDocument, document);
  244. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  245. {
  246. if (e.Result == ButtonResult.OK)
  247. {
  248. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  249. {
  250. document.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  251. }
  252. }
  253. });
  254. if (document.IsLocked)
  255. {
  256. //未成功解密文档时,释放Document对象,返回
  257. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  258. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  259. fileNamesIndex++;
  260. continue;
  261. }
  262. }
  263. FileInfo fileinfo = new FileInfo(filename);
  264. if (WatermarkIsRemove)
  265. {
  266. document.DeleteWatermarks();
  267. }
  268. if (BackgroundIsRemove)
  269. {
  270. document.GetBackground().Clear();
  271. }
  272. if (HeaderFooterIsRemove)
  273. {
  274. document.GetHeaderFooter().Clear();
  275. }
  276. if (BatesIsRemove)
  277. {
  278. document.GetBates().Clear();
  279. }
  280. if (SecurityIsRemove)
  281. {
  282. document.Decrypt(CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "_Remove.pdf"));
  283. }
  284. else
  285. {
  286. document.WriteToFilePath(CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "_Remove.pdf"));
  287. }
  288. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  289. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  290. fileNamesIndex++;
  291. document.Release();
  292. }
  293. OptionsRemoveGridIsEnabled = "True";
  294. HomePageBatchProcessingDialogModel.closeDialog = false;
  295. }
  296. #endregion
  297. #region 批量处理逻辑函数
  298. /// <summary>
  299. /// 添加PDF文件
  300. /// </summary>
  301. private void addpicture()
  302. {
  303. FileNameNumber = fileNames.Count;
  304. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  305. dlg.Multiselect = true;
  306. dlg.Filter = "PDF|*.pdf;*.PDF;";
  307. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  308. {
  309. fileNames.AddRange(dlg.FileNames.ToList());
  310. RemoveExcess(ref fileNames);
  311. SetOptionsRemoveGridIsEnabled = "True";
  312. AddFileVisibility = Visibility.Collapsed;
  313. updateListview("wait");
  314. }
  315. }
  316. public void addPDFFiles(string filename) {
  317. FileNameNumber = fileNames.Count;
  318. fileNames.Add(filename);
  319. RemoveExcess(ref fileNames);
  320. SetOptionsRemoveGridIsEnabled = "True";
  321. AddFileVisibility = Visibility.Collapsed;
  322. updateListview("wait");
  323. }
  324. /// <summary>
  325. /// 添加已打开PDF文件
  326. /// </summary>
  327. public void addOpenedPDFFiles()
  328. {
  329. FileNameNumber = fileNames.Count;
  330. foreach (var filename in App.OpenedFileList)
  331. {
  332. fileNames.Add(filename);
  333. }
  334. RemoveExcess(ref fileNames);
  335. SetOptionsRemoveGridIsEnabled = "True";
  336. AddFileVisibility = Visibility.Collapsed;
  337. updateListview("wait");
  338. }
  339. /// <summary>
  340. /// 删除重复的文件
  341. /// </summary>
  342. public void RemoveExcess(ref List<string> Filenames)
  343. {
  344. List<string> filenames = new List<string>();
  345. foreach (var fileName in Filenames)
  346. {
  347. if (!filenames.Contains(fileName))
  348. {
  349. filenames.Add(fileName);
  350. }
  351. }
  352. Filenames.Clear();
  353. Filenames = filenames;
  354. }
  355. /// <summary>
  356. /// 添加PDF文件夹
  357. /// </summary>
  358. private void addpicturefiles()
  359. {
  360. FileNameNumber = fileNames.Count;
  361. FolderBrowserDialog dialog = new FolderBrowserDialog();
  362. dialog.Description = "请选择文件路径";
  363. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  364. {
  365. string foldPath = dialog.SelectedPath;
  366. var apps = System.IO.Directory.GetFiles(foldPath);
  367. foreach (string app in apps)
  368. {
  369. var fi = new FileInfo(app);
  370. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  371. {
  372. fileNames.Add(app);
  373. }
  374. }
  375. RemoveExcess(ref fileNames);
  376. updateListview("wait");
  377. SetOptionsRemoveGridIsEnabled = "True";
  378. AddFileVisibility = Visibility.Collapsed;
  379. }
  380. }
  381. /// <summary>
  382. /// 更新listview显示
  383. /// state 状态显示字符串
  384. /// </summary>
  385. public void updateListview(string state)
  386. {
  387. updateCurrentListview();
  388. DataTable pdfdatatable = new DataTable();
  389. pdfdatatable.Columns.Add("FileName");
  390. pdfdatatable.Columns.Add("FileSize");
  391. pdfdatatable.Columns.Add("FileState");
  392. int datatableindex = 0;
  393. foreach (var fileName in fileNames)
  394. {
  395. string file_all = fileName;
  396. FileInfo f = new FileInfo(file_all);
  397. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  398. pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  399. datatableindex++;
  400. }
  401. PDFDataTable = pdfdatatable;
  402. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  403. }
  404. /// <summary>
  405. /// 更新Currentlistview显示
  406. /// state 状态显示字符串
  407. /// </summary>
  408. public void updateCurrentListview(string state = "wait")
  409. {
  410. if (fileNames.Count >= FileNameNumber)
  411. {
  412. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  413. {
  414. PDFCurrentDataTable.Rows.Add(state);
  415. }
  416. }
  417. else
  418. {
  419. Reverseorder(ref fileNamesView);
  420. foreach (int filenamesview in fileNamesView)
  421. {
  422. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  423. }
  424. }
  425. }
  426. /// <summary>
  427. /// 逆序int类型集合
  428. /// </summary>
  429. public void Reverseorder(ref List<int> Numbers)
  430. {
  431. Numbers = Numbers.OrderBy(a => a).ToList();
  432. Numbers.Reverse();
  433. }
  434. /// <summary>
  435. /// 打开文件PDF
  436. /// </summary>
  437. public void openfiledialog()
  438. {
  439. foreach (int filenamesview in fileNamesView)
  440. {
  441. System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
  442. }
  443. }
  444. /// <summary>
  445. /// 删除文件PDF
  446. /// </summary>
  447. public void removepdffile()
  448. {
  449. FileNameNumber = fileNames.Count;
  450. Reverseorder(ref fileNamesView);
  451. foreach (int filenamesview in fileNamesView)
  452. {
  453. //Trace.WriteLine(filenamesview);
  454. fileNames.Remove(fileNames[filenamesview]);
  455. }
  456. if (fileNames.Count < 1)
  457. {
  458. SetOptionsRemoveGridIsEnabled = "False";
  459. AddFileVisibility = Visibility.Visible;
  460. }
  461. updateListview("wait");
  462. }
  463. public void removepdffile(int index)
  464. {
  465. PDFCurrentDataTable.Rows.RemoveAt(index);
  466. fileNames.Remove(fileNames[index]);
  467. if (fileNames.Count < 1)
  468. {
  469. SetOptionsRemoveGridIsEnabled = "False";
  470. AddFileVisibility = Visibility.Visible;
  471. }
  472. updateListview("wait");
  473. }
  474. public void PDFFileCount()
  475. {
  476. if (fileNames.Count == 0)
  477. {
  478. SetOptionsRemoveGridIsEnabled = "False";
  479. AddFileVisibility = Visibility.Visible;
  480. }
  481. else
  482. {
  483. SetOptionsRemoveGridIsEnabled = "True";
  484. AddFileVisibility = Visibility.Collapsed;
  485. }
  486. }
  487. #endregion
  488. #region 构架行为
  489. public void OnNavigatedTo(NavigationContext navigationContext)
  490. {
  491. List<string> filepath = new List<string>();
  492. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  493. if (filepath != null)
  494. {
  495. fileNames = filepath;
  496. PDFFileCount();
  497. updateListview("wait");
  498. }
  499. }
  500. public bool IsNavigationTarget(NavigationContext navigationContext)
  501. {
  502. return true;
  503. }
  504. public void OnNavigatedFrom(NavigationContext navigationContext)
  505. {
  506. }
  507. #endregion
  508. }
  509. }