HomePageRemoveDialogViewModel.cs 18 KB

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