ConverterCSVDialogViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit_Conversion.Options;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Office.Interop.Excel;
  5. using Microsoft.Office.Interop.Word;
  6. using PDF_Master.CustomControl;
  7. using PDF_Master.Helper;
  8. using PDF_Master.Model;
  9. using PDF_Master.Model.Dialog.ConverterDialogs;
  10. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  11. using PDF_Master.Properties;
  12. using Prism.Commands;
  13. using Prism.Mvvm;
  14. using Prism.Services.Dialogs;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Forms;
  23. using static Dropbox.Api.Files.FileCategory;
  24. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  25. using DialogResult = Prism.Services.Dialogs.DialogResult;
  26. namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
  27. {
  28. public class ConverterCSVDialogViewModel : BindableBase, IDialogAware
  29. {
  30. #region 文案
  31. private string T_title;
  32. public string T_Title
  33. {
  34. get { return T_title; }
  35. set
  36. {
  37. SetProperty(ref T_title, value);
  38. }
  39. }
  40. private string T_yes;
  41. public string T_YES
  42. {
  43. get { return T_yes; }
  44. set
  45. {
  46. SetProperty(ref T_yes, value);
  47. }
  48. }
  49. private string T_no;
  50. public string T_No
  51. {
  52. get { return T_no; }
  53. set
  54. {
  55. SetProperty(ref T_no, value);
  56. }
  57. }
  58. private string T_eachForm;
  59. public string T_EachForm
  60. {
  61. get { return T_eachForm; }
  62. set
  63. {
  64. SetProperty(ref T_eachForm, value);
  65. }
  66. }
  67. private string T_aDocument;
  68. public string T_ADocument
  69. {
  70. get { return T_aDocument; }
  71. set
  72. {
  73. SetProperty(ref T_aDocument, value);
  74. }
  75. }
  76. private string T_settings;
  77. public string T_Settings
  78. {
  79. get { return T_settings; }
  80. set
  81. {
  82. SetProperty(ref T_settings, value);
  83. }
  84. }
  85. private string T_pageRange;
  86. public string T_PageRange
  87. {
  88. get { return T_pageRange; }
  89. set
  90. {
  91. SetProperty(ref T_pageRange, value);
  92. }
  93. }
  94. private string _TextUpgrade;
  95. public string TextUpgrade
  96. {
  97. get { return _TextUpgrade; }
  98. set { _TextUpgrade = value; }
  99. }
  100. private void IntString()
  101. {
  102. TextUpgrade = App.ServiceLoader.GetString("TextUpgrade");
  103. T_Title = App.MainPageLoader.GetString("Convert_PDFToCSVTitle");
  104. T_YES = App.MainPageLoader.GetString("Convert_Yes");
  105. T_No = App.MainPageLoader.GetString("Convert_No");
  106. T_EachForm = App.MainPageLoader.GetString("Convert_PDFToCSVEachForm");
  107. T_ADocument = App.MainPageLoader.GetString("Convert_PDFToCSVADocment");
  108. T_Settings = App.MainPageLoader.GetString("Convert_PDFToCSVSettings");
  109. T_PageRange = App.MainPageLoader.GetString("Convert_PDFPageRange");
  110. }
  111. #endregion
  112. public string Title => "";
  113. public event Action<IDialogResult> RequestClose;
  114. #region 参数和属性
  115. private Visibility limitationsConvertVisible = Visibility.Hidden;
  116. public Visibility LimitationsConvertVisible
  117. {
  118. get { return limitationsConvertVisible; }
  119. set
  120. {
  121. SetProperty(ref limitationsConvertVisible, value);
  122. }
  123. }
  124. public ConverterCSVDialogModel ConverterCSVModel = new ConverterCSVDialogModel();
  125. public CPDFViewer currentViewer;
  126. public IDialogService dialogs;
  127. public string CurrentPageIndex = "1";
  128. public string PageRangeText { set; get; } = "0";
  129. public string PageRangeSelectIndex { set; get; } = "0";
  130. public bool IsCurrentPageIndex = true;
  131. private int maxPageRange = 0;
  132. public int MaxPageRange
  133. {
  134. get { return maxPageRange; }
  135. set
  136. {
  137. SetProperty(ref maxPageRange, value);
  138. }
  139. }
  140. #endregion
  141. #region 委托声明
  142. public DelegateCommand<string> RadioButtonCommand { get; set; }
  143. public DelegateCommand CancelCommand { get; set; }
  144. public DelegateCommand ConverterCommnad { get; set; }
  145. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  146. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  147. public DelegateCommand SetCustomPageRangeCommand { get; set; }
  148. public DelegateCommand<object> PreviewCancelDownCommand { get; set; }
  149. public DelegateCommand BatchConverterCommand { get; set; }
  150. public DelegateCommand UnlockMouseDownCommand { get; set; }
  151. #endregion
  152. public ConverterCSVDialogViewModel(IDialogService dialogService)
  153. {
  154. IntString();
  155. CancelCommand = new DelegateCommand(cancel);
  156. ConverterCommnad = new DelegateCommand(converter);
  157. RadioButtonCommand = new DelegateCommand<string>(radiobutton);
  158. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  159. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  160. SetCustomPageRangeCommand = new DelegateCommand(SetCustomPageRange);
  161. PreviewCancelDownCommand = new DelegateCommand<object>(PreviewCancelDown);
  162. BatchConverterCommand = new DelegateCommand(BatchConverter);
  163. UnlockMouseDownCommand = new DelegateCommand(UnlockMouseDown);
  164. dialogs = dialogService;
  165. }
  166. #region 逻辑函数
  167. public void SetCustomPageRange()
  168. {
  169. if (PageRangeSelectIndex == "4")
  170. {
  171. List<int> PageIndexLists = new List<int>();
  172. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  173. { //TODO
  174. AlertsMessage alertsMessage = new AlertsMessage();
  175. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  176. if (alertsMessage.result == ContentResult.Ok)
  177. {
  178. return;
  179. }
  180. else
  181. {
  182. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  183. //{
  184. // Unicode = Unicode,
  185. // Status = EnumDelete.StatusCreate
  186. //});
  187. return;
  188. }
  189. }
  190. }
  191. }
  192. private void CmbPageSelectionChangedEvent(object e)
  193. {
  194. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  195. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  196. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  197. var ConverterPreview = e as PageTurningPreview;
  198. if (ConverterPreview != null)
  199. {
  200. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  201. var currentViewer = this.currentViewer;
  202. string PageRange = "";
  203. var PageRangeText = this.PageRangeText;
  204. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3" || PageRangeSelectIndex == "4")
  205. {
  206. if (PageRangeSelectIndex == "4") { HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, true); } else { HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true); }
  207. char[] enumerationSeparator = new char[] { ',' };
  208. char[] rangeSeparator = new char[] { '-' };
  209. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  210. { //TODO
  211. ConverterPreview.PageIndexLists.Add(0);
  212. Trace.WriteLine("输入不对");
  213. AlertsMessage alertsMessage = new AlertsMessage();
  214. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  215. return;
  216. }
  217. }
  218. if (PageRangeSelectIndex == "1")
  219. {
  220. char[] enumerationSeparator = new char[] { ',' };
  221. char[] rangeSeparator = new char[] { '-' };
  222. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  223. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  224. { //TODO
  225. Trace.WriteLine("输入不对");
  226. AlertsMessage alertsMessage = new AlertsMessage();
  227. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  228. return;
  229. }
  230. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  231. }
  232. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  233. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  234. ConverterPreview.CurrentIndex = 0;
  235. }
  236. }
  237. private void CmbPageTextChangedEvent(object e)
  238. {
  239. var ConverterPreview = e as PageTurningPreview;
  240. if (ConverterPreview != null)
  241. {
  242. if (ConverterPreview != null)
  243. {
  244. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  245. var currentViewer = this.currentViewer;
  246. string PageRange = "";
  247. var PageRangeText = this.PageRangeText;
  248. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  249. char[] enumerationSeparator = new char[] { ',' };
  250. char[] rangeSeparator = new char[] { '-' };
  251. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  252. { //TODO
  253. HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, true);
  254. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  255. { //TODO
  256. ConverterPreview.PageIndexLists.Add(0);
  257. Trace.WriteLine("输入不对");
  258. AlertsMessage alertsMessage = new AlertsMessage();
  259. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  260. return;
  261. }
  262. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  263. ConverterPreview.CurrentIndex = 0;
  264. return;
  265. }
  266. else
  267. {
  268. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  269. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  270. ConverterPreview.CurrentIndex = 0;
  271. }
  272. }
  273. }
  274. }
  275. private void BatchConverter()
  276. {
  277. DialogParameters convertpdftoword = new DialogParameters();
  278. convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  279. convertpdftoword.Add("ConverterTypeIndex", 4);
  280. HomePageBatchProcessingDialogModel.FilePaths = new List<string> { currentViewer.Document.FilePath.ToString() };
  281. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  282. convertpdftoword.Add(ParameterNames.FilePath, new string[] { currentViewer.Document.FilePath.ToString() });
  283. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  284. }
  285. private void UnlockMouseDown()
  286. {
  287. ConverterHelper.convertUnlock();
  288. }
  289. private void cancel()
  290. {
  291. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  292. }
  293. private void PreviewCancelDown(object e)
  294. {
  295. var PageRangeComboBox = e as WritableComboBox;
  296. if (PageRangeComboBox != null)
  297. {
  298. PageRangeComboBox.IsloseFocus = false;
  299. }
  300. }
  301. private void converter()
  302. {
  303. if (PageRangeSelectIndex == "4")
  304. {
  305. List<int> PageIndexLists = new List<int>();
  306. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  307. { //TODO
  308. //AlertsMessage alertsMessage = new AlertsMessage();
  309. //alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  310. //if (alertsMessage.result == ContentResult.Ok)
  311. //{
  312. // return;
  313. //}
  314. //else
  315. //{
  316. // //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  317. // //{
  318. // // Unicode = Unicode,
  319. // // Status = EnumDelete.StatusCreate
  320. // //});
  321. // return;
  322. //}
  323. }
  324. }
  325. if (ConverterCSVModel.IsCustomFileName)
  326. {
  327. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  328. /*
  329. *设置这个对话框的起始保存路径
  330. */
  331. sfd.InitialDirectory = currentViewer.Document.FilePath;
  332. /*
  333. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  334. */
  335. sfd.Filter = "CSV|*.csv;";
  336. /*
  337. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  338. */
  339. sfd.FileName = currentViewer.Document.FileName + ".csv";
  340. /*
  341. * 做一些工作
  342. */
  343. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  344. {
  345. ConverterCSVModel.OutputPath = sfd.FileName;
  346. try { if (File.Exists(ConverterCSVModel.OutputPath)) File.Delete(ConverterCSVModel.OutputPath); }
  347. catch
  348. {
  349. AlertsMessage alertsMessage = new AlertsMessage();
  350. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
  351. return;
  352. }
  353. }
  354. else
  355. {
  356. return;
  357. }
  358. }
  359. else
  360. {
  361. FolderBrowserDialog dlg = new FolderBrowserDialog();
  362. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  363. {
  364. ConverterCSVModel.OutputPath = dlg.SelectedPath.Trim();
  365. }
  366. else { return; }
  367. }
  368. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterCSVModel.PageRange, PageRangeText, true, CurrentPageIndex);
  369. if (ConverterCSVModel.PageRange == "")
  370. {
  371. Trace.WriteLine("输入不对");
  372. AlertsMessage alertsMessage = new AlertsMessage();
  373. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  374. return;
  375. }
  376. char[] enumerationSeparator = new char[] { ',' };
  377. char[] rangeSeparator = new char[] { '-' };
  378. if (!CommonHelper.GetPagesInRange(ref ConverterCSVModel.PageIndexLists, ConverterCSVModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  379. { //TODO
  380. Trace.WriteLine("输入不对");
  381. AlertsMessage alertsMessage = new AlertsMessage();
  382. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  383. return;
  384. }
  385. ConverterCSVModel.csvOptions = ConverterCSVModel.CSVOptions();
  386. //DialogParameters value = new DialogParameters();
  387. //value.Add(ParameterNames.ConverterType,"CSV");
  388. //value.Add(ParameterNames.ConverterTypeModel, ConverterCSVModel);
  389. var dialogresult = new DialogResult(ButtonResult.OK);
  390. dialogresult.Parameters.Add(ParameterNames.ConverterType, "CSV");
  391. dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterCSVModel);
  392. RequestClose.Invoke(dialogresult);
  393. //dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  394. //{
  395. //});
  396. }
  397. private void radiobutton(string e)
  398. {
  399. string radioButton = e;
  400. if (radioButton != null)
  401. {
  402. switch (radioButton)
  403. {
  404. case "ForEachPageRadioBtn":
  405. ConverterCSVModel.Options.IsMergeCsv = false;
  406. ConverterCSVModel.IsCustomFileName = false;
  407. break;
  408. case "OnlyTableRadioBtn":
  409. ConverterCSVModel.Options.IsMergeCsv = true;
  410. ConverterCSVModel.IsCustomFileName = true;
  411. break;
  412. default:
  413. break;
  414. }
  415. }
  416. }
  417. #endregion
  418. #region 构架行为
  419. public bool CanCloseDialog()
  420. {
  421. return true;
  422. }
  423. public void OnDialogClosed()
  424. {
  425. }
  426. public void OnDialogOpened(IDialogParameters parameters)
  427. {
  428. CPDFViewer pdfViewer = null;
  429. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  430. if (pdfViewer != null)
  431. {
  432. currentViewer = pdfViewer;
  433. MaxPageRange = currentViewer.Document.PageCount;
  434. if (currentViewer.Tag != null) { ConverterCSVModel.Pawssword = currentViewer.Tag.ToString(); }
  435. ConverterCSVModel.InputPath = pdfViewer.Document.FilePath;
  436. FileInfo fileinfo = new FileInfo(ConverterCSVModel.InputPath);
  437. ConverterCSVModel.Options.IsMergeCsv = false;
  438. ConverterCSVModel.IsCustomFileName = false;
  439. ConverterCSVModel.OutputPath = fileinfo.DirectoryName;
  440. if (Settings.Default.UserDate.subscribestatus != 1)
  441. {
  442. LimitationsConvertVisible = Visibility.Visible;
  443. }
  444. }
  445. }
  446. #endregion
  447. }
  448. }