HomePageWatermarkDialogViewModel.cs 28 KB

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