HomePageCompressDialogViewModel.cs 17 KB

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