HomePageBatesDialogViewModel.cs 21 KB

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