HomePageWatermarkDialogViewModel.cs 11 KB

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