HomePageRemoveDialogViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  7. using PDF_Office.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_Office.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 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. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  165. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  166. OptionsRemoveCommand = new DelegateCommand(OptionsRemove);
  167. }
  168. #region 逻辑函数
  169. private bool CheckHaveAllPermissions(CPDFDocument document)
  170. {
  171. CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
  172. if (permissionsInfo.AllowsDocumentChanges &&
  173. permissionsInfo.AllowsPrinting &&
  174. permissionsInfo.AllowsHighQualityPrinting &&
  175. permissionsInfo.AllowsCopying &&
  176. //permissionsInfo.AllowsDocumentAssembly&&
  177. //TODO: 这个权限有问题
  178. permissionsInfo.AllowsCommenting)
  179. {
  180. return true;
  181. }
  182. else
  183. {
  184. return false;
  185. }
  186. }
  187. private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword(CPDFDocument document)
  188. {
  189. if (document.IsLocked)
  190. {
  191. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  192. }
  193. else { }
  194. if (document.IsEncrypted)
  195. {
  196. if (CheckHaveAllPermissions(document))
  197. {
  198. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
  199. }
  200. else
  201. {
  202. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  203. }
  204. }
  205. else
  206. {
  207. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt;
  208. }
  209. }
  210. private void OptionsRemove()
  211. {
  212. if (!(WatermarkIsRemove || BackgroundIsRemove || HeaderFooterIsRemove || BatesIsRemove || SecurityIsRemove)) {
  213. MessageBoxEx.Show("没选");
  214. return;
  215. }
  216. OptionsRemoveGridIsEnabled = "False";
  217. //GC.KeepAlive(indexDelegate);
  218. fileNamesIndex = 0;
  219. foreach (var filename in fileNames)
  220. {
  221. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  222. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  223. if (document == null)
  224. {
  225. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  226. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  227. fileNamesIndex++;
  228. continue;
  229. }
  230. if (document.IsLocked)
  231. {
  232. DialogParameters value = new DialogParameters();
  233. value.Add(ParameterNames.PDFDocument, document);
  234. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  235. {
  236. if (e.Result == ButtonResult.OK)
  237. {
  238. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  239. {
  240. document.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  241. }
  242. }
  243. });
  244. if (document.IsLocked)
  245. {
  246. //未成功解密文档时,释放Document对象,返回
  247. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  248. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  249. fileNamesIndex++;
  250. continue;
  251. }
  252. }
  253. FileInfo fileinfo = new FileInfo(filename);
  254. if (WatermarkIsRemove)
  255. {
  256. document.DeleteWatermarks();
  257. }
  258. if (BackgroundIsRemove)
  259. {
  260. document.GetBackground().Clear();
  261. }
  262. if (HeaderFooterIsRemove)
  263. {
  264. document.GetHeaderFooter().Clear();
  265. }
  266. if (BatesIsRemove)
  267. {
  268. document.GetBates().Clear();
  269. }
  270. if (SecurityIsRemove)
  271. {
  272. document.Descrypt(CommonHelper.CreateFilePath(filename + "_Remove.pdf"));
  273. }
  274. else
  275. {
  276. document.WriteToFilePath(CommonHelper.CreateFilePath(filename + "_Remove.pdf"));
  277. }
  278. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  279. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  280. fileNamesIndex++;
  281. document.Release();
  282. }
  283. OptionsRemoveGridIsEnabled = "True";
  284. }
  285. #endregion
  286. #region 批量处理逻辑函数
  287. /// <summary>
  288. /// 添加PDF文件
  289. /// </summary>
  290. private void addpicture()
  291. {
  292. FileNameNumber = fileNames.Count;
  293. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  294. dlg.Multiselect = true;
  295. dlg.Filter = "PDF|*.pdf;*.PDF;";
  296. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  297. {
  298. fileNames.AddRange(dlg.FileNames.ToList());
  299. RemoveExcess(ref fileNames);
  300. SetOptionsRemoveGridIsEnabled = "True";
  301. AddFileVisibility = Visibility.Collapsed;
  302. updateListview("wait");
  303. }
  304. }
  305. public void addPDFFiles(string filename) {
  306. FileNameNumber = fileNames.Count;
  307. fileNames.Add(filename);
  308. RemoveExcess(ref fileNames);
  309. SetOptionsRemoveGridIsEnabled = "True";
  310. AddFileVisibility = Visibility.Collapsed;
  311. updateListview("wait");
  312. }
  313. /// <summary>
  314. /// 删除重复的文件
  315. /// </summary>
  316. public void RemoveExcess(ref List<string> Filenames)
  317. {
  318. List<string> filenames = new List<string>();
  319. foreach (var fileName in Filenames)
  320. {
  321. if (!filenames.Contains(fileName))
  322. {
  323. filenames.Add(fileName);
  324. }
  325. }
  326. Filenames.Clear();
  327. Filenames = filenames;
  328. }
  329. /// <summary>
  330. /// 添加PDF文件夹
  331. /// </summary>
  332. private void addpicturefiles()
  333. {
  334. FileNameNumber = fileNames.Count;
  335. FolderBrowserDialog dialog = new FolderBrowserDialog();
  336. dialog.Description = "请选择文件路径";
  337. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  338. {
  339. string foldPath = dialog.SelectedPath;
  340. var apps = System.IO.Directory.GetFiles(foldPath);
  341. foreach (string app in apps)
  342. {
  343. var fi = new FileInfo(app);
  344. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  345. {
  346. fileNames.Add(app);
  347. }
  348. }
  349. RemoveExcess(ref fileNames);
  350. updateListview("wait");
  351. SetOptionsRemoveGridIsEnabled = "True";
  352. AddFileVisibility = Visibility.Collapsed;
  353. }
  354. }
  355. /// <summary>
  356. /// 更新listview显示
  357. /// state 状态显示字符串
  358. /// </summary>
  359. public void updateListview(string state)
  360. {
  361. updateCurrentListview();
  362. DataTable pdfdatatable = new DataTable();
  363. pdfdatatable.Columns.Add("FileName");
  364. pdfdatatable.Columns.Add("FileSize");
  365. pdfdatatable.Columns.Add("FileState");
  366. int datatableindex = 0;
  367. foreach (var fileName in fileNames)
  368. {
  369. string file_all = fileName;
  370. FileInfo f = new FileInfo(file_all);
  371. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  372. pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  373. datatableindex++;
  374. }
  375. PDFDataTable = pdfdatatable;
  376. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  377. }
  378. /// <summary>
  379. /// 更新Currentlistview显示
  380. /// state 状态显示字符串
  381. /// </summary>
  382. public void updateCurrentListview(string state = "wait")
  383. {
  384. if (fileNames.Count >= FileNameNumber)
  385. {
  386. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  387. {
  388. PDFCurrentDataTable.Rows.Add(state);
  389. }
  390. }
  391. else
  392. {
  393. Reverseorder(ref fileNamesView);
  394. foreach (int filenamesview in fileNamesView)
  395. {
  396. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  397. }
  398. }
  399. }
  400. /// <summary>
  401. /// 逆序int类型集合
  402. /// </summary>
  403. public void Reverseorder(ref List<int> Numbers)
  404. {
  405. Numbers = Numbers.OrderBy(a => a).ToList();
  406. Numbers.Reverse();
  407. }
  408. /// <summary>
  409. /// 打开文件PDF
  410. /// </summary>
  411. public void openfiledialog()
  412. {
  413. foreach (int filenamesview in fileNamesView)
  414. {
  415. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  416. }
  417. }
  418. /// <summary>
  419. /// 删除文件PDF
  420. /// </summary>
  421. public void removepdffile()
  422. {
  423. FileNameNumber = fileNames.Count;
  424. Reverseorder(ref fileNamesView);
  425. foreach (int filenamesview in fileNamesView)
  426. {
  427. //Trace.WriteLine(filenamesview);
  428. fileNames.Remove(fileNames[filenamesview]);
  429. }
  430. if (fileNames.Count < 1)
  431. {
  432. SetOptionsRemoveGridIsEnabled = "False";
  433. AddFileVisibility = Visibility.Visible;
  434. }
  435. updateListview("wait");
  436. }
  437. public void removepdffile(int index)
  438. {
  439. PDFCurrentDataTable.Rows.RemoveAt(index);
  440. fileNames.Remove(fileNames[index]);
  441. if (fileNames.Count < 1)
  442. {
  443. SetOptionsRemoveGridIsEnabled = "False";
  444. AddFileVisibility = Visibility.Visible;
  445. }
  446. updateListview("wait");
  447. }
  448. public void PDFFileCount()
  449. {
  450. if (fileNames.Count == 0)
  451. {
  452. SetOptionsRemoveGridIsEnabled = "False";
  453. AddFileVisibility = Visibility.Visible;
  454. }
  455. else
  456. {
  457. SetOptionsRemoveGridIsEnabled = "True";
  458. AddFileVisibility = Visibility.Collapsed;
  459. }
  460. }
  461. #endregion
  462. #region 构架行为
  463. public void OnNavigatedTo(NavigationContext navigationContext)
  464. {
  465. List<string> filepath = new List<string>();
  466. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  467. if (filepath != null)
  468. {
  469. fileNames = filepath;
  470. PDFFileCount();
  471. updateListview("wait");
  472. }
  473. }
  474. public bool IsNavigationTarget(NavigationContext navigationContext)
  475. {
  476. return true;
  477. }
  478. public void OnNavigatedFrom(NavigationContext navigationContext)
  479. {
  480. }
  481. #endregion
  482. }
  483. }