HomePageWatermarkDialogViewModel.cs 28 KB

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