HomePageConverterDialogViewModel.cs 31 KB

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