HomePageBatesDialogViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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.Data;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Linq;
  14. using PDF_Office.Helper;
  15. using System.Windows;
  16. using System.Windows.Forms;
  17. using ComPDFKitViewer.PdfViewer;
  18. using Prism.Events;
  19. using PDF_Office.EventAggregators;
  20. using PDFSettings;
  21. using ImTools;
  22. using PDF_Office.Model.EditTools.Watermark;
  23. using PDF_Office.Model.EditTools.Bates;
  24. using PDF_Office.Model.EditTools.Background;
  25. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageBates
  26. {
  27. public class HomePageBatesDialogViewModel : BindableBase, INavigationAware
  28. {
  29. #region 参数和属性
  30. private List<string> fileNames;
  31. private CPDFDocument tempDocument;
  32. public string PageNumber = "";
  33. private CPDFBates bates;
  34. public BatesInfo BatesInfo;
  35. public IEventAggregator eventAggregator;
  36. public IRegionManager batesRegion;
  37. private CPDFViewer PDFViewer;
  38. private ViewContentViewModel viewContentViewModel;
  39. public string TemplateListName = "HomePageBatesTemplateListContent";
  40. public string CreateName = "HomePageBatesCreateContent";
  41. public string BatesDocumentName = "HomePageBatesDocumentContent";
  42. public string BatesType = "HomePageBatesType";
  43. public List<int> fileNamesView = new List<int>();
  44. private int fileNamesIndex = 0;
  45. public int FileNameNumber = 0;
  46. private string _batesSettingsRegionName;
  47. /// <summary>
  48. /// 属性设置Region
  49. /// </summary>
  50. public string BatesSettingsRegionName
  51. {
  52. get { return _batesSettingsRegionName; }
  53. set { _batesSettingsRegionName = value; }
  54. }
  55. private Visibility _batesSettingsVisible = Visibility.Visible;
  56. /// <summary>
  57. /// 属性设置Region可见
  58. /// </summary>
  59. public Visibility BatesSettingsVisible
  60. {
  61. get { return _batesSettingsVisible; }
  62. set { _batesSettingsVisible = value; }
  63. }
  64. private string batesGridIsEnabled = "True";
  65. public string BatesGridIsEnabled
  66. {
  67. get
  68. {
  69. return batesGridIsEnabled;
  70. }
  71. set
  72. {
  73. SetProperty(ref batesGridIsEnabled, value);
  74. }
  75. }
  76. private string setBatesGridIsEnabled = "True";
  77. public string SetBatesGridIsEnabled
  78. {
  79. get
  80. {
  81. return setBatesGridIsEnabled;
  82. }
  83. set
  84. {
  85. SetProperty(ref setBatesGridIsEnabled, value);
  86. }
  87. }
  88. private DataTable pdfDataTable = new DataTable();
  89. public DataTable PDFDataTable
  90. {
  91. get { return pdfDataTable; }
  92. set
  93. {
  94. SetProperty(ref pdfDataTable, value);
  95. }
  96. }
  97. private DataTable pdfCurrentDataTable = new DataTable();
  98. public DataTable PDFCurrentDataTable
  99. {
  100. get { return pdfCurrentDataTable; }
  101. set
  102. {
  103. SetProperty(ref pdfCurrentDataTable, value);
  104. }
  105. }
  106. private string removeIsEnable = "False";
  107. public string RemoveIsEnable
  108. {
  109. get { return removeIsEnable; }
  110. set
  111. {
  112. SetProperty(ref removeIsEnable, value);
  113. }
  114. }
  115. private string selectFileName = "False";
  116. public string SelectFileName
  117. {
  118. get { return selectFileName; }
  119. set
  120. {
  121. SetProperty(ref selectFileName, value);
  122. }
  123. }
  124. private Visibility batesVisible = Visibility.Collapsed;
  125. public Visibility BatesVisible
  126. {
  127. get { return batesVisible; }
  128. set { SetProperty(ref batesVisible, value); }
  129. }
  130. private Visibility addFileVisibility = Visibility.Hidden;
  131. public Visibility AddFileVisibility
  132. {
  133. get { return addFileVisibility; }
  134. set
  135. {
  136. SetProperty(ref addFileVisibility, value);
  137. RaisePropertyChanged();
  138. }
  139. }
  140. private string createGridSpan = "1";
  141. public string CreateGridSpan
  142. {
  143. get { return createGridSpan; }
  144. set
  145. {
  146. SetProperty(ref createGridSpan, value);
  147. RaisePropertyChanged();
  148. }
  149. }
  150. private Visibility createGridSpanVisibility = Visibility.Visible;
  151. public Visibility CreateGridSpanVisibility
  152. {
  153. get { return createGridSpanVisibility; }
  154. set
  155. {
  156. SetProperty(ref createGridSpanVisibility, value);
  157. RaisePropertyChanged();
  158. }
  159. }
  160. #endregion
  161. #region 委托声明
  162. public DelegateCommand ADDPDFCommand { get; set; }
  163. public DelegateCommand RemovePDFFileCommand { get; set; }
  164. public DelegateCommand CreateCommand { get; set; }
  165. public DelegateCommand ADDPDFFilesCommand { get; set; }
  166. #endregion
  167. public string Unicode = null;
  168. public HomePageBatesDialogViewModel(IRegionManager batesRegion, IEventAggregator eventAggregator)
  169. {
  170. this.eventAggregator = eventAggregator;
  171. this.batesRegion = batesRegion;
  172. BatesSettingsRegionName = Guid.NewGuid().ToString();
  173. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  174. PDFCurrentDataTable.Columns.Add("FilePageRangeText");
  175. PDFCurrentDataTable.Columns.Add("FilePageRangeSelectIndex");
  176. PDFCurrentDataTable.Columns.Add("IsEvenPageIsEnabled");
  177. PDFCurrentDataTable.Columns.Add("FileState");
  178. PDFDataTable.Columns.Add("FileName");
  179. PDFDataTable.Columns.Add("FilePageRangeText");
  180. PDFDataTable.Columns.Add("FilePageRangeSelectIndex");
  181. PDFDataTable.Columns.Add("IsEvenPageIsEnabled");
  182. PDFDataTable.Columns.Add("FileSize");
  183. PDFDataTable.Columns.Add("FileState");
  184. ADDPDFCommand = new DelegateCommand(addpicture);
  185. ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
  186. RemovePDFFileCommand = new DelegateCommand(removepdffile);
  187. CreateCommand = new DelegateCommand(create);
  188. eventAggregator.GetEvent<EditBatesHeaderFooterTemplateItemEvent>().Subscribe(EditBatesTemplateItem, e => e.Unicode == Unicode && e.Type == BatesType);
  189. eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Subscribe(EnterTemplateListOrCreate, e => e.Unicode == Unicode);
  190. }
  191. #region 逻辑函数
  192. public void EditBatesTemplateItem(BatesHeaderFooterItemUnicode batesItemunicode)
  193. {
  194. BatesHeaderFooterItem batesItem = batesItemunicode.Status;
  195. NavigationParameters param = new NavigationParameters();
  196. param.Add(ParameterNames.PDFViewer, PDFViewer);
  197. param.Add("BatesItem", batesItem);
  198. batesRegion.RequestNavigate(BatesSettingsRegionName, CreateName, param);
  199. BatesSettingsVisible = Visibility.Visible;
  200. CreateGridSpan = "2";
  201. CreateGridSpanVisibility = Visibility.Collapsed;
  202. }
  203. public void EnterTemplateListOrCreate(EnumTemplateListOrCreateUnicode enumTemplateListOrCreateunicode)
  204. {
  205. EnumTemplateListOrCreate enumTemplateListOrCreate = enumTemplateListOrCreateunicode.Status;
  206. if (enumTemplateListOrCreate == EnumTemplateListOrCreate.StatusTemplate)
  207. {
  208. CreateGridSpan = "1";
  209. CreateGridSpanVisibility = Visibility.Visible;
  210. EnterSelectedContent(TemplateListName);
  211. }
  212. else
  213. {
  214. CreateGridSpan = "2";
  215. CreateGridSpanVisibility = Visibility.Collapsed;
  216. EnterSelectedContent(CreateName);
  217. }
  218. }
  219. public void EnterSelectedContent(string SelectedContentName)
  220. {
  221. NavigationParameters param = new NavigationParameters();
  222. param.Add(ParameterNames.PDFViewer, PDFViewer);
  223. param.Add("ViewModel", this);
  224. batesRegion.RequestNavigate(BatesSettingsRegionName, SelectedContentName, param);
  225. BatesSettingsVisible = Visibility.Visible;
  226. }
  227. private void create()
  228. {
  229. fileNamesIndex = 0;
  230. BatesGridIsEnabled = "False";
  231. foreach (var filename in fileNames)
  232. {
  233. FileInfo fileinfo = new FileInfo(filename);
  234. string OutputPath = fileinfo.DirectoryName;
  235. char[] enumerationSeparator = new char[] { ',' };
  236. char[] rangeSeparator = new char[] { '-' };
  237. List<int> PageIndexLists = new List<int>();
  238. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  239. if (document == null)
  240. {
  241. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错";
  242. continue;
  243. }
  244. CreateBates(document);
  245. document.WriteToFilePath(filename + "_Bates.pdf");
  246. //if (!CommonHelper.GetPagesInRange(ref PageIndexLists, "1-" + document.PageCount.ToString(), document.PageCount, enumerationSeparator, rangeSeparator))
  247. //{ //TODO
  248. // Trace.WriteLine("输入不对");
  249. // MessageBoxEx.Show("输入不对");
  250. // return;
  251. //}
  252. //document.Release();
  253. //PDFDataTable.Rows[fileNamesIndex]["FilePageRangeSelectIndex"] = "2";
  254. PDFDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  255. //PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeText"] = "2";
  256. //PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeSelectIndex"] = "2";
  257. PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "完成";
  258. fileNamesIndex++;
  259. }
  260. BatesGridIsEnabled = "True";
  261. }
  262. public void CreateBates(CPDFDocument document)
  263. {
  264. if (BatesInfo != null)
  265. {
  266. if (bates != null) { bates.Release(); }
  267. bates = document.GetBates();
  268. for (int i = 0; i < 6; i++)
  269. {
  270. bates.SetText(i, BatesInfo.TextData[i].text);
  271. bates.SetFontName(i, BatesInfo.TextData[i].fontName);
  272. bates.SetFontSize(i, BatesInfo.TextData[i].fontSize);
  273. bates.SetTextColor(i, BatesInfo.TextData[i].Color);
  274. }
  275. bates.SetPageOffset(BatesInfo.StarPagetNumber + 1);
  276. bates.SetPages(getFilePageRange(document, PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeText"].ToString(), PDFCurrentDataTable.Rows[fileNamesIndex]["FilePageRangeSelectIndex"].ToString()));
  277. bates.SetMargin(BatesInfo.margin);
  278. bates.Update();
  279. }
  280. }
  281. #endregion
  282. #region 批量处理逻辑函数
  283. /// <summary>
  284. /// 添加PDF文件
  285. /// </summary>
  286. private void addpicture()
  287. {
  288. FileNameNumber = fileNames.Count;
  289. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  290. dlg.Multiselect = true;
  291. dlg.Filter = "PDF|*.pdf;*.PDF;";
  292. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  293. {
  294. fileNames.AddRange(dlg.FileNames.ToList());
  295. RemoveExcess(ref fileNames);
  296. SetBatesGridIsEnabled = "True";
  297. AddFileVisibility = Visibility.Collapsed;
  298. updateListview("待确定");
  299. }
  300. }
  301. /// <summary>
  302. /// 拖拽添加PDF文件
  303. /// </summary>
  304. public void addPDFFiles(string filename)
  305. {
  306. FileNameNumber = fileNames.Count;
  307. fileNames.Add(filename);
  308. RemoveExcess(ref fileNames);
  309. SetBatesGridIsEnabled = "True";
  310. AddFileVisibility = Visibility.Collapsed;
  311. updateListview("待确定");
  312. }
  313. /// <summary>
  314. /// 删除重复的文件
  315. /// </summary>
  316. public void RemoveExcess(ref List<string> Filenames)
  317. {
  318. List<string> filenames = new List<string>();
  319. foreach (var fileName in Filenames)
  320. {
  321. if (!filenames.Contains(fileName))
  322. {
  323. filenames.Add(fileName);
  324. }
  325. }
  326. Filenames.Clear();
  327. Filenames = filenames;
  328. }
  329. /// <summary>
  330. /// 添加PDF文件夹
  331. /// </summary>
  332. private void addpicturefiles()
  333. {
  334. FileNameNumber = fileNames.Count;
  335. FolderBrowserDialog dialog = new FolderBrowserDialog();
  336. dialog.Description = "请选择文件路径";
  337. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  338. {
  339. string foldPath = dialog.SelectedPath;
  340. var apps = System.IO.Directory.GetFiles(foldPath);
  341. foreach (string app in apps)
  342. {
  343. var fi = new FileInfo(app);
  344. if (fi.Extension == ".pdf" || fi.Extension == ".PDF")
  345. {
  346. fileNames.Add(app);
  347. }
  348. }
  349. RemoveExcess(ref fileNames);
  350. updateListview("待确定");
  351. SetBatesGridIsEnabled = "True";
  352. AddFileVisibility = Visibility.Collapsed;
  353. }
  354. }
  355. /// <summary>
  356. /// 更新listview显示
  357. /// state 状态显示字符串
  358. /// </summary>
  359. public void updateListview(string state)
  360. {
  361. updateCurrentListview();
  362. DataTable pdfdatatable = new DataTable();
  363. pdfdatatable.Columns.Add("FileName");
  364. pdfdatatable.Columns.Add("FilePageRangeText");
  365. pdfdatatable.Columns.Add("FilePageRangeSelectIndex");
  366. pdfdatatable.Columns.Add("IsEvenPageIsEnabled");
  367. pdfdatatable.Columns.Add("FileSize");
  368. pdfdatatable.Columns.Add("FileState");
  369. int datatableindex = 0;
  370. List<int> PageIndexLists = new List<int>();
  371. foreach (var fileName in fileNames)
  372. {
  373. string file_all = fileName;
  374. FileInfo f = new FileInfo(file_all);
  375. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  376. if (!isEvenPage(fileName)&& PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"].ToString()=="2") {
  377. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  378. }
  379. 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)) {
  380. PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"] = "0";
  381. }
  382. pdfdatatable.Rows.Add(f.Name, PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeText"], PDFCurrentDataTable.Rows[datatableindex]["FilePageRangeSelectIndex"], isEvenPage(fileName), file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]);
  383. datatableindex++;
  384. }
  385. PDFDataTable = pdfdatatable;
  386. HomePageBatchProcessingDialogModel.FilePaths = fileNames;
  387. }
  388. /// <summary>
  389. /// 更新Currentlistview显示
  390. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  391. /// </summary>
  392. public void updateCurrentListview(string pagerangetext = "1", string pagerangeselectindex = "0", bool isevenpageisenabled = true, string state = "待完成")
  393. {
  394. if (fileNames.Count >= FileNameNumber)
  395. {
  396. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  397. {
  398. PDFCurrentDataTable.Rows.Add(pagerangetext, pagerangeselectindex, isevenpageisenabled, state);
  399. }
  400. }
  401. else
  402. {
  403. Reverseorder(ref fileNamesView);
  404. foreach (int filenamesview in fileNamesView)
  405. {
  406. PDFCurrentDataTable.Rows.RemoveAt(filenamesview);
  407. }
  408. }
  409. }
  410. /// <summary>
  411. /// 更新Currentlistview显示
  412. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  413. /// </summary>
  414. public void updateFilesPageRange(string pagerangetext = "1", string pagerangeselectindex = "0")
  415. {
  416. for (int i = 0; fileNames.Count > i; i++)
  417. {
  418. PDFDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  419. PDFDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  420. PDFCurrentDataTable.Rows[i]["FilePageRangeText"] = pagerangetext;
  421. PDFCurrentDataTable.Rows[i]["FilePageRangeSelectIndex"] = pagerangeselectindex;
  422. }
  423. }
  424. /// <summary>
  425. /// 获取文件列表单个文件对应的页码范围
  426. /// document 文档对象 pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)
  427. /// </summary>
  428. public string getFilePageRange(CPDFDocument document, string pagerangetext = "1", string pagerangeselectindex = "0")
  429. {
  430. string filePageRange = "1";
  431. EditToolsHelper.GetPageRange(int.Parse(pagerangeselectindex), document, ref filePageRange, pagerangetext);
  432. return filePageRange;
  433. }
  434. /// <summary>
  435. /// 判断是否关闭偶数页
  436. /// document 文档对象 )
  437. /// </summary>
  438. public bool isEvenPage(string filename)
  439. {
  440. CPDFDocument document = CPDFDocument.InitWithFilePath(filename);
  441. bool isevenpage = false;
  442. if (document.PageCount > 1)
  443. {
  444. isevenpage = true;
  445. }
  446. document.Release();
  447. return isevenpage;
  448. }
  449. /// <summary>
  450. /// 逆序int类型集合
  451. /// </summary>
  452. public void Reverseorder(ref List<int> Numbers)
  453. {
  454. Numbers = Numbers.OrderBy(a => a).ToList();
  455. Numbers.Reverse();
  456. }
  457. /// <summary>
  458. /// 打开文件PDF
  459. /// </summary>
  460. public void openfiledialog()
  461. {
  462. foreach (int filenamesview in fileNamesView)
  463. {
  464. System.Diagnostics.Process.Start(fileNames[filenamesview]);
  465. }
  466. }
  467. /// <summary>
  468. /// 删除文件PDF
  469. /// </summary>
  470. public void removepdffile()
  471. {
  472. FileNameNumber = fileNames.Count;
  473. Reverseorder(ref fileNamesView);
  474. foreach (int filenamesview in fileNamesView)
  475. {
  476. //Trace.WriteLine(filenamesview);
  477. fileNames.Remove(fileNames[filenamesview]);
  478. }
  479. if (fileNames.Count < 1)
  480. {
  481. SetBatesGridIsEnabled = "False";
  482. AddFileVisibility = Visibility.Visible;
  483. }
  484. updateListview("待确定");
  485. }
  486. public void removepdffile(int index)
  487. {
  488. fileNames.Remove(fileNames[index]);
  489. if (fileNames.Count < 1)
  490. {
  491. SetBatesGridIsEnabled = "False";
  492. AddFileVisibility = Visibility.Visible;
  493. }
  494. updateListview("待确定");
  495. }
  496. public void PDFFileCount()
  497. {
  498. if (fileNames.Count == 0)
  499. {
  500. SetBatesGridIsEnabled = "False";
  501. AddFileVisibility = Visibility.Visible;
  502. }
  503. else
  504. {
  505. SetBatesGridIsEnabled = "True";
  506. AddFileVisibility = Visibility.Collapsed;
  507. }
  508. }
  509. #endregion
  510. #region 构架行为
  511. public void OnNavigatedTo(NavigationContext navigationContext)
  512. {
  513. List<string> filepath = new List<string>();
  514. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  515. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  516. navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
  517. if (filepath != null)
  518. {
  519. fileNames = filepath;
  520. PDFFileCount();
  521. updateListview("待确定");
  522. EnterSelectedContent(TemplateListName);
  523. }
  524. }
  525. public bool IsNavigationTarget(NavigationContext navigationContext)
  526. {
  527. return true;
  528. }
  529. public void OnNavigatedFrom(NavigationContext navigationContext)
  530. {
  531. }
  532. #endregion
  533. }
  534. }