HomePageRemoveDialogViewModel.cs 17 KB

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