HomePageCompressDialogViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Windows;
  15. using System.Windows.Forms;
  16. using DataTable = System.Data.DataTable;
  17. using DialogResult = Prism.Services.Dialogs.DialogResult;
  18. using Task = System.Threading.Tasks.Task;
  19. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing
  20. {
  21. public class HomePageCompressDialogViewModel : BindableBase, INavigationAware
  22. {
  23. private List<string> fileNames;
  24. private CPDFDocument tempDocument;
  25. public List<int> fileNamesView = new List<int>();
  26. private int fileNamesIndex = 0;
  27. private IntPtr compressingIntpr = IntPtr.Zero;
  28. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  29. HomePageCompressDialogModel homePageCompressDialogModel=new HomePageCompressDialogModel();
  30. private DataTable pdfDataTable = new DataTable();
  31. public DataTable PDFDataTable
  32. {
  33. get { return pdfDataTable; }
  34. set
  35. {
  36. SetProperty(ref pdfDataTable, value);
  37. }
  38. }
  39. private string compressGridIsEnabled="True";
  40. public string CompressGridIsEnabled
  41. {
  42. get
  43. {
  44. return compressGridIsEnabled;
  45. }
  46. set
  47. {
  48. SetProperty(ref compressGridIsEnabled, value);
  49. }
  50. }
  51. private string setCompressGridIsEnabled = "True";
  52. public string SetCompressGridIsEnabled
  53. {
  54. get
  55. {
  56. return setCompressGridIsEnabled;
  57. }
  58. set
  59. {
  60. SetProperty(ref setCompressGridIsEnabled, value);
  61. }
  62. }
  63. private string removeIsEnable = "False";
  64. public string RemoveIsEnable
  65. {
  66. get { return removeIsEnable; }
  67. set
  68. {
  69. SetProperty(ref removeIsEnable, value);
  70. }
  71. }
  72. private string selectFileName = "False";
  73. public string SelectFileName
  74. {
  75. get { return selectFileName; }
  76. set
  77. {
  78. SetProperty(ref selectFileName, value);
  79. }
  80. }
  81. private Visibility addFileVisibility = Visibility.Hidden;
  82. public Visibility AddFileVisibility
  83. {
  84. get { return addFileVisibility; }
  85. set
  86. {
  87. SetProperty(ref addFileVisibility, value);
  88. RaisePropertyChanged();
  89. }
  90. }
  91. public DelegateCommand ADDPDFCommand { get; set; }
  92. public DelegateCommand RemovePDFFileCommand { get; set; }
  93. public DelegateCommand ADDPDFFilesCommand { get; set; }
  94. public DelegateCommand CompressCommand { get; set; }
  95. public DelegateCommand<string> RadioButtonCommand{ get; set; }
  96. public HomePageCompressDialogViewModel()
  97. {
  98. PDFDataTable.Columns.Add("FileName");
  99. PDFDataTable.Columns.Add("FileSize");
  100. PDFDataTable.Columns.Add("FileState");
  101. ADDPDFCommand = new DelegateCommand(addpicture);
  102. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  103. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  104. CompressCommand = new DelegateCommand(compress);
  105. RadioButtonCommand = new DelegateCommand<string>(RadioButton);
  106. }
  107. private void RadioButton(string sender) {
  108. if (sender != "") {
  109. switch (sender) {
  110. case "LargeQualityRadioBtn":
  111. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLarge;
  112. break;
  113. case "StandardQualityRadioBtn":
  114. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusStandard;
  115. break;
  116. case "LittleQualityRadioBtn":
  117. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLittle;
  118. break;
  119. case "MicroQualityRadioBtn":
  120. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusMicro;
  121. break;
  122. default:
  123. homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusDefault;
  124. break;
  125. }
  126. }
  127. }
  128. private int GetIndex(int pageindex)
  129. {
  130. PDFDataTable.Rows[fileNamesIndex]["FileState"]="已经压缩"+pageindex.ToString()+"页";
  131. return 0;
  132. }
  133. private async void compress() {
  134. CompressGridIsEnabled = "False";
  135. //GC.KeepAlive(indexDelegate);
  136. fileNamesIndex = 0;
  137. foreach (var filename in fileNames)
  138. {
  139. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  140. if (document == null) {
  141. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  142. continue;
  143. }
  144. FileInfo fileinfo = new FileInfo(filename);
  145. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  146. indexDelegate += GetIndex;
  147. compressingIntpr = document.CompressFile_Init(((float)homePageCompressDialogModel.CompressQuality), indexDelegate);
  148. tempDocument = document;
  149. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filename+"compress.pdf"); });
  150. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  151. fileNamesIndex++;
  152. document.Release();
  153. }
  154. CompressGridIsEnabled = "True";
  155. MessageBoxEx.Show("已完成");
  156. SetCompressGridIsEnabled = "False";
  157. AddFileVisibility = Visibility.Visible;
  158. fileNames.Clear();
  159. HomePageBatchProcessingDialogModel.FilePaths.Clear();
  160. }
  161. /// <summary>
  162. /// 添加PDF文件
  163. /// </summary>
  164. private void addpicture()
  165. {
  166. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  167. dlg.Multiselect = true;
  168. dlg.Filter = "PDF|*.pdf;*.PDF;";
  169. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  170. {
  171. fileNames.AddRange(dlg.FileNames.ToList());
  172. RemoveExcess(ref fileNames);
  173. SetCompressGridIsEnabled = "True";
  174. AddFileVisibility = Visibility.Collapsed;
  175. updateListview("待确定");
  176. }
  177. }
  178. /// <summary>
  179. /// 删除重复的文件
  180. /// </summary>
  181. public void RemoveExcess(ref List<string> Filenames)
  182. {
  183. List<string> filenames = new List<string>();
  184. foreach (var fileName in Filenames)
  185. {
  186. if (!filenames.Contains(fileName))
  187. {
  188. filenames.Add(fileName);
  189. }
  190. }
  191. Filenames.Clear();
  192. Filenames = filenames;
  193. }
  194. /// <summary>
  195. /// 添加PDF文件夹
  196. /// </summary>
  197. private void addpicturefiles()
  198. {
  199. FolderBrowserDialog dialog = new FolderBrowserDialog();
  200. dialog.Description = "请选择文件路径";
  201. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  202. {
  203. string foldPath = dialog.SelectedPath;
  204. var apps = System.IO.Directory.GetFiles(foldPath);
  205. foreach (string app in apps)
  206. {
  207. var fi = new FileInfo(app);
  208. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  209. {
  210. fileNames.Add(app);
  211. }
  212. }
  213. RemoveExcess(ref fileNames);
  214. updateListview("待确定");
  215. SetCompressGridIsEnabled = "True";
  216. AddFileVisibility = Visibility.Collapsed;
  217. }
  218. }
  219. /// <summary>
  220. /// 更新listview显示
  221. /// state 状态显示字符串
  222. /// </summary>
  223. public void updateListview(string state)
  224. {
  225. DataTable pdfdatatable = new DataTable();
  226. pdfdatatable.Columns.Add("FileName");
  227. pdfdatatable.Columns.Add("FileSize");
  228. pdfdatatable.Columns.Add("FileState");
  229. foreach (var fileName in fileNames)
  230. {
  231. string file_all = fileName;
  232. FileInfo f = new FileInfo(file_all);
  233. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  234. pdfdatatable.Rows.Add(f.Name, file_size, state);
  235. }
  236. PDFDataTable = pdfdatatable;
  237. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  238. }
  239. /// <summary>
  240. /// 逆序int类型集合
  241. /// </summary>
  242. public void Reverseorder(ref List<int> Numbers)
  243. {
  244. Numbers = Numbers.OrderBy(a => a).ToList();
  245. Numbers.Reverse();
  246. }
  247. /// <summary>
  248. /// 打开文件PDF
  249. /// </summary>
  250. public void openfiledialog()
  251. {
  252. foreach (int filenamesview in fileNamesView)
  253. {
  254. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  255. }
  256. }
  257. /// <summary>
  258. /// 删除文件PDF
  259. /// </summary>
  260. public void removepdffile()
  261. {
  262. Reverseorder(ref fileNamesView);
  263. foreach (int filenamesview in fileNamesView)
  264. {
  265. //Trace.WriteLine(filenamesview);
  266. fileNames.Remove(fileNames[filenamesview]);
  267. }
  268. if (fileNames.Count < 1)
  269. {
  270. SetCompressGridIsEnabled = "False";
  271. AddFileVisibility = Visibility.Visible;
  272. }
  273. updateListview("待确定");
  274. }
  275. public void PDFFileCount()
  276. {
  277. if (fileNames.Count == 0)
  278. {
  279. SetCompressGridIsEnabled = "False";
  280. AddFileVisibility = Visibility.Visible;
  281. }
  282. else
  283. {
  284. SetCompressGridIsEnabled = "True";
  285. AddFileVisibility = Visibility.Collapsed;
  286. }
  287. }
  288. public void OnNavigatedTo(NavigationContext navigationContext)
  289. {
  290. List<string> filepath = new List<string>();
  291. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  292. if (filepath != null)
  293. {
  294. fileNames = filepath;
  295. PDFFileCount();
  296. updateListview("待确定");
  297. }
  298. }
  299. public bool IsNavigationTarget(NavigationContext navigationContext)
  300. {
  301. return true;
  302. }
  303. public void OnNavigatedFrom(NavigationContext navigationContext)
  304. {
  305. if(tempDocument!=null)
  306. tempDocument.CompressFile_Cancel(compressingIntpr);
  307. }
  308. }
  309. }