HomePageRemoveDialogViewModel.cs 17 KB

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