HomePageRemoveDialogViewModel.cs 15 KB

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