HomePageConverterDialogViewModel.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. using ComDocumentAIKit;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit_Conversion.Converter;
  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.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageConverter;
  12. using Prism.Commands;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using Prism.Services.Dialogs;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Data;
  19. using System.Diagnostics;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Reflection;
  23. using System.Windows;
  24. using System.Windows.Forms;
  25. using DataTable = System.Data.DataTable;
  26. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageConverter
  27. {
  28. public class HomePageConverterDialogViewModel : BindableBase, INavigationAware
  29. {
  30. #region 参数和属性
  31. private List<string> fileNames;
  32. public HomePageConverterDialogModel ConverterDialogModel = new HomePageConverterDialogModel();
  33. public HomePageConverterCSVModel ConverterCSVModel = new HomePageConverterCSVModel();
  34. public HomePageConverterExcelModel ConverterExcelModel = new HomePageConverterExcelModel();
  35. public HomePageConverterHTMLModel ConverterHTMLModel = new HomePageConverterHTMLModel();
  36. public HomePageConverterImgModel ConverterImgModel = new HomePageConverterImgModel();
  37. public HomePageConverterPPTModel ConverterPPTModel = new HomePageConverterPPTModel();
  38. public HomePageConverterRTFModel ConverterRTFModel = new HomePageConverterRTFModel();
  39. public HomePageConverterTextModel ConverterTextModel = new HomePageConverterTextModel();
  40. public HomePageConverterWordModel ConverterWordModel = new HomePageConverterWordModel();
  41. private CPDFDocument tempDocument;
  42. public string PageNumber = "";
  43. public IRegionManager ConverterRegion;
  44. public IDialogService dialogs;
  45. public List<int> fileNamesView = new List<int>();
  46. private int fileNamesIndex = 0;
  47. private int FileNameNumber = 0;
  48. public List<string> ConverterTypeFamily { set; get; } = new List<string>();
  49. private void SetImageTypeOrigin()
  50. {
  51. this.ConverterTypeFamily.Add("Word");
  52. this.ConverterTypeFamily.Add("Excel");
  53. this.ConverterTypeFamily.Add("PPT");
  54. this.ConverterTypeFamily.Add("RTF");
  55. this.ConverterTypeFamily.Add("CSV");
  56. this.ConverterTypeFamily.Add("HTML");
  57. this.ConverterTypeFamily.Add("Text");
  58. this.ConverterTypeFamily.Add("Img");
  59. }
  60. private string converterGridIsEnabled = "True";
  61. public string ConverterGridIsEnabled
  62. {
  63. get
  64. {
  65. return converterGridIsEnabled;
  66. }
  67. set
  68. {
  69. SetProperty(ref converterGridIsEnabled, value);
  70. }
  71. }
  72. private string setConverterGridIsEnabled = "True";
  73. public string SetConverterGridIsEnabled
  74. {
  75. get
  76. {
  77. return setConverterGridIsEnabled;
  78. }
  79. set
  80. {
  81. SetProperty(ref setConverterGridIsEnabled, value);
  82. }
  83. }
  84. private DataTable pdfDataTable = new DataTable();
  85. public DataTable PDFDataTable
  86. {
  87. get { return pdfDataTable; }
  88. set
  89. {
  90. SetProperty(ref pdfDataTable, value);
  91. }
  92. }
  93. private DataTable pdfCurrentDataTable = new DataTable();
  94. public DataTable PDFCurrentDataTable
  95. {
  96. get { return pdfCurrentDataTable; }
  97. set
  98. {
  99. SetProperty(ref pdfCurrentDataTable, value);
  100. }
  101. }
  102. private string removeIsEnable = "False";
  103. public string RemoveIsEnable
  104. {
  105. get { return removeIsEnable; }
  106. set
  107. {
  108. SetProperty(ref removeIsEnable, value);
  109. }
  110. }
  111. private string selectFileName = "False";
  112. public string SelectFileName
  113. {
  114. get { return selectFileName; }
  115. set
  116. {
  117. SetProperty(ref selectFileName, value);
  118. }
  119. }
  120. private int converterTypeIndex = 0;
  121. public int ConverterTypeIndex
  122. {
  123. get { return converterTypeIndex; }
  124. set
  125. {
  126. SetProperty(ref converterTypeIndex, value);
  127. }
  128. }
  129. private string converterRegionName;
  130. public string ConverterRegionName
  131. {
  132. get
  133. {
  134. return converterRegionName;
  135. }
  136. set
  137. {
  138. SetProperty(ref converterRegionName, value);
  139. }
  140. }
  141. private Visibility converterVisible = Visibility.Collapsed;
  142. public Visibility ConverterVisible
  143. {
  144. get { return converterVisible; }
  145. set { SetProperty(ref converterVisible, value); }
  146. }
  147. private Visibility addFileVisibility = Visibility.Hidden;
  148. public Visibility AddFileVisibility
  149. {
  150. get { return addFileVisibility; }
  151. set
  152. {
  153. SetProperty(ref addFileVisibility, value);
  154. RaisePropertyChanged();
  155. }
  156. }
  157. private Visibility oCRVisibility = Visibility.Visible;
  158. public Visibility OCRVisibility
  159. {
  160. get { return oCRVisibility; }
  161. set
  162. {
  163. SetProperty(ref oCRVisibility, value);
  164. RaisePropertyChanged();
  165. }
  166. }
  167. private bool oCRCheckBoxIsCheckBox = false;
  168. public bool OCRCheckBoxIsCheckBox
  169. {
  170. get { return oCRCheckBoxIsCheckBox; }
  171. set
  172. {
  173. SetProperty(ref oCRCheckBoxIsCheckBox, value);
  174. ConverterDialogModel.Options.IsAllowOCR = oCRCheckBoxIsCheckBox;
  175. }
  176. }
  177. private int languageSelectedIndex = 2;
  178. public int LanguageSelectedIndex
  179. {
  180. get { return languageSelectedIndex; }
  181. set
  182. {
  183. SetProperty(ref languageSelectedIndex, value);
  184. SelectLanguage(languageSelectedIndex);
  185. }
  186. }
  187. #endregion
  188. #region 委托声明
  189. public DelegateCommand ADDPDFCommand { get; set; }
  190. public DelegateCommand ADDOpenedPDFCommand { get; set; }
  191. public DelegateCommand RemovePDFFileCommand { get; set; }
  192. public DelegateCommand ConvertCommand { get; set; }
  193. public DelegateCommand ADDPDFFilesCommand { get; set; }
  194. #endregion
  195. public HomePageConverterDialogViewModel(IRegionManager converterRegion, IDialogService dialogs)
  196. {
  197. SetImageTypeOrigin();
  198. PDFCurrentDataTable.Columns.Add("FilePageRangeText");
  199. PDFCurrentDataTable.Columns.Add("FilePageRangeSelectIndex");
  200. PDFCurrentDataTable.Columns.Add("IsEvenPageIsEnabled");
  201. PDFCurrentDataTable.Columns.Add("FileState");
  202. PDFDataTable.Columns.Add("FileName");
  203. PDFDataTable.Columns.Add("FilePageRangeText");
  204. PDFDataTable.Columns.Add("FilePageRangeSelectIndex");
  205. PDFDataTable.Columns.Add("IsEvenPageIsEnabled");
  206. PDFDataTable.Columns.Add("MaxPageRange");
  207. PDFDataTable.Columns.Add("FileSize");
  208. PDFDataTable.Columns.Add("FileState");
  209. ADDPDFCommand = new DelegateCommand(addpicture);
  210. ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles);
  211. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  212. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  213. ConvertCommand = new DelegateCommand(convert);
  214. ConverterRegion = converterRegion;
  215. this.dialogs = dialogs;
  216. ConverterRegionName=Guid.NewGuid().ToString();
  217. }
  218. #region 逻辑函数
  219. private void SetProgress(int pageIndex)
  220. {
  221. PDFDataTable.Rows[fileNamesIndex]["FileState"] = pageIndex.ToString();
  222. pageIndex.ToString();
  223. }
  224. private void SelectLanguage(int SelectedIndex)
  225. {
  226. switch (SelectedIndex)
  227. {
  228. case 0:
  229. ConverterDialogModel.Options.OCRLanguage = COCRLanguage.Chinese;
  230. break;
  231. case 1:
  232. ConverterDialogModel.Options.OCRLanguage = COCRLanguage.ChineseTraditional;
  233. break;
  234. case 2:
  235. ConverterDialogModel.Options.OCRLanguage = COCRLanguage.English;
  236. break;
  237. case 3:
  238. ConverterDialogModel.Options.OCRLanguage = COCRLanguage.Japanese;
  239. break;
  240. case 4:
  241. ConverterDialogModel.Options.OCRLanguage = COCRLanguage.Korean;
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. private async void convert()
  248. {
  249. FolderBrowserDialog dlg = new FolderBrowserDialog();
  250. string saveSelectedPath = "";
  251. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  252. {
  253. saveSelectedPath = dlg.SelectedPath.Trim();
  254. }
  255. else { return; }
  256. HomePageBatchProcessingDialogModel.closeDialog = true;
  257. updateListview("wait");
  258. bool result = false;
  259. OnProgress GetProgress = null;
  260. GetProgress += SetProgress;
  261. fileNamesIndex = 0;
  262. ConverterGridIsEnabled = "False";
  263. foreach (var filename in fileNames)
  264. {
  265. FileInfo fileinfo = new FileInfo(filename);
  266. string OutputPath = saveSelectedPath;
  267. char[] enumerationSeparator = new char[] { ',' };
  268. char[] rangeSeparator = new char[] { '-' };
  269. List<int> PageIndexLists=new List<int>();
  270. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  271. if (document == null )
  272. {
  273. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  274. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  275. fileNamesIndex++;
  276. continue;
  277. }
  278. string DocPassword = "";
  279. if (document.IsLocked)
  280. {
  281. DialogParameters value = new DialogParameters();
  282. value.Add(ParameterNames.PDFDocument, document);
  283. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  284. {
  285. if (e.Result == ButtonResult.OK)
  286. {
  287. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  288. {
  289. DocPassword = e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString();
  290. document.UnlockWithPassword(DocPassword);
  291. }
  292. }
  293. });
  294. if (document.IsLocked)
  295. {
  296. //未成功解密文档时,释放Document对象,返回
  297. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  298. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  299. fileNamesIndex++;
  300. continue;
  301. }
  302. }
  303. string pageRange = getFilePageRange(document, PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeText"].ToString(), PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeSelectIndex"].ToString());
  304. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, pageRange, document.PageCount, enumerationSeparator, rangeSeparator))
  305. { //TODO
  306. MessageBoxEx.Show("输入不对");
  307. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  308. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  309. fileNamesIndex++;
  310. continue;
  311. }
  312. for (int index=0; index< PageIndexLists.Count;index++) {
  313. PageIndexLists[index]++;
  314. }
  315. switch (ConverterTypeIndex)
  316. {
  317. case 0:
  318. if (ConverterWordModel != null)
  319. {
  320. ConverterWordModel.Options.OCRLanguage = ConverterDialogModel.Options.OCRLanguage;
  321. ConverterWordModel.Options.IsAllowOCR = ConverterDialogModel.Options.IsAllowOCR;
  322. PageNumber = ConverterWordModel.PageIndexLists.Count().ToString();
  323. result = await ConverterHelper.WordConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword,ConverterWordModel.WordOptions());
  324. }
  325. break;
  326. case 1:
  327. if (ConverterExcelModel != null)
  328. {
  329. ConverterExcelModel.Options.OCRLanguage = ConverterDialogModel.Options.OCRLanguage;
  330. ConverterExcelModel.Options.IsAllowOCR = ConverterDialogModel.Options.IsAllowOCR;
  331. PageNumber = ConverterExcelModel.PageIndexLists.Count().ToString();
  332. result = await ConverterHelper.ExcelConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword, ConverterExcelModel.ExcelOptions());
  333. }
  334. break;
  335. case 2:
  336. if (ConverterPPTModel != null)
  337. {
  338. ConverterPPTModel.Options.OCRLanguage = ConverterDialogModel.Options.OCRLanguage;
  339. ConverterPPTModel.Options.IsAllowOCR = ConverterDialogModel.Options.IsAllowOCR;
  340. PageNumber = ConverterPPTModel.PageIndexLists.Count().ToString();
  341. result = await ConverterHelper.PPTConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword,ConverterPPTModel.PPTOptions());
  342. }
  343. break;
  344. case 3:
  345. if (ConverterRTFModel != null)
  346. {
  347. ConverterPPTModel.Options.OCRLanguage = ConverterDialogModel.Options.OCRLanguage;
  348. ConverterPPTModel.Options.IsAllowOCR = ConverterDialogModel.Options.IsAllowOCR;
  349. PageNumber = ConverterRTFModel.PageIndexLists.Count().ToString();
  350. result = await ConverterHelper.RTFConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword);
  351. }
  352. break;
  353. case 5:
  354. if (ConverterHTMLModel != null)
  355. {
  356. ConverterHTMLModel.Options.OCRLanguage = ConverterDialogModel.Options.OCRLanguage;
  357. ConverterHTMLModel.Options.IsAllowOCR = ConverterDialogModel.Options.IsAllowOCR;
  358. PageNumber = ConverterHTMLModel.PageIndexLists.Count().ToString();
  359. result = await ConverterHelper.HTMLConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword, ConverterHTMLModel.HTMLOptions());
  360. }
  361. break;
  362. case 6:
  363. if (ConverterTextModel != null)
  364. {
  365. ConverterTextModel.Options.OCRLanguage = ConverterDialogModel.Options.OCRLanguage;
  366. ConverterTextModel.Options.IsAllowOCR = ConverterDialogModel.Options.IsAllowOCR;
  367. PageNumber = ConverterTextModel.PageIndexLists.Count().ToString();
  368. result = await ConverterHelper.TxtConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword, ConverterTextModel.TxtOptions());
  369. }
  370. break;
  371. case 4:
  372. if (ConverterCSVModel != null)
  373. {
  374. PageNumber = ConverterCSVModel.PageIndexLists.Count().ToString();
  375. result = await ConverterHelper.CSVConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword, ConverterCSVModel.CSVOptions());
  376. }
  377. break;
  378. case 7:
  379. if (ConverterImgModel != null)
  380. {
  381. PageNumber = ConverterImgModel.PageIndexLists.Count().ToString();
  382. result = await ConverterHelper.ImgConvert(filename, OutputPath, GetProgress, PageIndexLists, DocPassword, ConverterImgModel.imageType, ConverterImgModel.ImgOptions());
  383. }
  384. break;
  385. default:
  386. break;
  387. }
  388. document.Release();
  389. if (!result) {
  390. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  391. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  392. fileNamesIndex++;
  393. continue;
  394. }
  395. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  396. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  397. fileNamesIndex++;
  398. }
  399. ConverterGridIsEnabled = "True";
  400. HomePageBatchProcessingDialogModel.closeDialog = false;
  401. }
  402. #endregion
  403. #region 批量处理逻辑函数
  404. /// <summary>
  405. /// 添加PDF文件
  406. /// </summary>
  407. private void addpicture()
  408. {
  409. FileNameNumber = fileNames.Count;
  410. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  411. dlg.Multiselect = true;
  412. dlg.Filter = "PDF|*.pdf;*.PDF;";
  413. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  414. {
  415. fileNames.AddRange(dlg.FileNames.ToList());
  416. RemoveExcess(ref fileNames);
  417. SetConverterGridIsEnabled = "True";
  418. AddFileVisibility = Visibility.Collapsed;
  419. updateListview("wait");
  420. }
  421. }
  422. public void addPDFFiles(string filename)
  423. {
  424. FileNameNumber = fileNames.Count;
  425. fileNames.Add(filename);
  426. RemoveExcess(ref fileNames);
  427. SetConverterGridIsEnabled = "True";
  428. AddFileVisibility = Visibility.Collapsed;
  429. updateListview("wait");
  430. }
  431. /// <summary>
  432. /// 添加已打开PDF文件
  433. /// </summary>
  434. public void addOpenedPDFFiles()
  435. {
  436. FileNameNumber = fileNames.Count;
  437. foreach (var filename in App.OpenedFileList)
  438. {
  439. fileNames.Add(filename);
  440. }
  441. RemoveExcess(ref fileNames);
  442. SetConverterGridIsEnabled = "True";
  443. AddFileVisibility = Visibility.Collapsed;
  444. updateListview("wait");
  445. }
  446. /// <summary>
  447. /// 删除重复的文件
  448. /// </summary>
  449. public void RemoveExcess(ref List<string> Filenames)
  450. {
  451. List<string> filenames = new List<string>();
  452. foreach (var fileName in Filenames)
  453. {
  454. if (!filenames.Contains(fileName))
  455. {
  456. filenames.Add(fileName);
  457. }
  458. }
  459. Filenames.Clear();
  460. Filenames = filenames;
  461. }
  462. /// <summary>
  463. /// 添加PDF文件夹
  464. /// </summary>
  465. private void addpicturefiles()
  466. {
  467. FileNameNumber = fileNames.Count;
  468. FolderBrowserDialog dialog = new FolderBrowserDialog();
  469. dialog.Description = "请选择文件路径";
  470. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  471. {
  472. string foldPath = dialog.SelectedPath;
  473. var apps = System.IO.Directory.GetFiles(foldPath);
  474. foreach (string app in apps)
  475. {
  476. var fi = new FileInfo(app);
  477. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  478. {
  479. fileNames.Add(app);
  480. }
  481. }
  482. RemoveExcess(ref fileNames);
  483. updateListview("wait");
  484. SetConverterGridIsEnabled = "True";
  485. AddFileVisibility = Visibility.Collapsed;
  486. }
  487. }
  488. /// <summary>
  489. /// 更新listview显示
  490. /// state 状态显示字符串
  491. /// </summary>
  492. public void updateListview(string state)
  493. {
  494. updateCurrentListview();
  495. DataTable pdfdatatable = new DataTable();
  496. pdfdatatable.Columns.Add("FileName");
  497. pdfdatatable.Columns.Add("FilePageRangeText");
  498. pdfdatatable.Columns.Add("FilePageRangeSelectIndex");
  499. pdfdatatable.Columns.Add("IsEvenPageIsEnabled");
  500. pdfdatatable.Columns.Add("MaxPageRange");
  501. pdfdatatable.Columns.Add("FileSize");
  502. pdfdatatable.Columns.Add("FileState");
  503. int datatableindex = 0;
  504. List<int> PageIndexLists = new List<int>();
  505. foreach (var fileName in fileNames)
  506. {
  507. string file_all = fileName;
  508. FileInfo f = new FileInfo(file_all);
  509. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  510. if (!isEvenPage(fileName) && PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "2")
  511. {
  512. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  513. }
  514. int pagecount = CPDFDocument.InitWithFilePath(fileName).PageCount;
  515. if (PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "3" && !CommonHelper.GetPagesInRange(ref PageIndexLists, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"].ToString(), pagecount, new char[] { ',' }, new char[] { '-' }, true))
  516. {
  517. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  518. }
  519. pdfdatatable.Rows.Add(f.Name, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"], PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"], isEvenPage(fileName), pagecount, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  520. datatableindex++;
  521. }
  522. PDFDataTable = pdfdatatable;
  523. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  524. }
  525. /// <summary>
  526. /// 更新Currentlistview显示
  527. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  528. /// </summary>
  529. public void updateCurrentListview(string pagerangetext = "1", string pagerangeselectindex = "0", bool isevenpageisenabled = true, string state = "wait")
  530. {
  531. if (fileNames.Count >= FileNameNumber)
  532. {
  533. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  534. {
  535. PDFCurrentDataTable.Rows.Add(pagerangetext, pagerangeselectindex, isevenpageisenabled, state);
  536. }
  537. }
  538. else
  539. {
  540. Reverseorder(ref fileNamesView);
  541. foreach (int filenamesview in fileNamesView)
  542. {
  543. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  544. }
  545. }
  546. }
  547. /// <summary>
  548. /// 更新Currentlistview显示
  549. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  550. /// </summary>
  551. public void updateFilesPageRange(string pagerangetext = "1", string pagerangeselectindex = "0")
  552. {
  553. for (int i = 0; fileNames.Count > i; i++)
  554. {
  555. PDFDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  556. PDFDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  557. PDFCurrentDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  558. PDFCurrentDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  559. }
  560. }
  561. /// <summary>
  562. /// 获取文件列表单个文件对应的页码范围
  563. /// document 文档对象 pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  564. /// </summary>
  565. public string getFilePageRange(CPDFDocument document, string pagerangetext = "1", string pagerangeselectindex = "0")
  566. {
  567. string filePageRange = "1";
  568. EditToolsHelper.GetPageRange(int.Parse(pagerangeselectindex), document, ref filePageRange, pagerangetext,true);
  569. return filePageRange;
  570. }
  571. /// <summary>
  572. /// 判断是否关闭偶数页
  573. /// document 文档对象 )
  574. /// </summary>
  575. public bool isEvenPage(string filename)
  576. {
  577. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  578. bool isevenpage = false;
  579. if (document.PageCount > 1)
  580. {
  581. isevenpage = true;
  582. }
  583. document.Release();
  584. return isevenpage;
  585. }
  586. /// <summary>
  587. /// 逆序int类型集合
  588. /// </summary>
  589. public void Reverseorder(ref List<int> Numbers)
  590. {
  591. Numbers = Numbers.OrderBy(a => a).ToList();
  592. Numbers.Reverse();
  593. }
  594. /// <summary>
  595. /// 打开文件PDF
  596. /// </summary>
  597. public void openfiledialog()
  598. {
  599. foreach (int filenamesview in fileNamesView)
  600. {
  601. System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
  602. }
  603. }
  604. /// <summary>
  605. /// 删除文件PDF
  606. /// </summary>
  607. public void removepdffile()
  608. {
  609. FileNameNumber = fileNames.Count;
  610. Reverseorder(ref fileNamesView);
  611. foreach (int filenamesview in fileNamesView)
  612. {
  613. //Trace.WriteLine(filenamesview);
  614. fileNames.Remove(fileNames[filenamesview]);
  615. }
  616. if (fileNames.Count < 1)
  617. {
  618. SetConverterGridIsEnabled = "False";
  619. AddFileVisibility = Visibility.Visible;
  620. }
  621. updateListview("wait");
  622. }
  623. public void removepdffile(int index)
  624. {
  625. PDFCurrentDataTable.Rows.RemoveAt(index);
  626. fileNames.Remove(fileNames[index]);
  627. if (fileNames.Count < 1)
  628. {
  629. SetConverterGridIsEnabled = "False";
  630. AddFileVisibility = Visibility.Visible;
  631. }
  632. updateListview("wait");
  633. }
  634. public void PDFFileCount()
  635. {
  636. if (fileNames.Count == 0)
  637. {
  638. SetConverterGridIsEnabled = "False";
  639. AddFileVisibility = Visibility.Visible;
  640. }
  641. else
  642. {
  643. SetConverterGridIsEnabled = "True";
  644. AddFileVisibility = Visibility.Collapsed;
  645. }
  646. }
  647. public void ChangeConverter(int index)
  648. {
  649. ConverterTypeIndex=index;
  650. NavigationParameters param = new NavigationParameters();
  651. param.Add(ParameterNames.ViewContentViewModel, this);
  652. if (index == 7 || index == 4)
  653. {
  654. OCRVisibility = Visibility.Hidden;
  655. }
  656. else {
  657. OCRVisibility = Visibility.Visible;
  658. }
  659. if (index == 0 || index == 1 || index == 4 || index == 7)
  660. {
  661. ConverterVisible = Visibility.Collapsed;
  662. ConverterRegion.RequestNavigate(ConverterRegionName, HomePageConverterDialogModel.GetConverter[index], param);
  663. ConverterVisible = Visibility.Visible;
  664. }
  665. else { ConverterVisible = Visibility.Collapsed; }
  666. }
  667. #endregion
  668. #region 构架行为
  669. public void OnNavigatedTo(NavigationContext navigationContext)
  670. {
  671. List<string> filepath = new List<string>();
  672. int convertertypeindex=0;
  673. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  674. navigationContext.Parameters.TryGetValue<int>("ConverterTypeIndex", out convertertypeindex);
  675. if (filepath != null)
  676. {
  677. fileNames = filepath;
  678. PDFFileCount();
  679. updateListview("wait");
  680. ChangeConverter(convertertypeindex);
  681. }
  682. }
  683. public bool IsNavigationTarget(NavigationContext navigationContext)
  684. {
  685. return true;
  686. }
  687. public void OnNavigatedFrom(NavigationContext navigationContext)
  688. {
  689. ConverterHelper.Clear();
  690. }
  691. #endregion
  692. }
  693. }