ConverterImgDialogViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using ComPDFKit_Conversion.Converter;
  2. using ComPDFKit_Conversion.Options;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.CustomControl;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.Dialog.ConverterDialogs;
  8. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Net.NetworkInformation;
  18. using System.Windows.Forms;
  19. using DialogResult = Prism.Services.Dialogs.DialogResult;
  20. namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
  21. {
  22. public class ConverterImgDialogViewModel : BindableBase, IDialogAware
  23. {
  24. public string Title => "";
  25. public event Action<IDialogResult> RequestClose;
  26. #region 参数和属性
  27. public ConverterImgDialogModel ConverterImgModel = new ConverterImgDialogModel();
  28. public CPDFViewer currentViewer;
  29. public string CurrentPageIndex = "1";
  30. public IDialogService dialogs;
  31. public string PageRangeText { set; get; } = "0";
  32. public string PageRangeSelectIndex { set; get; } = "0";
  33. private int maxPageRange = 0;
  34. public int MaxPageRange
  35. {
  36. get { return maxPageRange; }
  37. set
  38. {
  39. SetProperty(ref maxPageRange, value);
  40. }
  41. }
  42. public List<string> ImageTypeFamily { set; get; } = new List<string>();
  43. private int imageTypeSelectedIndex=0;
  44. /// <summary>
  45. /// 图像类型的下拉框索引值
  46. /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
  47. /// </summary>
  48. public int ImageTypeSelectedIndex
  49. {
  50. get { return imageTypeSelectedIndex; }
  51. set
  52. {
  53. SetProperty(ref imageTypeSelectedIndex, value);
  54. UpdateImageType(ImageTypeSelectedIndex);
  55. }
  56. }
  57. private int imageDPISelectedIndex;
  58. /// <summary>
  59. /// 图像类型的下拉框索引值
  60. /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
  61. /// </summary>
  62. public int ImageDPISelectedIndex
  63. {
  64. get { return imageDPISelectedIndex; }
  65. set
  66. {
  67. SetProperty(ref imageDPISelectedIndex, value);
  68. UpdateImageDPI(ImageDPISelectedIndex);
  69. }
  70. }
  71. private void SetImageTypeOrigin()
  72. {
  73. this.ImageTypeFamily.Add("JPEG");
  74. this.ImageTypeFamily.Add("JPG");
  75. this.ImageTypeFamily.Add("PNG");
  76. this.ImageTypeFamily.Add("GIF");
  77. this.ImageTypeFamily.Add("TIFF");
  78. this.ImageTypeFamily.Add("TGA");
  79. this.ImageTypeFamily.Add("BMP");
  80. this.ImageTypeFamily.Add("JPEG-2000");
  81. }
  82. public List<string> ImageDPIFamily { set; get; } = new List<string>();
  83. private void SetImageDPIOrigin()
  84. {
  85. this.ImageDPIFamily.Add("50 dpi");
  86. this.ImageDPIFamily.Add("72 dpi");
  87. this.ImageDPIFamily.Add("96 dpi");
  88. this.ImageDPIFamily.Add("150 dpi");
  89. this.ImageDPIFamily.Add("300 dpi");
  90. this.ImageDPIFamily.Add("600 dpi");
  91. }
  92. #endregion
  93. #region 委托声明
  94. public DelegateCommand CancelCommand { get; set; }
  95. public DelegateCommand ConverterCommnad { get; set; }
  96. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  97. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  98. public DelegateCommand BatchConverterCommand { get; set; }
  99. #endregion
  100. public ConverterImgDialogViewModel(IDialogService dialogService)
  101. {
  102. CancelCommand = new DelegateCommand(cancel);
  103. ConverterCommnad = new DelegateCommand(converter);
  104. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  105. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  106. BatchConverterCommand = new DelegateCommand(BatchConverter);
  107. SetImageTypeOrigin();
  108. SetImageDPIOrigin();
  109. dialogs = dialogService;
  110. }
  111. #region 逻辑函数
  112. /// <summary>
  113. /// 根据下拉框选项,选择不同的图片类型
  114. /// </summary>
  115. /// <param name="index"></param>
  116. private void UpdateImageType(int index)
  117. {
  118. this.ConverterImgModel.Options.ImageType = ImgType.PNG;
  119. switch (index)
  120. {
  121. case 0:
  122. this.ConverterImgModel.imageType = ".jpeg";
  123. break;
  124. case 7:
  125. this.ConverterImgModel.imageType = ".jp2";
  126. break;
  127. case 3:
  128. this.ConverterImgModel.imageType = ".gif";
  129. break;
  130. case 4:
  131. this.ConverterImgModel.imageType = ".tiff";
  132. break;
  133. case 5:
  134. this.ConverterImgModel.imageType = ".tga";
  135. break;
  136. case 6:
  137. this.ConverterImgModel.imageType = ".bmp";
  138. break;
  139. case 2:
  140. this.ConverterImgModel.imageType = ".png";
  141. break;
  142. case 1:
  143. this.ConverterImgModel.imageType = ".jpg";
  144. this.ConverterImgModel.Options.ImageType = ImgType.JPEG;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. private void UpdateImageDPI(int index)
  151. {
  152. switch (index)
  153. {
  154. case 0:
  155. this.ConverterImgModel.Options.ImageDpi = 50;
  156. Trace.WriteLine("50");
  157. break;
  158. case 1:
  159. this.ConverterImgModel.Options.ImageDpi = 72;
  160. Trace.WriteLine("72");
  161. break;
  162. case 2:
  163. this.ConverterImgModel.Options.ImageDpi = 96;
  164. Trace.WriteLine("96");
  165. break;
  166. case 3:
  167. this.ConverterImgModel.Options.ImageDpi = 150;
  168. Trace.WriteLine("150");
  169. break;
  170. case 4:
  171. this.ConverterImgModel.Options.ImageDpi = 300;
  172. Trace.WriteLine("300");
  173. break;
  174. case 5:
  175. this.ConverterImgModel.Options.ImageDpi = 600;
  176. Trace.WriteLine("600");
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. private void cancel()
  183. {
  184. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  185. }
  186. private void converter()
  187. {
  188. FolderBrowserDialog dlg = new FolderBrowserDialog();
  189. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  190. {
  191. ConverterImgModel.OutputPath = dlg.SelectedPath.Trim();
  192. }
  193. else
  194. {
  195. return;
  196. }
  197. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterImgModel.PageRange, PageRangeText, true, CurrentPageIndex);
  198. if (ConverterImgModel.PageRange == "")
  199. {
  200. Trace.WriteLine("输入不对");
  201. MessageBoxEx.Show("输入不对");
  202. return;
  203. }
  204. char[] enumerationSeparator = new char[] { ',' };
  205. char[] rangeSeparator = new char[] { '-' };
  206. if (!CommonHelper.GetPagesInRange(ref ConverterImgModel.PageIndexLists, ConverterImgModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  207. { //TODO
  208. Trace.WriteLine("输入不对");
  209. MessageBoxEx.Show("输入不对");
  210. return;
  211. }
  212. ConverterImgModel.imgOptions = ConverterImgModel.ImgOptions();
  213. // DialogParameters value = new DialogParameters();
  214. //value.Add(ParameterNames.ConverterType, "Img");
  215. //value.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
  216. var dialogresult = new DialogResult(ButtonResult.OK);
  217. dialogresult.Parameters.Add(ParameterNames.ConverterType, "Img");
  218. dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
  219. RequestClose.Invoke(dialogresult);
  220. //RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  221. //dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  222. //{
  223. //});
  224. }
  225. private void CmbPageSelectionChangedEvent(object e)
  226. {
  227. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  228. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  229. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  230. var ConverterPreview = e as PageTurningPreview;
  231. if (ConverterPreview != null)
  232. {
  233. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  234. var currentViewer = this.currentViewer;
  235. string PageRange = "";
  236. var PageRangeText = this.PageRangeText;
  237. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
  238. {
  239. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  240. char[] enumerationSeparator = new char[] { ',' };
  241. char[] rangeSeparator = new char[] { '-' };
  242. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  243. { //TODO
  244. ConverterPreview.PageIndexLists.Add(0);
  245. Trace.WriteLine("输入不对");
  246. MessageBoxEx.Show("输入不对");
  247. return;
  248. }
  249. }
  250. if (PageRangeSelectIndex == "1")
  251. {
  252. char[] enumerationSeparator = new char[] { ',' };
  253. char[] rangeSeparator = new char[] { '-' };
  254. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  255. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  256. { //TODO
  257. Trace.WriteLine("输入不对");
  258. MessageBoxEx.Show("输入不对");
  259. return;
  260. }
  261. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  262. }
  263. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Count).ToString();
  264. ConverterPreview.CurrentIndex = 0;
  265. }
  266. }
  267. private void CmbPageTextChangedEvent(object e)
  268. {
  269. var ConverterPreview = e as PageTurningPreview;
  270. if (ConverterPreview != null)
  271. {
  272. if (ConverterPreview != null)
  273. {
  274. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  275. var currentViewer = this.currentViewer;
  276. string PageRange = "";
  277. var PageRangeText = this.PageRangeText;
  278. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  279. char[] enumerationSeparator = new char[] { ',' };
  280. char[] rangeSeparator = new char[] { '-' };
  281. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  282. { //TODO
  283. ConverterPreview.PageIndexLists.Add(0);
  284. return;
  285. }
  286. else
  287. {
  288. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  289. ConverterPreview.CurrentIndex = 0;
  290. }
  291. }
  292. }
  293. }
  294. private void BatchConverter()
  295. {
  296. DialogParameters convertpdftoword = new DialogParameters();
  297. convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  298. convertpdftoword.Add("ConverterTypeIndex", 7);
  299. HomePageBatchProcessingDialogModel.FilePaths = new List<string> { currentViewer.Document.FilePath.ToString() };
  300. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  301. convertpdftoword.Add(ParameterNames.FilePath, new string[] { currentViewer.Document.FilePath.ToString() });
  302. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  303. }
  304. #endregion
  305. #region 构架行为
  306. public bool CanCloseDialog()
  307. {
  308. return true;
  309. }
  310. public void OnDialogClosed()
  311. {
  312. }
  313. public void OnDialogOpened(IDialogParameters parameters)
  314. {
  315. CPDFViewer pdfViewer = null;
  316. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  317. if (pdfViewer != null)
  318. {
  319. currentViewer = pdfViewer;
  320. MaxPageRange = currentViewer.Document.PageCount;
  321. if (currentViewer.Tag != null) { ConverterImgModel.Pawssword = currentViewer.Tag.ToString(); }
  322. ConverterImgModel.InputPath = pdfViewer.Document.FilePath;
  323. ImageTypeSelectedIndex = 0;
  324. ImageDPISelectedIndex = 0;
  325. FileInfo fileinfo = new FileInfo(ConverterImgModel.InputPath);
  326. ConverterImgModel.OutputPath = fileinfo.DirectoryName;
  327. }
  328. }
  329. #endregion
  330. }
  331. }