HomePageWatermarkDialogViewModel.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.CustomControl;
  3. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  4. using PDF_Master.Model;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Windows.Forms;
  14. using PDF_Master.Helper;
  15. using System.Windows;
  16. using System.Data;
  17. using ComPDFKitViewer.PdfViewer;
  18. using Prism.Events;
  19. using PDF_Master.EventAggregators;
  20. using PDFSettings;
  21. using PDF_Master.Model.EditTools.Watermark;
  22. using ComPDFKit.PDFWatermark;
  23. using Prism.Services.Dialogs;
  24. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageWatermark
  25. {
  26. public class HomePageWatermarkDialogViewModel : BindableBase, INavigationAware
  27. {
  28. #region 参数和属性
  29. private List<string> fileNames;
  30. public string PageNumber = "";
  31. public IEventAggregator eventAggregator;
  32. public IRegionManager watermarkRegion;
  33. public IDialogService dialogs;
  34. private CPDFViewer PDFViewer;
  35. private CPDFWatermark watermark;
  36. public WatermarkInfo WatermarkInfo;
  37. private CPDFViewer CurrentPDFViewer;
  38. private ViewContentViewModel viewContentViewModel;
  39. public string TemplateListName = "HomePageWatermarkTemplateListBaseContent";
  40. public string CreateBaseName = "HomePageWatermarkCreateBaseContent";
  41. public string CreateModTextName = "HomePageWatermarkCreateTextContent";
  42. public string CreateModFileName = "HomePageWatermarkCreateFileContent";
  43. public string TemplateListModTextName = "HomePageWatermarkTemplateListTextContent";
  44. public string TemplateListModFileName = "HomePageWatermarkTemplateListFileContent";
  45. private string _watermarkSettingsRegionName;
  46. public string WatermarkSettingsRegionName
  47. {
  48. get { return _watermarkSettingsRegionName; }
  49. set { _watermarkSettingsRegionName = value; }
  50. }
  51. private Visibility _watermarkSettingsVisible = Visibility.Collapsed;
  52. public Visibility WatermarkSettingsVisible
  53. {
  54. get { return _watermarkSettingsVisible; }
  55. set { _watermarkSettingsVisible = value; }
  56. }
  57. private EnumTextOrFile _currentCreateMod;
  58. public EnumTextOrFile CurrentCreateMod
  59. {
  60. get { return _currentCreateMod; }
  61. set { _currentCreateMod = value; }
  62. }
  63. private EnumTextOrFile _currentTemplateListMod;
  64. public EnumTextOrFile CurrentTemplateListMod
  65. {
  66. get { return _currentTemplateListMod; }
  67. set { _currentTemplateListMod = value; }
  68. }
  69. public List<int> fileNamesView = new List<int>();
  70. private int fileNamesIndex = 0;
  71. private int FileNameNumber = 0;
  72. private string watermarkGridIsEnabled = "True";
  73. public string WatermarkGridIsEnabled
  74. {
  75. get
  76. {
  77. return watermarkGridIsEnabled;
  78. }
  79. set
  80. {
  81. SetProperty(ref watermarkGridIsEnabled, value);
  82. }
  83. }
  84. private string setWatermarkGridIsEnabled = "True";
  85. public string SetWatermarkGridIsEnabled
  86. {
  87. get
  88. {
  89. return setWatermarkGridIsEnabled;
  90. }
  91. set
  92. {
  93. SetProperty(ref setWatermarkGridIsEnabled, value);
  94. }
  95. }
  96. private DataTable pdfDataTable = new DataTable();
  97. public DataTable PDFDataTable
  98. {
  99. get { return pdfDataTable; }
  100. set
  101. {
  102. SetProperty(ref pdfDataTable, value);
  103. }
  104. }
  105. private DataTable pdfCurrentDataTable = new DataTable();
  106. public DataTable PDFCurrentDataTable
  107. {
  108. get { return pdfCurrentDataTable; }
  109. set
  110. {
  111. SetProperty(ref pdfCurrentDataTable, value);
  112. }
  113. }
  114. private string removeIsEnable = "False";
  115. public string RemoveIsEnable
  116. {
  117. get { return removeIsEnable; }
  118. set
  119. {
  120. SetProperty(ref removeIsEnable, value);
  121. }
  122. }
  123. private string selectFileName = "False";
  124. public string SelectFileName
  125. {
  126. get { return selectFileName; }
  127. set
  128. {
  129. SetProperty(ref selectFileName, value);
  130. }
  131. }
  132. private string watermarkRegionName;
  133. public string WatermarkRegionName
  134. {
  135. get
  136. {
  137. return watermarkRegionName;
  138. }
  139. set
  140. {
  141. SetProperty(ref watermarkRegionName, value);
  142. }
  143. }
  144. private Visibility watermarkVisible = Visibility.Collapsed;
  145. public Visibility WatermarkVisible
  146. {
  147. get { return watermarkVisible; }
  148. set { SetProperty(ref watermarkVisible, value); }
  149. }
  150. private Visibility addFileVisibility = Visibility.Hidden;
  151. public Visibility AddFileVisibility
  152. {
  153. get { return addFileVisibility; }
  154. set
  155. {
  156. SetProperty(ref addFileVisibility, value);
  157. RaisePropertyChanged();
  158. }
  159. }
  160. private string createGridSpan = "1";
  161. public string CreateGridSpan
  162. {
  163. get { return createGridSpan; }
  164. set
  165. {
  166. SetProperty(ref createGridSpan, value);
  167. RaisePropertyChanged();
  168. }
  169. }
  170. private Visibility createGridSpanVisibility = Visibility.Visible;
  171. public Visibility CreateGridSpanVisibility
  172. {
  173. get { return createGridSpanVisibility; }
  174. set
  175. {
  176. SetProperty(ref createGridSpanVisibility, value);
  177. RaisePropertyChanged();
  178. }
  179. }
  180. private bool _createBtnIsEnabled = false;
  181. public bool CreateBtnIsEnabled
  182. {
  183. get { return _createBtnIsEnabled; }
  184. set { SetProperty(ref _createBtnIsEnabled, value); }
  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 CreateCommand { get; set; }
  192. public DelegateCommand ADDPDFFilesCommand { get; set; }
  193. #endregion
  194. public string Unicode = null;
  195. public HomePageWatermarkDialogViewModel(IRegionManager watermarkRegion, IEventAggregator eventAggregator, IDialogService dialogs)
  196. {
  197. this.eventAggregator = eventAggregator;
  198. this.watermarkRegion = watermarkRegion;
  199. this.dialogs = dialogs;
  200. Unicode = Guid.NewGuid().ToString();
  201. WatermarkSettingsVisible = Visibility.Visible;
  202. PDFCurrentDataTable.Columns.Add("FilePageRangeText");
  203. PDFCurrentDataTable.Columns.Add("FilePageRangeSelectIndex");
  204. PDFCurrentDataTable.Columns.Add("IsEvenPageIsEnabled");
  205. PDFCurrentDataTable.Columns.Add("FileState");
  206. PDFDataTable.Columns.Add("FileName");
  207. PDFDataTable.Columns.Add("FilePageRangeText");
  208. PDFDataTable.Columns.Add("FilePageRangeSelectIndex");
  209. PDFDataTable.Columns.Add("IsEvenPageIsEnabled");
  210. PDFDataTable.Columns.Add("MaxPageRange");
  211. PDFDataTable.Columns.Add("FileSize");
  212. PDFDataTable.Columns.Add("FileState");
  213. ADDPDFCommand = new DelegateCommand(addpicture);
  214. ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles);
  215. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  216. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  217. CreateCommand = new DelegateCommand(create);
  218. WatermarkSettingsRegionName = Guid.NewGuid().ToString();
  219. eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Subscribe(EnterTemplateListOrCreate, e => e.Unicode == Unicode);
  220. eventAggregator.GetEvent<SetCurrentCreateModEvent>().Subscribe(SetCurrentCreateMod, e => e.Unicode == Unicode);
  221. eventAggregator.GetEvent<SetCurrentTemplateListModEvent>().Subscribe(SetCurrentTemplateListMod, e => e.Unicode == Unicode);
  222. eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
  223. eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Subscribe(EditWatermarkTemplateItem, e => e.Unicode == Unicode);
  224. }
  225. #region 逻辑函数
  226. public void EnterTemplateListOrCreate(EnumTemplateListOrCreateUnicode enumTemplateListOrCreateunicode)
  227. {
  228. EnumTemplateListOrCreate enumTemplateListOrCreate = enumTemplateListOrCreateunicode.Status;
  229. if (enumTemplateListOrCreate == EnumTemplateListOrCreate.StatusTemplate)
  230. {
  231. CreateGridSpan = "1";
  232. CreateGridSpanVisibility = Visibility.Visible;
  233. EnterSelectedContent(TemplateListName);
  234. }
  235. else
  236. {
  237. CreateGridSpan = "2";
  238. CreateGridSpanVisibility = Visibility.Collapsed;
  239. EnterSelectedContent(CreateBaseName);
  240. }
  241. }
  242. public void EnterSelectedContent(string SelectedContentName)
  243. {
  244. NavigationParameters param = new NavigationParameters();
  245. param.Add(ParameterNames.PDFViewer, PDFViewer);
  246. param.Add("Unicode", Unicode);
  247. param.Add("ViewModel", this);
  248. if (SelectedContentName == TemplateListName)
  249. {
  250. param.Add("CurrentCreateModName", CurrentCreateMod);
  251. }
  252. else if (SelectedContentName == CreateBaseName)
  253. {
  254. param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
  255. }
  256. watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, SelectedContentName, param);
  257. WatermarkSettingsVisible = Visibility.Visible;
  258. }
  259. public void SetCurrentCreateMod(stringUnicode currentCreateModNameunicode)
  260. {
  261. string currentCreateModName = currentCreateModNameunicode.Status;
  262. if (currentCreateModName == CreateModTextName)
  263. {
  264. CurrentCreateMod = EnumTextOrFile.StatusText;
  265. }
  266. else if (currentCreateModName == CreateModFileName)
  267. {
  268. CurrentCreateMod = EnumTextOrFile.StatusFile;
  269. }
  270. }
  271. public void SetCurrentTemplateListMod(stringUnicode currentTemplateListModNameunicode)
  272. {
  273. string currentTemplateListModName = currentTemplateListModNameunicode.Status;
  274. if (currentTemplateListModName == TemplateListModTextName)
  275. {
  276. CurrentTemplateListMod = EnumTextOrFile.StatusText;
  277. }
  278. else if (currentTemplateListModName == TemplateListModFileName)
  279. {
  280. CurrentTemplateListMod = EnumTextOrFile.StatusFile;
  281. }
  282. }
  283. public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicod)
  284. {
  285. CurrentPDFViewer = cPDFViewerunicod.Status;
  286. }
  287. public void EditWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
  288. {
  289. WatermarkItem watermarkItem = watermarkItemunicode.Status;
  290. NavigationParameters param = new NavigationParameters();
  291. param.Add("Unicode", Unicode);
  292. param.Add(ParameterNames.PDFViewer, PDFViewer);
  293. param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
  294. param.Add("WatermarkItem", watermarkItem);
  295. watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, CreateBaseName, param);
  296. WatermarkSettingsVisible = Visibility.Visible;
  297. CreateGridSpan = "2";
  298. CreateGridSpanVisibility = Visibility.Collapsed;
  299. }
  300. private void create()
  301. {
  302. FolderBrowserDialog dlg = new FolderBrowserDialog();
  303. string saveSelectedPath = "";
  304. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  305. {
  306. saveSelectedPath = dlg.SelectedPath.Trim();
  307. saveSelectedPath = saveSelectedPath + "\\";
  308. }
  309. else { return; }
  310. HomePageBatchProcessingDialogModel.closeDialog = true;
  311. updateListview("wait");
  312. fileNamesIndex = 0;
  313. WatermarkGridIsEnabled = "False";
  314. foreach (var filename in fileNames)
  315. {
  316. FileInfo fileinfo = new FileInfo(filename);
  317. string OutputPath = fileinfo.DirectoryName;
  318. char[] enumerationSeparator = new char[] { ',' };
  319. char[] rangeSeparator = new char[] { '-' };
  320. List<int> PageIndexLists = new List<int>();
  321. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  322. if (document == null )
  323. {
  324. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  325. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  326. fileNamesIndex++;
  327. continue;
  328. }
  329. if (document.IsLocked)
  330. {
  331. DialogParameters value = new DialogParameters();
  332. value.Add(ParameterNames.PDFDocument, document);
  333. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  334. {
  335. if (e.Result == ButtonResult.OK)
  336. {
  337. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  338. {
  339. document.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  340. }
  341. }
  342. });
  343. if (document.IsLocked)
  344. {
  345. //未成功解密文档时,释放Document对象,返回
  346. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  347. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  348. fileNamesIndex++;
  349. continue;
  350. }
  351. }
  352. CreateWatermark(document);
  353. document.WriteToFilePath(CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "_Watermark.pdf"));
  354. //if (!CommonHelper.GetPagesInRange(ref PageIndexLists, "1-" + document.PageCount.ToString(), document.PageCount, enumerationSeparator, rangeSeparator))
  355. //{ //TODO
  356. // Trace.WriteLine("输入不对");
  357. // MessageBoxEx.Show("输入不对");
  358. // return;
  359. //}
  360. document.Release();
  361. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  362. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  363. fileNamesIndex++;
  364. }
  365. WatermarkGridIsEnabled = "True";
  366. HomePageBatchProcessingDialogModel.closeDialog = false;
  367. }
  368. public void CreateWatermark(CPDFDocument document)
  369. {
  370. if (WatermarkInfo != null)
  371. {
  372. document.DeleteWatermarks();
  373. if (WatermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  374. {
  375. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  376. watermark.SetText(WatermarkInfo.Text);
  377. watermark.SetFontName(WatermarkInfo.FontName);
  378. if (WatermarkInfo.IsRelativeScale)
  379. {
  380. watermark.SetFontSizeWithPercentage(WatermarkInfo.TextSize);
  381. }
  382. else
  383. {
  384. watermark.SetFontSize(WatermarkInfo.TextSize);
  385. }
  386. watermark.SetTextRGBColor(WatermarkInfo.TextColor);
  387. }
  388. else
  389. {
  390. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  391. if (WatermarkInfo.ImageArray != null)
  392. {
  393. watermark.SetImage(WatermarkInfo.ImageArray, WatermarkInfo.ImageWidth, WatermarkInfo.ImageHeight);
  394. }
  395. watermark.SetScale(WatermarkInfo.ImageSize / 100);
  396. }
  397. watermark.SetRotation((float)((WatermarkInfo.Rotation / 180) * Math.PI));
  398. watermark.SetOpacity((byte)(((float)WatermarkInfo.Opacity / 100) * 255));
  399. watermark.SetFront(WatermarkInfo.IsFront);
  400. watermark.SetVertalign(WatermarkInfo.WatermarkVertalign);
  401. watermark.SetHorizalign(WatermarkInfo.WatermarkHorizalign);
  402. watermark.SetFullScreen(WatermarkInfo.Isfull);
  403. watermark.SetVertOffset(WatermarkInfo.VertOffset);
  404. watermark.SetHorizOffset(WatermarkInfo.HorizOffset);
  405. watermark.SetHorizontalSpacing(WatermarkInfo.HorizontalSpacing);
  406. watermark.SetVerticalSpacing(WatermarkInfo.VerticalSpacing);
  407. watermark.SetPages(getFilePageRange(document, PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeText"].ToString(), PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeSelectIndex"].ToString()));
  408. Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark());
  409. Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark());
  410. }
  411. }
  412. #endregion
  413. #region 批量处理逻辑函数
  414. /// <summary>
  415. /// 添加PDF文件
  416. /// </summary>
  417. private void addpicture()
  418. {
  419. FileNameNumber = fileNames.Count;
  420. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  421. dlg.Multiselect = true;
  422. dlg.Filter = "PDF|*.pdf;*.PDF;";
  423. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  424. {
  425. fileNames.AddRange(dlg.FileNames.ToList());
  426. RemoveExcess(ref fileNames);
  427. SetWatermarkGridIsEnabled = "True";
  428. AddFileVisibility = Visibility.Collapsed;
  429. updateListview("wait");
  430. }
  431. }
  432. public void addPDFFiles(string filename)
  433. {
  434. FileNameNumber = fileNames.Count;
  435. fileNames.Add(filename);
  436. RemoveExcess(ref fileNames);
  437. SetWatermarkGridIsEnabled = "True";
  438. AddFileVisibility = Visibility.Collapsed;
  439. updateListview("wait");
  440. }
  441. /// <summary>
  442. /// 添加已打开PDF文件
  443. /// </summary>
  444. public void addOpenedPDFFiles()
  445. {
  446. FileNameNumber = fileNames.Count;
  447. foreach (var filename in App.OpenedFileList)
  448. {
  449. fileNames.Add(filename);
  450. }
  451. RemoveExcess(ref fileNames);
  452. SetWatermarkGridIsEnabled = "True";
  453. AddFileVisibility = Visibility.Collapsed;
  454. updateListview("wait");
  455. }
  456. /// <summary>
  457. /// 删除重复的文件
  458. /// </summary>
  459. public void RemoveExcess(ref List<string> Filenames)
  460. {
  461. List<string> filenames = new List<string>();
  462. foreach (var fileName in Filenames)
  463. {
  464. if (!filenames.Contains(fileName))
  465. {
  466. filenames.Add(fileName);
  467. }
  468. }
  469. Filenames.Clear();
  470. Filenames = filenames;
  471. }
  472. /// <summary>
  473. /// 添加PDF文件夹
  474. /// </summary>
  475. private void addpicturefiles()
  476. {
  477. FileNameNumber = fileNames.Count;
  478. FolderBrowserDialog dialog = new FolderBrowserDialog();
  479. dialog.Description = "请选择文件路径";
  480. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  481. {
  482. string foldPath = dialog.SelectedPath;
  483. var apps = System.IO.Directory.GetFiles(foldPath);
  484. foreach (string app in apps)
  485. {
  486. var fi = new FileInfo(app);
  487. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  488. {
  489. fileNames.Add(app);
  490. }
  491. }
  492. RemoveExcess(ref fileNames);
  493. updateListview("wait");
  494. SetWatermarkGridIsEnabled = "True";
  495. AddFileVisibility = Visibility.Collapsed;
  496. }
  497. }
  498. /// <summary>
  499. /// 更新listview显示
  500. /// state 状态显示字符串
  501. /// </summary>
  502. public void updateListview(string state)
  503. {
  504. updateCurrentListview();
  505. DataTable pdfdatatable = new DataTable();
  506. pdfdatatable.Columns.Add("FileName");
  507. pdfdatatable.Columns.Add("FilePageRangeText");
  508. pdfdatatable.Columns.Add("FilePageRangeSelectIndex");
  509. pdfdatatable.Columns.Add("IsEvenPageIsEnabled");
  510. pdfdatatable.Columns.Add("MaxPageRange");
  511. pdfdatatable.Columns.Add("FileSize");
  512. pdfdatatable.Columns.Add("FileState");
  513. int datatableindex = 0;
  514. List<int> PageIndexLists = new List<int>();
  515. foreach (var fileName in fileNames)
  516. {
  517. string file_all = fileName;
  518. FileInfo f = new FileInfo(file_all);
  519. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  520. if (!isEvenPage(fileName) && PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "2")
  521. {
  522. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  523. }
  524. int pagecount = CPDFDocument.InitWithFilePath(fileName).PageCount;
  525. if (PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "3" && !CommonHelper.GetPagesInRange(ref PageIndexLists, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"].ToString(), pagecount, new char[] { ',' }, new char[] { '-' }, true))
  526. {
  527. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  528. }
  529. pdfdatatable.Rows.Add(f.Name, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"], PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"], isEvenPage(fileName), pagecount, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  530. datatableindex++;
  531. }
  532. PDFDataTable = pdfdatatable;
  533. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  534. }
  535. /// <summary>
  536. /// 更新Currentlistview显示
  537. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  538. /// </summary>
  539. public void updateCurrentListview(string pagerangetext = "1", string pagerangeselectindex = "0", bool isevenpageisenabled = true, string state = "wait")
  540. {
  541. if (fileNames.Count >= FileNameNumber)
  542. {
  543. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  544. {
  545. PDFCurrentDataTable.Rows.Add(pagerangetext, pagerangeselectindex, isevenpageisenabled, state);
  546. }
  547. }
  548. else
  549. {
  550. Reverseorder(ref fileNamesView);
  551. foreach (int filenamesview in fileNamesView)
  552. {
  553. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  554. }
  555. }
  556. }
  557. /// <summary>
  558. /// 更新Currentlistview显示
  559. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  560. /// </summary>
  561. public void updateFilesPageRange(string pagerangetext = "1", string pagerangeselectindex = "0")
  562. {
  563. for (int i = 0; fileNames.Count > i; i++)
  564. {
  565. PDFDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  566. PDFDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  567. PDFCurrentDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  568. PDFCurrentDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  569. }
  570. }
  571. /// <summary>
  572. /// 获取文件列表单个文件对应的页码范围
  573. /// document 文档对象 pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  574. /// </summary>
  575. public string getFilePageRange(CPDFDocument document, string pagerangetext = "1", string pagerangeselectindex = "0")
  576. {
  577. string filePageRange = "1";
  578. EditToolsHelper.GetPageRange(int.Parse(pagerangeselectindex), document, ref filePageRange, pagerangetext);
  579. return filePageRange;
  580. }
  581. /// <summary>
  582. /// 判断是否关闭偶数页
  583. /// document 文档对象 )
  584. /// </summary>
  585. public bool isEvenPage(string filename)
  586. {
  587. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  588. bool isevenpage = false;
  589. if (document.PageCount > 1)
  590. {
  591. isevenpage = true;
  592. }
  593. document.Release();
  594. return isevenpage;
  595. }
  596. /// <summary>
  597. /// 逆序int类型集合
  598. /// </summary>
  599. public void Reverseorder(ref List<int> Numbers)
  600. {
  601. Numbers = Numbers.OrderBy(a => a).ToList();
  602. Numbers.Reverse();
  603. }
  604. /// <summary>
  605. /// 打开文件PDF
  606. /// </summary>
  607. public void openfiledialog()
  608. {
  609. foreach (int filenamesview in fileNamesView)
  610. {
  611. System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
  612. }
  613. }
  614. /// <summary>
  615. /// 删除文件PDF
  616. /// </summary>
  617. public void removepdffile()
  618. {
  619. FileNameNumber = fileNames.Count;
  620. Reverseorder(ref fileNamesView);
  621. foreach (int filenamesview in fileNamesView)
  622. {
  623. //Trace.WriteLine(filenamesview);
  624. fileNames.Remove(fileNames[filenamesview]);
  625. }
  626. if (fileNames.Count < 1)
  627. {
  628. SetWatermarkGridIsEnabled = "False";
  629. AddFileVisibility = Visibility.Visible;
  630. }
  631. updateListview("wait");
  632. }
  633. public void removepdffile(int index)
  634. {
  635. PDFCurrentDataTable.Rows.RemoveAt(index);
  636. fileNames.Remove(fileNames[index]);
  637. if (fileNames.Count < 1)
  638. {
  639. SetWatermarkGridIsEnabled = "False";
  640. AddFileVisibility = Visibility.Visible;
  641. }
  642. updateListview("wait");
  643. }
  644. public void PDFFileCount()
  645. {
  646. if (fileNames.Count == 0)
  647. {
  648. SetWatermarkGridIsEnabled = "False";
  649. AddFileVisibility = Visibility.Visible;
  650. }
  651. else
  652. {
  653. SetWatermarkGridIsEnabled = "True";
  654. AddFileVisibility = Visibility.Collapsed;
  655. }
  656. }
  657. #endregion
  658. #region 构架行为
  659. public void OnNavigatedTo(NavigationContext navigationContext)
  660. {
  661. List<string> filepath = new List<string>();
  662. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  663. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  664. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  665. if (filepath != null)
  666. {
  667. CreateGridSpan = "1";
  668. CreateGridSpanVisibility = Visibility.Visible;
  669. EnterSelectedContent(TemplateListName);
  670. fileNames = filepath;
  671. PDFFileCount();
  672. updateListview("wait");
  673. }
  674. }
  675. public bool IsNavigationTarget(NavigationContext navigationContext)
  676. {
  677. return true;
  678. }
  679. public void OnNavigatedFrom(NavigationContext navigationContext)
  680. {
  681. }
  682. #endregion
  683. }
  684. }