HomePageRemoveDialogViewModel.cs 11 KB

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