HomePageRemoveDialogViewModel.cs 19 KB

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