HomePageCompressDialogViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  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_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing
  22. {
  23. public class HomePageCompressDialogViewModel : BindableBase, INavigationAware
  24. {
  25. #region 参数和属性
  26. private List<string> fileNames;
  27. public IDialogService dialogs;
  28. private CPDFDocument tempDocument;
  29. public List<int> fileNamesView = new List<int>();
  30. private int fileNamesIndex = 0;
  31. private int FileNameNumber = 0;
  32. private IntPtr compressingIntpr = IntPtr.Zero;
  33. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  34. HomePageCompressDialogModel homePageCompressDialogModel=new HomePageCompressDialogModel();
  35. private DataTable pdfDataTable = new DataTable();
  36. public DataTable PDFDataTable
  37. {
  38. get { return pdfDataTable; }
  39. set
  40. {
  41. SetProperty(ref pdfDataTable, value);
  42. }
  43. }
  44. private DataTable pdfCurrentDataTable = new DataTable();
  45. public DataTable PDFCurrentDataTable
  46. {
  47. get { return pdfCurrentDataTable; }
  48. set
  49. {
  50. SetProperty(ref pdfCurrentDataTable, value);
  51. }
  52. }
  53. private string compressGridIsEnabled="True";
  54. public string CompressGridIsEnabled
  55. {
  56. get
  57. {
  58. return compressGridIsEnabled;
  59. }
  60. set
  61. {
  62. SetProperty(ref compressGridIsEnabled, value);
  63. }
  64. }
  65. private string setCompressGridIsEnabled = "True";
  66. public string SetCompressGridIsEnabled
  67. {
  68. get
  69. {
  70. return setCompressGridIsEnabled;
  71. }
  72. set
  73. {
  74. SetProperty(ref setCompressGridIsEnabled, value);
  75. }
  76. }
  77. private string removeIsEnable = "False";
  78. public string RemoveIsEnable
  79. {
  80. get { return removeIsEnable; }
  81. set
  82. {
  83. SetProperty(ref removeIsEnable, value);
  84. }
  85. }
  86. private string selectFileName = "False";
  87. public string SelectFileName
  88. {
  89. get { return selectFileName; }
  90. set
  91. {
  92. SetProperty(ref selectFileName, value);
  93. }
  94. }
  95. private Visibility addFileVisibility = Visibility.Hidden;
  96. public Visibility AddFileVisibility
  97. {
  98. get { return addFileVisibility; }
  99. set
  100. {
  101. SetProperty(ref addFileVisibility, value);
  102. RaisePropertyChanged();
  103. }
  104. }
  105. #endregion
  106. #region 委托声明
  107. public DelegateCommand ADDPDFCommand { get; set; }
  108. public DelegateCommand ADDOpenedPDFCommand { get; set; }
  109. public DelegateCommand RemovePDFFileCommand { get; set; }
  110. public DelegateCommand ADDPDFFilesCommand { get; set; }
  111. public DelegateCommand CompressCommand { get; set; }
  112. public DelegateCommand<string> RadioButtonCommand{ get; set; }
  113. #endregion
  114. public HomePageCompressDialogViewModel(IDialogService dialogs)
  115. {
  116. PDFCurrentDataTable.Columns.Add("FileState");
  117. PDFDataTable.Columns.Add("FileName");
  118. PDFDataTable.Columns.Add("FileSize");
  119. PDFDataTable.Columns.Add("FileState");
  120. PDFDataTable.Columns.Add("MaxPageRange");
  121. ADDPDFCommand = new DelegateCommand(addpicture);
  122. ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles);
  123. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  124. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  125. CompressCommand = new DelegateCommand(compress);
  126. RadioButtonCommand = new DelegateCommand<string>(RadioButton);
  127. this.dialogs = dialogs;
  128. }
  129. #region 逻辑函数
  130. private void RadioButton(string sender) {
  131. if (sender != "") {
  132. switch (sender) {
  133. case "LargeQualityRadioBtn":
  134. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLarge;
  135. break;
  136. case "StandardQualityRadioBtn":
  137. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusStandard;
  138. break;
  139. case "LittleQualityRadioBtn":
  140. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLittle;
  141. break;
  142. case "MicroQualityRadioBtn":
  143. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusMicro;
  144. break;
  145. default:
  146. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusDefault;
  147. break;
  148. }
  149. }
  150. }
  151. private int GetIndex(int pageindex)
  152. {
  153. PDFDataTable.Rows[fileNamesIndex]["FileState"]=pageindex.ToString();
  154. return 0;
  155. }
  156. private async void compress() {
  157. FolderBrowserDialog dlg = new FolderBrowserDialog();
  158. string saveSelectedPath = "";
  159. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  160. {
  161. saveSelectedPath = dlg.SelectedPath.Trim();
  162. saveSelectedPath = saveSelectedPath + "\\";
  163. }
  164. else { return; }
  165. HomePageBatchProcessingDialogModel.closeDialog = true;
  166. CompressGridIsEnabled = "False";
  167. //GC.KeepAlive(indexDelegate);
  168. fileNamesIndex = 0;
  169. foreach (var filename in fileNames)
  170. {
  171. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  172. if (document == null)
  173. {
  174. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  175. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  176. fileNamesIndex++;
  177. continue;
  178. }
  179. if (document.IsLocked)
  180. {
  181. DialogParameters value = new DialogParameters();
  182. value.Add(ParameterNames.PDFDocument, document);
  183. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  184. {
  185. if (e.Result == ButtonResult.OK)
  186. {
  187. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  188. {
  189. document.UnlockWithPassword (e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  190. }
  191. }
  192. });
  193. if (document.IsLocked)
  194. {
  195. //未成功解密文档时,释放Document对象,返回
  196. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  197. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  198. fileNamesIndex++;
  199. continue;
  200. }
  201. }
  202. FileInfo fileinfo = new FileInfo(filename);
  203. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  204. indexDelegate += GetIndex;
  205. compressingIntpr = document.CompressFile_Init(((float)homePageCompressDialogModel.CompressQuality), indexDelegate);
  206. tempDocument = document;
  207. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "compress.pdf")); });
  208. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  209. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  210. fileNamesIndex++;
  211. document.Release();
  212. }
  213. CompressGridIsEnabled = "True";
  214. HomePageBatchProcessingDialogModel.closeDialog = false;
  215. }
  216. #endregion
  217. #region 批量处理逻辑函数
  218. /// <summary>
  219. /// 添加PDF文件
  220. /// </summary>
  221. private void addpicture()
  222. {
  223. FileNameNumber = fileNames.Count;
  224. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  225. dlg.Multiselect = true;
  226. dlg.Filter = "PDF|*.pdf;*.PDF;";
  227. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  228. {
  229. fileNames.AddRange(dlg.FileNames.ToList());
  230. RemoveExcess(ref fileNames);
  231. SetCompressGridIsEnabled = "True";
  232. AddFileVisibility = Visibility.Collapsed;
  233. updateListview("wait");
  234. }
  235. }
  236. public void addPDFFiles(string filename)
  237. {
  238. FileNameNumber = fileNames.Count;
  239. fileNames.Add(filename);
  240. RemoveExcess(ref fileNames);
  241. SetCompressGridIsEnabled = "True";
  242. AddFileVisibility = Visibility.Collapsed;
  243. updateListview("wait");
  244. }
  245. /// <summary>
  246. /// 添加已打开PDF文件
  247. /// </summary>
  248. public void addOpenedPDFFiles()
  249. {
  250. FileNameNumber = fileNames.Count;
  251. foreach (var filename in App.OpenedFileList)
  252. {
  253. fileNames.Add(filename);
  254. }
  255. RemoveExcess(ref fileNames);
  256. SetCompressGridIsEnabled = "True";
  257. AddFileVisibility = Visibility.Collapsed;
  258. updateListview("wait");
  259. }
  260. /// <summary>
  261. /// 删除重复的文件
  262. /// </summary>
  263. public void RemoveExcess(ref List<string> Filenames)
  264. {
  265. List<string> filenames = new List<string>();
  266. foreach (var fileName in Filenames)
  267. {
  268. if (!filenames.Contains(fileName))
  269. {
  270. filenames.Add(fileName);
  271. }
  272. }
  273. Filenames.Clear();
  274. Filenames = filenames;
  275. }
  276. /// <summary>
  277. /// 添加PDF文件夹
  278. /// </summary>
  279. private void addpicturefiles()
  280. {
  281. FileNameNumber = fileNames.Count;
  282. FolderBrowserDialog dialog = new FolderBrowserDialog();
  283. dialog.Description = "请选择文件路径";
  284. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  285. {
  286. string foldPath = dialog.SelectedPath;
  287. var apps = System.IO.Directory.GetFiles(foldPath);
  288. foreach (string app in apps)
  289. {
  290. var fi = new FileInfo(app);
  291. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  292. {
  293. fileNames.Add(app);
  294. }
  295. }
  296. RemoveExcess(ref fileNames);
  297. updateListview("wait");
  298. SetCompressGridIsEnabled = "True";
  299. AddFileVisibility = Visibility.Collapsed;
  300. }
  301. }
  302. /// <summary>
  303. /// 更新listview显示
  304. /// state 状态显示字符串
  305. /// </summary>
  306. public void updateListview(string state)
  307. {
  308. updateCurrentListview();
  309. DataTable pdfdatatable = new DataTable();
  310. pdfdatatable.Columns.Add("FileName");
  311. pdfdatatable.Columns.Add("FileSize");
  312. pdfdatatable.Columns.Add("FileState");
  313. pdfdatatable.Columns.Add("MaxPageRange");
  314. int datatableindex = 0;
  315. foreach (var fileName in fileNames)
  316. {
  317. string file_all = fileName;
  318. FileInfo f = new FileInfo(file_all);
  319. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  320. int pagecount = CPDFDocument.InitWithFilePath(fileName).PageCount;
  321. pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"],pagecount);
  322. datatableindex++;
  323. }
  324. PDFDataTable = pdfdatatable;
  325. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  326. }
  327. /// <summary>
  328. /// 更新Currentlistview显示
  329. /// state 状态显示字符串
  330. /// </summary>
  331. public void updateCurrentListview(string state = "wait")
  332. {
  333. if (fileNames.Count >= FileNameNumber)
  334. {
  335. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  336. {
  337. PDFCurrentDataTable.Rows.Add(state);
  338. }
  339. }
  340. else
  341. {
  342. Reverseorder(ref fileNamesView);
  343. foreach (int filenamesview in fileNamesView)
  344. {
  345. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  346. }
  347. }
  348. }
  349. /// <summary>
  350. /// 逆序int类型集合
  351. /// </summary>
  352. public void Reverseorder(ref List<int> Numbers)
  353. {
  354. Numbers = Numbers.OrderBy(a => a).ToList();
  355. Numbers.Reverse();
  356. }
  357. /// <summary>
  358. /// 打开文件PDF
  359. /// </summary>
  360. public void openfiledialog()
  361. {
  362. foreach (int filenamesview in fileNamesView)
  363. {
  364. System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
  365. }
  366. }
  367. /// <summary>
  368. /// 删除文件PDF
  369. /// </summary>
  370. public void removepdffile()
  371. {
  372. FileNameNumber = fileNames.Count;
  373. Reverseorder(ref fileNamesView);
  374. foreach (int filenamesview in fileNamesView)
  375. {
  376. //Trace.WriteLine(filenamesview);
  377. fileNames.Remove(fileNames[filenamesview]);
  378. }
  379. if (fileNames.Count < 1)
  380. {
  381. SetCompressGridIsEnabled = "False";
  382. AddFileVisibility = Visibility.Visible;
  383. }
  384. updateListview("wait");
  385. }
  386. public void removepdffile(int index)
  387. {
  388. PDFCurrentDataTable.Rows.RemoveAt(index);
  389. fileNames.Remove(fileNames[index]);
  390. if (fileNames.Count < 1)
  391. {
  392. SetCompressGridIsEnabled = "False";
  393. AddFileVisibility = Visibility.Visible;
  394. }
  395. updateListview("wait");
  396. }
  397. public void PDFFileCount()
  398. {
  399. if (fileNames.Count == 0)
  400. {
  401. SetCompressGridIsEnabled = "False";
  402. AddFileVisibility = Visibility.Visible;
  403. }
  404. else
  405. {
  406. SetCompressGridIsEnabled = "True";
  407. AddFileVisibility = Visibility.Collapsed;
  408. }
  409. }
  410. #endregion
  411. #region 构架行为
  412. public void OnNavigatedTo(NavigationContext navigationContext)
  413. {
  414. List<string> filepath = new List<string>();
  415. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  416. if (filepath != null)
  417. {
  418. fileNames = filepath;
  419. PDFFileCount();
  420. updateListview("wait");
  421. }
  422. }
  423. public bool IsNavigationTarget(NavigationContext navigationContext)
  424. {
  425. return true;
  426. }
  427. public void OnNavigatedFrom(NavigationContext navigationContext)
  428. {
  429. if(tempDocument!=null)
  430. tempDocument.CompressFile_Cancel(compressingIntpr);
  431. }
  432. #endregion
  433. }
  434. }