HomePageCompressDialogViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 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 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 RemovePDFFileCommand { get; set; }
  109. public DelegateCommand ADDPDFFilesCommand { get; set; }
  110. public DelegateCommand CompressCommand { get; set; }
  111. public DelegateCommand<string> RadioButtonCommand{ get; set; }
  112. #endregion
  113. public HomePageCompressDialogViewModel(IDialogService dialogs)
  114. {
  115. PDFCurrentDataTable.Columns.Add("FileState");
  116. PDFDataTable.Columns.Add("FileName");
  117. PDFDataTable.Columns.Add("FileSize");
  118. PDFDataTable.Columns.Add("FileState");
  119. ADDPDFCommand = new DelegateCommand(addpicture);
  120. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  121. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  122. CompressCommand = new DelegateCommand(compress);
  123. RadioButtonCommand = new DelegateCommand<string>(RadioButton);
  124. this.dialogs = dialogs;
  125. }
  126. #region 逻辑函数
  127. private void RadioButton(string sender) {
  128. if (sender != "") {
  129. switch (sender) {
  130. case "LargeQualityRadioBtn":
  131. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLarge;
  132. break;
  133. case "StandardQualityRadioBtn":
  134. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusStandard;
  135. break;
  136. case "LittleQualityRadioBtn":
  137. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLittle;
  138. break;
  139. case "MicroQualityRadioBtn":
  140. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusMicro;
  141. break;
  142. default:
  143. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusDefault;
  144. break;
  145. }
  146. }
  147. }
  148. private int GetIndex(int pageindex)
  149. {
  150. PDFDataTable.Rows[fileNamesIndex]["FileState"]="已经压缩"+pageindex.ToString()+"页";
  151. return 0;
  152. }
  153. private async void compress() {
  154. CompressGridIsEnabled = "False";
  155. //GC.KeepAlive(indexDelegate);
  156. fileNamesIndex = 0;
  157. foreach (var filename in fileNames)
  158. {
  159. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  160. if (document == null)
  161. {
  162. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  163. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  164. fileNamesIndex++;
  165. continue;
  166. }
  167. if (document.IsLocked)
  168. {
  169. DialogParameters value = new DialogParameters();
  170. value.Add(ParameterNames.PDFDocument, document);
  171. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  172. {
  173. if (e.Result == ButtonResult.OK)
  174. {
  175. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  176. {
  177. document.UnlockWithPassword (e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  178. }
  179. }
  180. });
  181. if (document.IsLocked)
  182. {
  183. //未成功解密文档时,释放Document对象,返回
  184. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  185. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  186. fileNamesIndex++;
  187. continue;
  188. }
  189. }
  190. FileInfo fileinfo = new FileInfo(filename);
  191. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  192. indexDelegate += GetIndex;
  193. compressingIntpr = document.CompressFile_Init(((float)homePageCompressDialogModel.CompressQuality), indexDelegate);
  194. tempDocument = document;
  195. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, CommonHelper.CreateFilePath(filename +"compress.pdf")); });
  196. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  197. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  198. fileNamesIndex++;
  199. document.Release();
  200. }
  201. CompressGridIsEnabled = "True";
  202. }
  203. #endregion
  204. #region 批量处理逻辑函数
  205. /// <summary>
  206. /// 添加PDF文件
  207. /// </summary>
  208. private void addpicture()
  209. {
  210. FileNameNumber = fileNames.Count;
  211. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  212. dlg.Multiselect = true;
  213. dlg.Filter = "PDF|*.pdf;*.PDF;";
  214. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  215. {
  216. fileNames.AddRange(dlg.FileNames.ToList());
  217. RemoveExcess(ref fileNames);
  218. SetCompressGridIsEnabled = "True";
  219. AddFileVisibility = Visibility.Collapsed;
  220. updateListview("待确定");
  221. }
  222. }
  223. public void addPDFFiles(string filename)
  224. {
  225. FileNameNumber = fileNames.Count;
  226. fileNames.Add(filename);
  227. RemoveExcess(ref fileNames);
  228. SetCompressGridIsEnabled = "True";
  229. AddFileVisibility = Visibility.Collapsed;
  230. updateListview("待确定");
  231. }
  232. /// <summary>
  233. /// 删除重复的文件
  234. /// </summary>
  235. public void RemoveExcess(ref List<string> Filenames)
  236. {
  237. List<string> filenames = new List<string>();
  238. foreach (var fileName in Filenames)
  239. {
  240. if (!filenames.Contains(fileName))
  241. {
  242. filenames.Add(fileName);
  243. }
  244. }
  245. Filenames.Clear();
  246. Filenames = filenames;
  247. }
  248. /// <summary>
  249. /// 添加PDF文件夹
  250. /// </summary>
  251. private void addpicturefiles()
  252. {
  253. FileNameNumber = fileNames.Count;
  254. FolderBrowserDialog dialog = new FolderBrowserDialog();
  255. dialog.Description = "请选择文件路径";
  256. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  257. {
  258. string foldPath = dialog.SelectedPath;
  259. var apps = System.IO.Directory.GetFiles(foldPath);
  260. foreach (string app in apps)
  261. {
  262. var fi = new FileInfo(app);
  263. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  264. {
  265. fileNames.Add(app);
  266. }
  267. }
  268. RemoveExcess(ref fileNames);
  269. updateListview("待确定");
  270. SetCompressGridIsEnabled = "True";
  271. AddFileVisibility = Visibility.Collapsed;
  272. }
  273. }
  274. /// <summary>
  275. /// 更新listview显示
  276. /// state 状态显示字符串
  277. /// </summary>
  278. public void updateListview(string state)
  279. {
  280. updateCurrentListview();
  281. DataTable pdfdatatable = new DataTable();
  282. pdfdatatable.Columns.Add("FileName");
  283. pdfdatatable.Columns.Add("FileSize");
  284. pdfdatatable.Columns.Add("FileState");
  285. int datatableindex = 0;
  286. foreach (var fileName in fileNames)
  287. {
  288. string file_all = fileName;
  289. FileInfo f = new FileInfo(file_all);
  290. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  291. pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  292. datatableindex++;
  293. }
  294. PDFDataTable = pdfdatatable;
  295. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  296. }
  297. /// <summary>
  298. /// 更新Currentlistview显示
  299. /// state 状态显示字符串
  300. /// </summary>
  301. public void updateCurrentListview(string state = "待完成")
  302. {
  303. if (fileNames.Count >= FileNameNumber)
  304. {
  305. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  306. {
  307. PDFCurrentDataTable.Rows.Add(state);
  308. }
  309. }
  310. else
  311. {
  312. Reverseorder(ref fileNamesView);
  313. foreach (int filenamesview in fileNamesView)
  314. {
  315. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 逆序int类型集合
  321. /// </summary>
  322. public void Reverseorder(ref List<int> Numbers)
  323. {
  324. Numbers = Numbers.OrderBy(a => a).ToList();
  325. Numbers.Reverse();
  326. }
  327. /// <summary>
  328. /// 打开文件PDF
  329. /// </summary>
  330. public void openfiledialog()
  331. {
  332. foreach (int filenamesview in fileNamesView)
  333. {
  334. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  335. }
  336. }
  337. /// <summary>
  338. /// 删除文件PDF
  339. /// </summary>
  340. public void removepdffile()
  341. {
  342. FileNameNumber = fileNames.Count;
  343. Reverseorder(ref fileNamesView);
  344. foreach (int filenamesview in fileNamesView)
  345. {
  346. //Trace.WriteLine(filenamesview);
  347. fileNames.Remove(fileNames[filenamesview]);
  348. }
  349. if (fileNames.Count < 1)
  350. {
  351. SetCompressGridIsEnabled = "False";
  352. AddFileVisibility = Visibility.Visible;
  353. }
  354. updateListview("待确定");
  355. }
  356. public void removepdffile(int index)
  357. {
  358. PDFCurrentDataTable.Rows.RemoveAt(index);
  359. fileNames.Remove(fileNames[index]);
  360. if (fileNames.Count < 1)
  361. {
  362. SetCompressGridIsEnabled = "False";
  363. AddFileVisibility = Visibility.Visible;
  364. }
  365. updateListview("待确定");
  366. }
  367. public void PDFFileCount()
  368. {
  369. if (fileNames.Count == 0)
  370. {
  371. SetCompressGridIsEnabled = "False";
  372. AddFileVisibility = Visibility.Visible;
  373. }
  374. else
  375. {
  376. SetCompressGridIsEnabled = "True";
  377. AddFileVisibility = Visibility.Collapsed;
  378. }
  379. }
  380. #endregion
  381. #region 构架行为
  382. public void OnNavigatedTo(NavigationContext navigationContext)
  383. {
  384. List<string> filepath = new List<string>();
  385. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  386. if (filepath != null)
  387. {
  388. fileNames = filepath;
  389. PDFFileCount();
  390. updateListview("待确定");
  391. }
  392. }
  393. public bool IsNavigationTarget(NavigationContext navigationContext)
  394. {
  395. return true;
  396. }
  397. public void OnNavigatedFrom(NavigationContext navigationContext)
  398. {
  399. if(tempDocument!=null)
  400. tempDocument.CompressFile_Cancel(compressingIntpr);
  401. }
  402. #endregion
  403. }
  404. }