HomePageRemoveDialogViewModel.cs 18 KB

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