ConverterImgDialogViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using ComPDFKit_Conversion.Converter;
  2. using ComPDFKit_Conversion.Options;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Master.CustomControl;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using PDF_Master.Model.Dialog.ConverterDialogs;
  8. using PDF_Master.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_Master.ViewModels.Dialog.ConverterDialogs
  21. {
  22. public class ConverterImgDialogViewModel : BindableBase, IDialogAware
  23. {
  24. #region 文案
  25. private string T_title;
  26. public string T_Title
  27. {
  28. get { return T_title; }
  29. set
  30. {
  31. SetProperty(ref T_title, value);
  32. }
  33. }
  34. private string T_yes;
  35. public string T_YES
  36. {
  37. get { return T_yes; }
  38. set
  39. {
  40. SetProperty(ref T_yes, value);
  41. }
  42. }
  43. private string T_no;
  44. public string T_No
  45. {
  46. get { return T_no; }
  47. set
  48. {
  49. SetProperty(ref T_no, value);
  50. }
  51. }
  52. private void IntString()
  53. {
  54. T_Title = App.MainPageLoader.GetString("Convert_PDFToImageTitle");
  55. T_YES = App.MainPageLoader.GetString("Convert_Yes");
  56. T_No = App.MainPageLoader.GetString("Convert_No");
  57. }
  58. #endregion
  59. public string Title => "";
  60. public event Action<IDialogResult> RequestClose;
  61. #region 参数和属性
  62. public ConverterImgDialogModel ConverterImgModel = new ConverterImgDialogModel();
  63. public CPDFViewer currentViewer;
  64. public string CurrentPageIndex = "1";
  65. public bool IsCurrentPageIndex = true;
  66. public IDialogService dialogs;
  67. public string PageRangeText { set; get; } = "0";
  68. public string PageRangeSelectIndex { set; get; } = "0";
  69. private int maxPageRange = 0;
  70. public int MaxPageRange
  71. {
  72. get { return maxPageRange; }
  73. set
  74. {
  75. SetProperty(ref maxPageRange, value);
  76. }
  77. }
  78. public List<string> ImageTypeFamily { set; get; } = new List<string>();
  79. private int imageTypeSelectedIndex=0;
  80. /// <summary>
  81. /// 图像类型的下拉框索引值
  82. /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
  83. /// </summary>
  84. public int ImageTypeSelectedIndex
  85. {
  86. get { return imageTypeSelectedIndex; }
  87. set
  88. {
  89. SetProperty(ref imageTypeSelectedIndex, value);
  90. UpdateImageType(ImageTypeSelectedIndex);
  91. }
  92. }
  93. private int imageDPISelectedIndex;
  94. /// <summary>
  95. /// 图像类型的下拉框索引值
  96. /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
  97. /// </summary>
  98. public int ImageDPISelectedIndex
  99. {
  100. get { return imageDPISelectedIndex; }
  101. set
  102. {
  103. SetProperty(ref imageDPISelectedIndex, value);
  104. UpdateImageDPI(ImageDPISelectedIndex);
  105. }
  106. }
  107. private void SetImageTypeOrigin()
  108. {
  109. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageJPEG"));
  110. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageJPG"));
  111. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImagePNG"));
  112. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageGIF"));
  113. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageTIFF"));
  114. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageTGA"));
  115. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageBMP"));
  116. this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageJPEG-2000"));
  117. }
  118. public List<string> ImageDPIFamily { set; get; } = new List<string>();
  119. private void SetImageDPIOrigin()
  120. {
  121. this.ImageDPIFamily.Add("50 dpi");
  122. this.ImageDPIFamily.Add("72 dpi");
  123. this.ImageDPIFamily.Add("96 dpi");
  124. this.ImageDPIFamily.Add("150 dpi");
  125. this.ImageDPIFamily.Add("300 dpi");
  126. this.ImageDPIFamily.Add("600 dpi");
  127. }
  128. private Dictionary<string, string> CheckPageSelect = new Dictionary<string, string>();
  129. private void InitCheckPageSelect()
  130. {
  131. CheckPageSelect.Clear();
  132. if (!IsCurrentPageIndex)
  133. {
  134. CheckPageSelect.Add("0", "0");
  135. CheckPageSelect.Add("1", "1");
  136. CheckPageSelect.Add("2", "2");
  137. CheckPageSelect.Add("3", "3");
  138. CheckPageSelect.Add("4", "3");
  139. }
  140. else
  141. {
  142. CheckPageSelect.Add("0", "0");
  143. CheckPageSelect.Add("1", "-2");
  144. CheckPageSelect.Add("2", "1");
  145. CheckPageSelect.Add("3", "2");
  146. CheckPageSelect.Add("4", "3");
  147. }
  148. }
  149. #endregion
  150. #region 委托声明
  151. public DelegateCommand CancelCommand { get; set; }
  152. public DelegateCommand ConverterCommnad { get; set; }
  153. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  154. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  155. public DelegateCommand SetCustomPageRangeCommand { get; set; }
  156. public DelegateCommand BatchConverterCommand { get; set; }
  157. #endregion
  158. public ConverterImgDialogViewModel(IDialogService dialogService)
  159. {
  160. IntString();
  161. CancelCommand = new DelegateCommand(cancel);
  162. ConverterCommnad = new DelegateCommand(converter);
  163. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  164. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  165. SetCustomPageRangeCommand = new DelegateCommand(SetCustomPageRange);
  166. BatchConverterCommand = new DelegateCommand(BatchConverter);
  167. SetImageTypeOrigin();
  168. SetImageDPIOrigin();
  169. dialogs = dialogService;
  170. }
  171. #region 逻辑函数
  172. public void SetCustomPageRange()
  173. {
  174. if (PageRangeSelectIndex == CheckPageSelect["4"])
  175. {
  176. List<int> PageIndexLists = new List<int>();
  177. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  178. { //TODO
  179. AlertsMessage alertsMessage = new AlertsMessage();
  180. alertsMessage.ShowDialog("页面范围输入错误", "", "确定");
  181. if (alertsMessage.result == ContentResult.Ok)
  182. {
  183. return;
  184. }
  185. else
  186. {
  187. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  188. //{
  189. // Unicode = Unicode,
  190. // Status = EnumDelete.StatusCreate
  191. //});
  192. return;
  193. }
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// 根据下拉框选项,选择不同的图片类型
  199. /// </summary>
  200. /// <param name="index"></param>
  201. private void UpdateImageType(int index)
  202. {
  203. this.ConverterImgModel.Options.ImageType = ImgType.PNG;
  204. switch (index)
  205. {
  206. case 0:
  207. this.ConverterImgModel.imageType = ".jpeg";
  208. break;
  209. case 7:
  210. this.ConverterImgModel.imageType = ".jp2";
  211. break;
  212. case 3:
  213. this.ConverterImgModel.imageType = ".gif";
  214. break;
  215. case 4:
  216. this.ConverterImgModel.imageType = ".tiff";
  217. break;
  218. case 5:
  219. this.ConverterImgModel.imageType = ".tga";
  220. break;
  221. case 6:
  222. this.ConverterImgModel.imageType = ".bmp";
  223. break;
  224. case 2:
  225. this.ConverterImgModel.imageType = ".png";
  226. break;
  227. case 1:
  228. this.ConverterImgModel.imageType = ".jpg";
  229. this.ConverterImgModel.Options.ImageType = ImgType.JPEG;
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. private void UpdateImageDPI(int index)
  236. {
  237. switch (index)
  238. {
  239. case 0:
  240. this.ConverterImgModel.Options.ImageDpi = 50;
  241. Trace.WriteLine("50");
  242. break;
  243. case 1:
  244. this.ConverterImgModel.Options.ImageDpi = 72;
  245. Trace.WriteLine("72");
  246. break;
  247. case 2:
  248. this.ConverterImgModel.Options.ImageDpi = 96;
  249. Trace.WriteLine("96");
  250. break;
  251. case 3:
  252. this.ConverterImgModel.Options.ImageDpi = 150;
  253. Trace.WriteLine("150");
  254. break;
  255. case 4:
  256. this.ConverterImgModel.Options.ImageDpi = 300;
  257. Trace.WriteLine("300");
  258. break;
  259. case 5:
  260. this.ConverterImgModel.Options.ImageDpi = 600;
  261. Trace.WriteLine("600");
  262. break;
  263. default:
  264. break;
  265. }
  266. }
  267. private void cancel()
  268. {
  269. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  270. }
  271. private void converter()
  272. {
  273. if (PageRangeSelectIndex == CheckPageSelect["4"])
  274. {
  275. List<int> PageIndexLists = new List<int>();
  276. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  277. { //TODO
  278. AlertsMessage alertsMessage = new AlertsMessage();
  279. alertsMessage.ShowDialog("页面范围输入错误", "", "确定");
  280. if (alertsMessage.result == ContentResult.Ok)
  281. {
  282. return;
  283. }
  284. else
  285. {
  286. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  287. //{
  288. // Unicode = Unicode,
  289. // Status = EnumDelete.StatusCreate
  290. //});
  291. return;
  292. }
  293. }
  294. }
  295. FolderBrowserDialog dlg = new FolderBrowserDialog();
  296. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  297. {
  298. ConverterImgModel.OutputPath = dlg.SelectedPath.Trim();
  299. }
  300. else
  301. {
  302. return;
  303. }
  304. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterImgModel.PageRange, PageRangeText, !IsCurrentPageIndex, CurrentPageIndex);
  305. if (ConverterImgModel.PageRange == "")
  306. {
  307. Trace.WriteLine("输入不对");
  308. MessageBoxEx.Show("输入不对");
  309. return;
  310. }
  311. char[] enumerationSeparator = new char[] { ',' };
  312. char[] rangeSeparator = new char[] { '-' };
  313. if (!CommonHelper.GetPagesInRange(ref ConverterImgModel.PageIndexLists, ConverterImgModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  314. { //TODO
  315. Trace.WriteLine("输入不对");
  316. MessageBoxEx.Show("输入不对");
  317. return;
  318. }
  319. ConverterImgModel.imgOptions = ConverterImgModel.ImgOptions();
  320. // DialogParameters value = new DialogParameters();
  321. //value.Add(ParameterNames.ConverterType, "Img");
  322. //value.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
  323. var dialogresult = new DialogResult(ButtonResult.OK);
  324. dialogresult.Parameters.Add(ParameterNames.ConverterType, "Img");
  325. dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
  326. RequestClose.Invoke(dialogresult);
  327. //RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  328. //dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  329. //{
  330. //});
  331. }
  332. private void CmbPageSelectionChangedEvent(object e)
  333. {
  334. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  335. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  336. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  337. var ConverterPreview = e as PageTurningPreview;
  338. if (ConverterPreview != null)
  339. {
  340. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  341. var currentViewer = this.currentViewer;
  342. string PageRange = "";
  343. var PageRangeText = this.PageRangeText;
  344. if (PageRangeSelectIndex == CheckPageSelect["0"] || PageRangeSelectIndex == CheckPageSelect["2"] || PageRangeSelectIndex == CheckPageSelect["3"])
  345. {
  346. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex);
  347. char[] enumerationSeparator = new char[] { ',' };
  348. char[] rangeSeparator = new char[] { '-' };
  349. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  350. { //TODO
  351. ConverterPreview.PageIndexLists.Add(0);
  352. Trace.WriteLine("输入不对");
  353. MessageBoxEx.Show("输入不对");
  354. return;
  355. }
  356. }
  357. if (PageRangeSelectIndex == CheckPageSelect["1"])
  358. {
  359. char[] enumerationSeparator = new char[] { ',' };
  360. char[] rangeSeparator = new char[] { '-' };
  361. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  362. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  363. { //TODO
  364. Trace.WriteLine("输入不对");
  365. MessageBoxEx.Show("输入不对");
  366. return;
  367. }
  368. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  369. }
  370. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  371. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  372. ConverterPreview.CurrentIndex = 0;
  373. }
  374. }
  375. private void CmbPageTextChangedEvent(object e)
  376. {
  377. var ConverterPreview = e as PageTurningPreview;
  378. if (ConverterPreview != null)
  379. {
  380. if (ConverterPreview != null)
  381. {
  382. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  383. var currentViewer = this.currentViewer;
  384. string PageRange = "";
  385. var PageRangeText = this.PageRangeText;
  386. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex);
  387. char[] enumerationSeparator = new char[] { ',' };
  388. char[] rangeSeparator = new char[] { '-' };
  389. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  390. { //TODO
  391. ConverterPreview.PageIndexLists.Add(0);
  392. return;
  393. }
  394. else
  395. {
  396. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  397. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  398. ConverterPreview.CurrentIndex = 0;
  399. }
  400. }
  401. }
  402. }
  403. private void BatchConverter()
  404. {
  405. DialogParameters convertpdftoword = new DialogParameters();
  406. convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  407. convertpdftoword.Add("ConverterTypeIndex", 7);
  408. HomePageBatchProcessingDialogModel.FilePaths = new List<string> { currentViewer.Document.FilePath.ToString() };
  409. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  410. convertpdftoword.Add(ParameterNames.FilePath, new string[] { currentViewer.Document.FilePath.ToString() });
  411. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  412. }
  413. #endregion
  414. #region 构架行为
  415. public bool CanCloseDialog()
  416. {
  417. return true;
  418. }
  419. public void OnDialogClosed()
  420. {
  421. }
  422. public void OnDialogOpened(IDialogParameters parameters)
  423. {
  424. CPDFViewer pdfViewer = null;
  425. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  426. parameters.TryGetValue<bool>("PageRangeComboBoxCurrentPage", out IsCurrentPageIndex);
  427. if (pdfViewer != null)
  428. {
  429. InitCheckPageSelect();
  430. currentViewer = pdfViewer;
  431. MaxPageRange = currentViewer.Document.PageCount;
  432. if (currentViewer.Tag != null) { ConverterImgModel.Pawssword = currentViewer.Tag.ToString(); }
  433. ConverterImgModel.InputPath = pdfViewer.Document.FilePath;
  434. ImageTypeSelectedIndex = 0;
  435. ImageDPISelectedIndex = 0;
  436. FileInfo fileinfo = new FileInfo(ConverterImgModel.InputPath);
  437. ConverterImgModel.OutputPath = fileinfo.DirectoryName;
  438. }
  439. }
  440. #endregion
  441. }
  442. }