HomePageWatermarkDialogViewModel.cs 30 KB

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