HomePageCompressDialogViewModel.cs 12 KB

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