HomePageWatermarkDialogViewModel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  4. using PDF_Office.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_Office.Helper;
  15. using System.Windows;
  16. using System.Data;
  17. using ComPDFKitViewer.PdfViewer;
  18. using Prism.Events;
  19. using PDF_Office.EventAggregators;
  20. using PDFSettings;
  21. using PDF_Office.Model.EditTools.Watermark;
  22. using ComPDFKit.PDFWatermark;
  23. using Prism.Services.Dialogs;
  24. namespace PDF_Office.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. #endregion
  182. #region 委托声明
  183. public DelegateCommand ADDPDFCommand { get; set; }
  184. public DelegateCommand ADDOpenedPDFCommand { get; set; }
  185. public DelegateCommand RemovePDFFileCommand { get; set; }
  186. public DelegateCommand CreateCommand { get; set; }
  187. public DelegateCommand ADDPDFFilesCommand { get; set; }
  188. #endregion
  189. public string Unicode = null;
  190. public HomePageWatermarkDialogViewModel(IRegionManager watermarkRegion, IEventAggregator eventAggregator, IDialogService dialogs)
  191. {
  192. this.eventAggregator = eventAggregator;
  193. this.watermarkRegion = watermarkRegion;
  194. this.dialogs = dialogs;
  195. Unicode = Guid.NewGuid().ToString();
  196. WatermarkSettingsVisible = Visibility.Visible;
  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. CreateCommand = new DelegateCommand(create);
  213. WatermarkSettingsRegionName = Guid.NewGuid().ToString();
  214. eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Subscribe(EnterTemplateListOrCreate, e => e.Unicode == Unicode);
  215. eventAggregator.GetEvent<SetCurrentCreateModEvent>().Subscribe(SetCurrentCreateMod, e => e.Unicode == Unicode);
  216. eventAggregator.GetEvent<SetCurrentTemplateListModEvent>().Subscribe(SetCurrentTemplateListMod, e => e.Unicode == Unicode);
  217. eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
  218. eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Subscribe(EditWatermarkTemplateItem, e => e.Unicode == Unicode);
  219. }
  220. #region 逻辑函数
  221. public void EnterTemplateListOrCreate(EnumTemplateListOrCreateUnicode enumTemplateListOrCreateunicode)
  222. {
  223. EnumTemplateListOrCreate enumTemplateListOrCreate = enumTemplateListOrCreateunicode.Status;
  224. if (enumTemplateListOrCreate == EnumTemplateListOrCreate.StatusTemplate)
  225. {
  226. CreateGridSpan = "1";
  227. CreateGridSpanVisibility = Visibility.Visible;
  228. EnterSelectedContent(TemplateListName);
  229. }
  230. else
  231. {
  232. CreateGridSpan = "2";
  233. CreateGridSpanVisibility = Visibility.Collapsed;
  234. EnterSelectedContent(CreateBaseName);
  235. }
  236. }
  237. public void EnterSelectedContent(string SelectedContentName)
  238. {
  239. NavigationParameters param = new NavigationParameters();
  240. param.Add(ParameterNames.PDFViewer, PDFViewer);
  241. param.Add("Unicode", Unicode);
  242. param.Add("ViewModel", this);
  243. if (SelectedContentName == TemplateListName)
  244. {
  245. param.Add("CurrentCreateModName", CurrentCreateMod);
  246. }
  247. else if (SelectedContentName == CreateBaseName)
  248. {
  249. param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
  250. }
  251. watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, SelectedContentName, param);
  252. WatermarkSettingsVisible = Visibility.Visible;
  253. }
  254. public void SetCurrentCreateMod(stringUnicode currentCreateModNameunicode)
  255. {
  256. string currentCreateModName = currentCreateModNameunicode.Status;
  257. if (currentCreateModName == CreateModTextName)
  258. {
  259. CurrentCreateMod = EnumTextOrFile.StatusText;
  260. }
  261. else if (currentCreateModName == CreateModFileName)
  262. {
  263. CurrentCreateMod = EnumTextOrFile.StatusFile;
  264. }
  265. }
  266. public void SetCurrentTemplateListMod(stringUnicode currentTemplateListModNameunicode)
  267. {
  268. string currentTemplateListModName = currentTemplateListModNameunicode.Status;
  269. if (currentTemplateListModName == TemplateListModTextName)
  270. {
  271. CurrentTemplateListMod = EnumTextOrFile.StatusText;
  272. }
  273. else if (currentTemplateListModName == TemplateListModFileName)
  274. {
  275. CurrentTemplateListMod = EnumTextOrFile.StatusFile;
  276. }
  277. }
  278. public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicod)
  279. {
  280. CurrentPDFViewer = cPDFViewerunicod.Status;
  281. }
  282. public void EditWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
  283. {
  284. WatermarkItem watermarkItem = watermarkItemunicode.Status;
  285. NavigationParameters param = new NavigationParameters();
  286. param.Add("Unicode", Unicode);
  287. param.Add(ParameterNames.PDFViewer, PDFViewer);
  288. param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
  289. param.Add("WatermarkItem", watermarkItem);
  290. watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, CreateBaseName, param);
  291. WatermarkSettingsVisible = Visibility.Visible;
  292. CreateGridSpan = "2";
  293. CreateGridSpanVisibility = Visibility.Collapsed;
  294. }
  295. private void create()
  296. {
  297. HomePageBatchProcessingDialogModel.closeDialog = true;
  298. updateListview("wait");
  299. fileNamesIndex = 0;
  300. WatermarkGridIsEnabled = "False";
  301. foreach (var filename in fileNames)
  302. {
  303. FileInfo fileinfo = new FileInfo(filename);
  304. string OutputPath = fileinfo.DirectoryName;
  305. char[] enumerationSeparator = new char[] { ',' };
  306. char[] rangeSeparator = new char[] { '-' };
  307. List<int> PageIndexLists = new List<int>();
  308. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  309. if (document == null )
  310. {
  311. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  312. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  313. fileNamesIndex++;
  314. continue;
  315. }
  316. if (document.IsLocked)
  317. {
  318. DialogParameters value = new DialogParameters();
  319. value.Add(ParameterNames.PDFDocument, document);
  320. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  321. {
  322. if (e.Result == ButtonResult.OK)
  323. {
  324. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  325. {
  326. document.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  327. }
  328. }
  329. });
  330. if (document.IsLocked)
  331. {
  332. //未成功解密文档时,释放Document对象,返回
  333. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  334. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  335. fileNamesIndex++;
  336. continue;
  337. }
  338. }
  339. CreateWatermark(document);
  340. document.WriteToFilePath(CommonHelper.CreateFilePath(filename + "_Watermark.pdf"));
  341. //if (!CommonHelper.GetPagesInRange(ref PageIndexLists, "1-" + document.PageCount.ToString(), document.PageCount, enumerationSeparator, rangeSeparator))
  342. //{ //TODO
  343. // Trace.WriteLine("输入不对");
  344. // MessageBoxEx.Show("输入不对");
  345. // return;
  346. //}
  347. document.Release();
  348. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  349. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  350. fileNamesIndex++;
  351. }
  352. WatermarkGridIsEnabled = "True";
  353. HomePageBatchProcessingDialogModel.closeDialog = false;
  354. }
  355. public void CreateWatermark(CPDFDocument document)
  356. {
  357. if (WatermarkInfo != null)
  358. {
  359. document.DeleteWatermarks();
  360. if (WatermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  361. {
  362. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  363. watermark.SetText(WatermarkInfo.Text);
  364. watermark.SetFontName(WatermarkInfo.FontName);
  365. if (WatermarkInfo.IsRelativeScale)
  366. {
  367. watermark.SetFontSizeWithPercentage(WatermarkInfo.TextSize);
  368. }
  369. else
  370. {
  371. watermark.SetFontSize(WatermarkInfo.TextSize);
  372. }
  373. watermark.SetTextRGBColor(WatermarkInfo.TextColor);
  374. }
  375. else
  376. {
  377. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  378. if (WatermarkInfo.ImageArray != null)
  379. {
  380. watermark.SetImage(WatermarkInfo.ImageArray, WatermarkInfo.ImageWidth, WatermarkInfo.ImageHeight);
  381. }
  382. watermark.SetScale(WatermarkInfo.ImageSize / 100);
  383. }
  384. watermark.SetRotation((float)((WatermarkInfo.Rotation / 180) * Math.PI));
  385. watermark.SetOpacity((byte)(((float)WatermarkInfo.Opacity / 100) * 255));
  386. watermark.SetFront(WatermarkInfo.IsFront);
  387. watermark.SetVertalign(WatermarkInfo.WatermarkVertalign);
  388. watermark.SetHorizalign(WatermarkInfo.WatermarkHorizalign);
  389. watermark.SetFullScreen(WatermarkInfo.Isfull);
  390. watermark.SetVertOffset(WatermarkInfo.VertOffset);
  391. watermark.SetHorizOffset(WatermarkInfo.HorizOffset);
  392. watermark.SetHorizontalSpacing(WatermarkInfo.HorizontalSpacing);
  393. watermark.SetVerticalSpacing(WatermarkInfo.VerticalSpacing);
  394. watermark.SetPages(getFilePageRange(document, PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeText"].ToString(), PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeSelectIndex"].ToString()));
  395. Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark());
  396. Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark());
  397. }
  398. }
  399. #endregion
  400. #region 批量处理逻辑函数
  401. /// <summary>
  402. /// 添加PDF文件
  403. /// </summary>
  404. private void addpicture()
  405. {
  406. FileNameNumber = fileNames.Count;
  407. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  408. dlg.Multiselect = true;
  409. dlg.Filter = "PDF|*.pdf;*.PDF;";
  410. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  411. {
  412. fileNames.AddRange(dlg.FileNames.ToList());
  413. RemoveExcess(ref fileNames);
  414. SetWatermarkGridIsEnabled = "True";
  415. AddFileVisibility = Visibility.Collapsed;
  416. updateListview("wait");
  417. }
  418. }
  419. public void addPDFFiles(string filename)
  420. {
  421. FileNameNumber = fileNames.Count;
  422. fileNames.Add(filename);
  423. RemoveExcess(ref fileNames);
  424. SetWatermarkGridIsEnabled = "True";
  425. AddFileVisibility = Visibility.Collapsed;
  426. updateListview("wait");
  427. }
  428. /// <summary>
  429. /// 添加已打开PDF文件
  430. /// </summary>
  431. public void addOpenedPDFFiles()
  432. {
  433. FileNameNumber = fileNames.Count;
  434. foreach (var filename in App.OpenedFileList)
  435. {
  436. fileNames.Add(filename);
  437. }
  438. RemoveExcess(ref fileNames);
  439. SetWatermarkGridIsEnabled = "True";
  440. AddFileVisibility = Visibility.Collapsed;
  441. updateListview("wait");
  442. }
  443. /// <summary>
  444. /// 删除重复的文件
  445. /// </summary>
  446. public void RemoveExcess(ref List<string> Filenames)
  447. {
  448. List<string> filenames = new List<string>();
  449. foreach (var fileName in Filenames)
  450. {
  451. if (!filenames.Contains(fileName))
  452. {
  453. filenames.Add(fileName);
  454. }
  455. }
  456. Filenames.Clear();
  457. Filenames = filenames;
  458. }
  459. /// <summary>
  460. /// 添加PDF文件夹
  461. /// </summary>
  462. private void addpicturefiles()
  463. {
  464. FileNameNumber = fileNames.Count;
  465. FolderBrowserDialog dialog = new FolderBrowserDialog();
  466. dialog.Description = "请选择文件路径";
  467. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  468. {
  469. string foldPath = dialog.SelectedPath;
  470. var apps = System.IO.Directory.GetFiles(foldPath);
  471. foreach (string app in apps)
  472. {
  473. var fi = new FileInfo(app);
  474. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  475. {
  476. fileNames.Add(app);
  477. }
  478. }
  479. RemoveExcess(ref fileNames);
  480. updateListview("wait");
  481. SetWatermarkGridIsEnabled = "True";
  482. AddFileVisibility = Visibility.Collapsed;
  483. }
  484. }
  485. /// <summary>
  486. /// 更新listview显示
  487. /// state 状态显示字符串
  488. /// </summary>
  489. public void updateListview(string state)
  490. {
  491. updateCurrentListview();
  492. DataTable pdfdatatable = new DataTable();
  493. pdfdatatable.Columns.Add("FileName");
  494. pdfdatatable.Columns.Add("FilePageRangeText");
  495. pdfdatatable.Columns.Add("FilePageRangeSelectIndex");
  496. pdfdatatable.Columns.Add("IsEvenPageIsEnabled");
  497. pdfdatatable.Columns.Add("MaxPageRange");
  498. pdfdatatable.Columns.Add("FileSize");
  499. pdfdatatable.Columns.Add("FileState");
  500. int datatableindex = 0;
  501. List<int> PageIndexLists = new List<int>();
  502. foreach (var fileName in fileNames)
  503. {
  504. string file_all = fileName;
  505. FileInfo f = new FileInfo(file_all);
  506. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  507. if (!isEvenPage(fileName) && PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "2")
  508. {
  509. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  510. }
  511. int pagecount = CPDFDocument.InitWithFilePath(fileName).PageCount;
  512. if (PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "3" && !CommonHelper.GetPagesInRange(ref PageIndexLists, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"].ToString(), pagecount, new char[] { ',' }, new char[] { '-' }, true))
  513. {
  514. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  515. }
  516. pdfdatatable.Rows.Add(f.Name, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"], PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"], isEvenPage(fileName), pagecount, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  517. datatableindex++;
  518. }
  519. PDFDataTable = pdfdatatable;
  520. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  521. }
  522. /// <summary>
  523. /// 更新Currentlistview显示
  524. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  525. /// </summary>
  526. public void updateCurrentListview(string pagerangetext = "1", string pagerangeselectindex = "0", bool isevenpageisenabled = true, string state = "wait")
  527. {
  528. if (fileNames.Count >= FileNameNumber)
  529. {
  530. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  531. {
  532. PDFCurrentDataTable.Rows.Add(pagerangetext, pagerangeselectindex, isevenpageisenabled, state);
  533. }
  534. }
  535. else
  536. {
  537. Reverseorder(ref fileNamesView);
  538. foreach (int filenamesview in fileNamesView)
  539. {
  540. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  541. }
  542. }
  543. }
  544. /// <summary>
  545. /// 更新Currentlistview显示
  546. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  547. /// </summary>
  548. public void updateFilesPageRange(string pagerangetext = "1", string pagerangeselectindex = "0")
  549. {
  550. for (int i = 0; fileNames.Count > i; i++)
  551. {
  552. PDFDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  553. PDFDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  554. PDFCurrentDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  555. PDFCurrentDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  556. }
  557. }
  558. /// <summary>
  559. /// 获取文件列表单个文件对应的页码范围
  560. /// document 文档对象 pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  561. /// </summary>
  562. public string getFilePageRange(CPDFDocument document, string pagerangetext = "1", string pagerangeselectindex = "0")
  563. {
  564. string filePageRange = "1";
  565. EditToolsHelper.GetPageRange(int.Parse(pagerangeselectindex), document, ref filePageRange, pagerangetext);
  566. return filePageRange;
  567. }
  568. /// <summary>
  569. /// 判断是否关闭偶数页
  570. /// document 文档对象 )
  571. /// </summary>
  572. public bool isEvenPage(string filename)
  573. {
  574. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  575. bool isevenpage = false;
  576. if (document.PageCount > 1)
  577. {
  578. isevenpage = true;
  579. }
  580. document.Release();
  581. return isevenpage;
  582. }
  583. /// <summary>
  584. /// 逆序int类型集合
  585. /// </summary>
  586. public void Reverseorder(ref List<int> Numbers)
  587. {
  588. Numbers = Numbers.OrderBy(a => a).ToList();
  589. Numbers.Reverse();
  590. }
  591. /// <summary>
  592. /// 打开文件PDF
  593. /// </summary>
  594. public void openfiledialog()
  595. {
  596. foreach (int filenamesview in fileNamesView)
  597. {
  598. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  599. }
  600. }
  601. /// <summary>
  602. /// 删除文件PDF
  603. /// </summary>
  604. public void removepdffile()
  605. {
  606. FileNameNumber = fileNames.Count;
  607. Reverseorder(ref fileNamesView);
  608. foreach (int filenamesview in fileNamesView)
  609. {
  610. //Trace.WriteLine(filenamesview);
  611. fileNames.Remove(fileNames[filenamesview]);
  612. }
  613. if (fileNames.Count < 1)
  614. {
  615. SetWatermarkGridIsEnabled = "False";
  616. AddFileVisibility = Visibility.Visible;
  617. }
  618. updateListview("wait");
  619. }
  620. public void removepdffile(int index)
  621. {
  622. PDFCurrentDataTable.Rows.RemoveAt(index);
  623. fileNames.Remove(fileNames[index]);
  624. if (fileNames.Count < 1)
  625. {
  626. SetWatermarkGridIsEnabled = "False";
  627. AddFileVisibility = Visibility.Visible;
  628. }
  629. updateListview("wait");
  630. }
  631. public void PDFFileCount()
  632. {
  633. if (fileNames.Count == 0)
  634. {
  635. SetWatermarkGridIsEnabled = "False";
  636. AddFileVisibility = Visibility.Visible;
  637. }
  638. else
  639. {
  640. SetWatermarkGridIsEnabled = "True";
  641. AddFileVisibility = Visibility.Collapsed;
  642. }
  643. }
  644. #endregion
  645. #region 构架行为
  646. public void OnNavigatedTo(NavigationContext navigationContext)
  647. {
  648. List<string> filepath = new List<string>();
  649. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  650. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  651. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  652. if (filepath != null)
  653. {
  654. CreateGridSpan = "1";
  655. CreateGridSpanVisibility = Visibility.Visible;
  656. EnterSelectedContent(TemplateListName);
  657. fileNames = filepath;
  658. PDFFileCount();
  659. updateListview("wait");
  660. }
  661. }
  662. public bool IsNavigationTarget(NavigationContext navigationContext)
  663. {
  664. return true;
  665. }
  666. public void OnNavigatedFrom(NavigationContext navigationContext)
  667. {
  668. }
  669. #endregion
  670. }
  671. }