HomePageWatermarkDialogViewModel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. updateListview("wait");
  298. fileNamesIndex = 0;
  299. WatermarkGridIsEnabled = "False";
  300. foreach (var filename in fileNames)
  301. {
  302. FileInfo fileinfo = new FileInfo(filename);
  303. string OutputPath = fileinfo.DirectoryName;
  304. char[] enumerationSeparator = new char[] { ',' };
  305. char[] rangeSeparator = new char[] { '-' };
  306. List<int> PageIndexLists = new List<int>();
  307. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  308. if (document == null )
  309. {
  310. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  311. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  312. fileNamesIndex++;
  313. continue;
  314. }
  315. if (document.IsLocked)
  316. {
  317. DialogParameters value = new DialogParameters();
  318. value.Add(ParameterNames.PDFDocument, document);
  319. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  320. {
  321. if (e.Result == ButtonResult.OK)
  322. {
  323. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  324. {
  325. document.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  326. }
  327. }
  328. });
  329. if (document.IsLocked)
  330. {
  331. //未成功解密文档时,释放Document对象,返回
  332. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  333. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  334. fileNamesIndex++;
  335. continue;
  336. }
  337. }
  338. CreateWatermark(document);
  339. document.WriteToFilePath(CommonHelper.CreateFilePath(filename + "_Watermark.pdf"));
  340. //if (!CommonHelper.GetPagesInRange(ref PageIndexLists, "1-" + document.PageCount.ToString(), document.PageCount, enumerationSeparator, rangeSeparator))
  341. //{ //TODO
  342. // Trace.WriteLine("输入不对");
  343. // MessageBoxEx.Show("输入不对");
  344. // return;
  345. //}
  346. document.Release();
  347. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  348. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  349. fileNamesIndex++;
  350. }
  351. WatermarkGridIsEnabled = "True";
  352. }
  353. public void CreateWatermark(CPDFDocument document)
  354. {
  355. if (WatermarkInfo != null)
  356. {
  357. document.DeleteWatermarks();
  358. if (WatermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  359. {
  360. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  361. watermark.SetText(WatermarkInfo.Text);
  362. watermark.SetFontName(WatermarkInfo.FontName);
  363. if (WatermarkInfo.IsRelativeScale)
  364. {
  365. watermark.SetFontSizeWithPercentage(WatermarkInfo.TextSize);
  366. }
  367. else
  368. {
  369. watermark.SetFontSize(WatermarkInfo.TextSize);
  370. }
  371. watermark.SetTextRGBColor(WatermarkInfo.TextColor);
  372. }
  373. else
  374. {
  375. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  376. if (WatermarkInfo.ImageArray != null)
  377. {
  378. watermark.SetImage(WatermarkInfo.ImageArray, WatermarkInfo.ImageWidth, WatermarkInfo.ImageHeight);
  379. }
  380. watermark.SetScale(WatermarkInfo.ImageSize / 100);
  381. }
  382. watermark.SetRotation((float)((WatermarkInfo.Rotation / 180) * Math.PI));
  383. watermark.SetOpacity((byte)(((float)WatermarkInfo.Opacity / 100) * 255));
  384. watermark.SetFront(WatermarkInfo.IsFront);
  385. watermark.SetVertalign(WatermarkInfo.WatermarkVertalign);
  386. watermark.SetHorizalign(WatermarkInfo.WatermarkHorizalign);
  387. watermark.SetFullScreen(WatermarkInfo.Isfull);
  388. watermark.SetVertOffset(WatermarkInfo.VertOffset);
  389. watermark.SetHorizOffset(WatermarkInfo.HorizOffset);
  390. watermark.SetHorizontalSpacing(WatermarkInfo.HorizontalSpacing);
  391. watermark.SetVerticalSpacing(WatermarkInfo.VerticalSpacing);
  392. watermark.SetPages(getFilePageRange(document, PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeText"].ToString(), PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeSelectIndex"].ToString()));
  393. Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark());
  394. Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark());
  395. }
  396. }
  397. #endregion
  398. #region 批量处理逻辑函数
  399. /// <summary>
  400. /// 添加PDF文件
  401. /// </summary>
  402. private void addpicture()
  403. {
  404. FileNameNumber = fileNames.Count;
  405. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  406. dlg.Multiselect = true;
  407. dlg.Filter = "PDF|*.pdf;*.PDF;";
  408. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  409. {
  410. fileNames.AddRange(dlg.FileNames.ToList());
  411. RemoveExcess(ref fileNames);
  412. SetWatermarkGridIsEnabled = "True";
  413. AddFileVisibility = Visibility.Collapsed;
  414. updateListview("wait");
  415. }
  416. }
  417. public void addPDFFiles(string filename)
  418. {
  419. FileNameNumber = fileNames.Count;
  420. fileNames.Add(filename);
  421. RemoveExcess(ref fileNames);
  422. SetWatermarkGridIsEnabled = "True";
  423. AddFileVisibility = Visibility.Collapsed;
  424. updateListview("wait");
  425. }
  426. /// <summary>
  427. /// 添加已打开PDF文件
  428. /// </summary>
  429. public void addOpenedPDFFiles()
  430. {
  431. FileNameNumber = fileNames.Count;
  432. foreach (var filename in App.OpenedFileList)
  433. {
  434. fileNames.Add(filename);
  435. }
  436. RemoveExcess(ref fileNames);
  437. SetWatermarkGridIsEnabled = "True";
  438. AddFileVisibility = Visibility.Collapsed;
  439. updateListview("wait");
  440. }
  441. /// <summary>
  442. /// 删除重复的文件
  443. /// </summary>
  444. public void RemoveExcess(ref List<string> Filenames)
  445. {
  446. List<string> filenames = new List<string>();
  447. foreach (var fileName in Filenames)
  448. {
  449. if (!filenames.Contains(fileName))
  450. {
  451. filenames.Add(fileName);
  452. }
  453. }
  454. Filenames.Clear();
  455. Filenames = filenames;
  456. }
  457. /// <summary>
  458. /// 添加PDF文件夹
  459. /// </summary>
  460. private void addpicturefiles()
  461. {
  462. FileNameNumber = fileNames.Count;
  463. FolderBrowserDialog dialog = new FolderBrowserDialog();
  464. dialog.Description = "请选择文件路径";
  465. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  466. {
  467. string foldPath = dialog.SelectedPath;
  468. var apps = System.IO.Directory.GetFiles(foldPath);
  469. foreach (string app in apps)
  470. {
  471. var fi = new FileInfo(app);
  472. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  473. {
  474. fileNames.Add(app);
  475. }
  476. }
  477. RemoveExcess(ref fileNames);
  478. updateListview("wait");
  479. SetWatermarkGridIsEnabled = "True";
  480. AddFileVisibility = Visibility.Collapsed;
  481. }
  482. }
  483. /// <summary>
  484. /// 更新listview显示
  485. /// state 状态显示字符串
  486. /// </summary>
  487. public void updateListview(string state)
  488. {
  489. updateCurrentListview();
  490. DataTable pdfdatatable = new DataTable();
  491. pdfdatatable.Columns.Add("FileName");
  492. pdfdatatable.Columns.Add("FilePageRangeText");
  493. pdfdatatable.Columns.Add("FilePageRangeSelectIndex");
  494. pdfdatatable.Columns.Add("IsEvenPageIsEnabled");
  495. pdfdatatable.Columns.Add("MaxPageRange");
  496. pdfdatatable.Columns.Add("FileSize");
  497. pdfdatatable.Columns.Add("FileState");
  498. int datatableindex = 0;
  499. List<int> PageIndexLists = new List<int>();
  500. foreach (var fileName in fileNames)
  501. {
  502. string file_all = fileName;
  503. FileInfo f = new FileInfo(file_all);
  504. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  505. if (!isEvenPage(fileName) && PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "2")
  506. {
  507. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  508. }
  509. int pagecount = CPDFDocument.InitWithFilePath(fileName).PageCount;
  510. if (PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString() == "3" && !CommonHelper.GetPagesInRange(ref PageIndexLists, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"].ToString(), pagecount, new char[] { ',' }, new char[] { '-' }, true))
  511. {
  512. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  513. }
  514. pdfdatatable.Rows.Add(f.Name, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"], PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"], isEvenPage(fileName), pagecount, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  515. datatableindex++;
  516. }
  517. PDFDataTable = pdfdatatable;
  518. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  519. }
  520. /// <summary>
  521. /// 更新Currentlistview显示
  522. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  523. /// </summary>
  524. public void updateCurrentListview(string pagerangetext = "1", string pagerangeselectindex = "0", bool isevenpageisenabled = true, string state = "wait")
  525. {
  526. if (fileNames.Count >= FileNameNumber)
  527. {
  528. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  529. {
  530. PDFCurrentDataTable.Rows.Add(pagerangetext, pagerangeselectindex, isevenpageisenabled, state);
  531. }
  532. }
  533. else
  534. {
  535. Reverseorder(ref fileNamesView);
  536. foreach (int filenamesview in fileNamesView)
  537. {
  538. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  539. }
  540. }
  541. }
  542. /// <summary>
  543. /// 更新Currentlistview显示
  544. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  545. /// </summary>
  546. public void updateFilesPageRange(string pagerangetext = "1", string pagerangeselectindex = "0")
  547. {
  548. for (int i = 0; fileNames.Count > i; i++)
  549. {
  550. PDFDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  551. PDFDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  552. PDFCurrentDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  553. PDFCurrentDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  554. }
  555. }
  556. /// <summary>
  557. /// 获取文件列表单个文件对应的页码范围
  558. /// document 文档对象 pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  559. /// </summary>
  560. public string getFilePageRange(CPDFDocument document, string pagerangetext = "1", string pagerangeselectindex = "0")
  561. {
  562. string filePageRange = "1";
  563. EditToolsHelper.GetPageRange(int.Parse(pagerangeselectindex), document, ref filePageRange, pagerangetext);
  564. return filePageRange;
  565. }
  566. /// <summary>
  567. /// 判断是否关闭偶数页
  568. /// document 文档对象 )
  569. /// </summary>
  570. public bool isEvenPage(string filename)
  571. {
  572. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  573. bool isevenpage = false;
  574. if (document.PageCount > 1)
  575. {
  576. isevenpage = true;
  577. }
  578. document.Release();
  579. return isevenpage;
  580. }
  581. /// <summary>
  582. /// 逆序int类型集合
  583. /// </summary>
  584. public void Reverseorder(ref List<int> Numbers)
  585. {
  586. Numbers = Numbers.OrderBy(a => a).ToList();
  587. Numbers.Reverse();
  588. }
  589. /// <summary>
  590. /// 打开文件PDF
  591. /// </summary>
  592. public void openfiledialog()
  593. {
  594. foreach (int filenamesview in fileNamesView)
  595. {
  596. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  597. }
  598. }
  599. /// <summary>
  600. /// 删除文件PDF
  601. /// </summary>
  602. public void removepdffile()
  603. {
  604. FileNameNumber = fileNames.Count;
  605. Reverseorder(ref fileNamesView);
  606. foreach (int filenamesview in fileNamesView)
  607. {
  608. //Trace.WriteLine(filenamesview);
  609. fileNames.Remove(fileNames[filenamesview]);
  610. }
  611. if (fileNames.Count < 1)
  612. {
  613. SetWatermarkGridIsEnabled = "False";
  614. AddFileVisibility = Visibility.Visible;
  615. }
  616. updateListview("wait");
  617. }
  618. public void removepdffile(int index)
  619. {
  620. PDFCurrentDataTable.Rows.RemoveAt(index);
  621. fileNames.Remove(fileNames[index]);
  622. if (fileNames.Count < 1)
  623. {
  624. SetWatermarkGridIsEnabled = "False";
  625. AddFileVisibility = Visibility.Visible;
  626. }
  627. updateListview("wait");
  628. }
  629. public void PDFFileCount()
  630. {
  631. if (fileNames.Count == 0)
  632. {
  633. SetWatermarkGridIsEnabled = "False";
  634. AddFileVisibility = Visibility.Visible;
  635. }
  636. else
  637. {
  638. SetWatermarkGridIsEnabled = "True";
  639. AddFileVisibility = Visibility.Collapsed;
  640. }
  641. }
  642. #endregion
  643. #region 构架行为
  644. public void OnNavigatedTo(NavigationContext navigationContext)
  645. {
  646. List<string> filepath = new List<string>();
  647. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  648. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  649. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  650. if (filepath != null)
  651. {
  652. CreateGridSpan = "1";
  653. CreateGridSpanVisibility = Visibility.Visible;
  654. EnterSelectedContent(TemplateListName);
  655. fileNames = filepath;
  656. PDFFileCount();
  657. updateListview("wait");
  658. }
  659. }
  660. public bool IsNavigationTarget(NavigationContext navigationContext)
  661. {
  662. return true;
  663. }
  664. public void OnNavigatedFrom(NavigationContext navigationContext)
  665. {
  666. }
  667. #endregion
  668. }
  669. }