HomePageCompressDialogViewModel.cs 16 KB

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