HomePageCompressDialogViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. public void addPDFFiles(string filename)
  182. {
  183. fileNames.Add(filename);
  184. RemoveExcess(ref fileNames);
  185. SetCompressGridIsEnabled = "True";
  186. AddFileVisibility = Visibility.Collapsed;
  187. updateListview("待确定");
  188. }
  189. /// <summary>
  190. /// 删除重复的文件
  191. /// </summary>
  192. public void RemoveExcess(ref List<string> Filenames)
  193. {
  194. List<string> filenames = new List<string>();
  195. foreach (var fileName in Filenames)
  196. {
  197. if (!filenames.Contains(fileName))
  198. {
  199. filenames.Add(fileName);
  200. }
  201. }
  202. Filenames.Clear();
  203. Filenames = filenames;
  204. }
  205. /// <summary>
  206. /// 添加PDF文件夹
  207. /// </summary>
  208. private void addpicturefiles()
  209. {
  210. FolderBrowserDialog dialog = new FolderBrowserDialog();
  211. dialog.Description = "请选择文件路径";
  212. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  213. {
  214. string foldPath = dialog.SelectedPath;
  215. var apps = System.IO.Directory.GetFiles(foldPath);
  216. foreach (string app in apps)
  217. {
  218. var fi = new FileInfo(app);
  219. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  220. {
  221. fileNames.Add(app);
  222. }
  223. }
  224. RemoveExcess(ref fileNames);
  225. updateListview("待确定");
  226. SetCompressGridIsEnabled = "True";
  227. AddFileVisibility = Visibility.Collapsed;
  228. }
  229. }
  230. /// <summary>
  231. /// 更新listview显示
  232. /// state 状态显示字符串
  233. /// </summary>
  234. public void updateListview(string state)
  235. {
  236. DataTable pdfdatatable = new DataTable();
  237. pdfdatatable.Columns.Add("FileName");
  238. pdfdatatable.Columns.Add("FileSize");
  239. pdfdatatable.Columns.Add("FileState");
  240. foreach (var fileName in fileNames)
  241. {
  242. string file_all = fileName;
  243. FileInfo f = new FileInfo(file_all);
  244. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  245. pdfdatatable.Rows.Add(f.Name, file_size, state);
  246. }
  247. PDFDataTable = pdfdatatable;
  248. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  249. }
  250. /// <summary>
  251. /// 逆序int类型集合
  252. /// </summary>
  253. public void Reverseorder(ref List<int> Numbers)
  254. {
  255. Numbers = Numbers.OrderBy(a => a).ToList();
  256. Numbers.Reverse();
  257. }
  258. /// <summary>
  259. /// 打开文件PDF
  260. /// </summary>
  261. public void openfiledialog()
  262. {
  263. foreach (int filenamesview in fileNamesView)
  264. {
  265. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  266. }
  267. }
  268. /// <summary>
  269. /// 删除文件PDF
  270. /// </summary>
  271. public void removepdffile()
  272. {
  273. Reverseorder(ref fileNamesView);
  274. foreach (int filenamesview in fileNamesView)
  275. {
  276. //Trace.WriteLine(filenamesview);
  277. fileNames.Remove(fileNames[filenamesview]);
  278. }
  279. if (fileNames.Count < 1)
  280. {
  281. SetCompressGridIsEnabled = "False";
  282. AddFileVisibility = Visibility.Visible;
  283. }
  284. updateListview("待确定");
  285. }
  286. public void PDFFileCount()
  287. {
  288. if (fileNames.Count == 0)
  289. {
  290. SetCompressGridIsEnabled = "False";
  291. AddFileVisibility = Visibility.Visible;
  292. }
  293. else
  294. {
  295. SetCompressGridIsEnabled = "True";
  296. AddFileVisibility = Visibility.Collapsed;
  297. }
  298. }
  299. #endregion
  300. #region 构架行为
  301. public void OnNavigatedTo(NavigationContext navigationContext)
  302. {
  303. List<string> filepath = new List<string>();
  304. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  305. if (filepath != null)
  306. {
  307. fileNames = filepath;
  308. PDFFileCount();
  309. updateListview("待确定");
  310. }
  311. }
  312. public bool IsNavigationTarget(NavigationContext navigationContext)
  313. {
  314. return true;
  315. }
  316. public void OnNavigatedFrom(NavigationContext navigationContext)
  317. {
  318. if(tempDocument!=null)
  319. tempDocument.CompressFile_Cancel(compressingIntpr);
  320. }
  321. #endregion
  322. }
  323. }