HomePageSplitDialogViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Master.Model;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using PDF_Master.Helper;
  9. using System.Diagnostics;
  10. using PDF_Master.CustomControl;
  11. using PDF_Master.Model.HomePageToolsDialogs;
  12. using System.Linq;
  13. using System.Windows.Forms;
  14. using DialogResult = Prism.Services.Dialogs.DialogResult;
  15. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
  16. {
  17. public class HomePageSplitDialogViewModel : BindableBase, IDialogAware
  18. {
  19. #region 参数和属性
  20. public CPDFDocument document;
  21. public CPDFViewer currentViewer;
  22. private HomePageSplitDialogModel splitModel = new HomePageSplitDialogModel();
  23. private string savefilepath { get; set; }
  24. private string savefilename { get; set; }
  25. public string PageRangeText { set; get; } = "0";
  26. public string PageRangeSelectIndex { set; get; } = "0";
  27. private string averagePagesIsCheck = "True";
  28. public string AveragePagesIsCheck
  29. {
  30. get { return averagePagesIsCheck; }
  31. set
  32. {
  33. SetProperty(ref averagePagesIsCheck, value);
  34. }
  35. }
  36. private string averageFilesIsCheck = "False";
  37. public string AverageFilesIsCheck
  38. {
  39. get { return averageFilesIsCheck; }
  40. set
  41. {
  42. SetProperty(ref averageFilesIsCheck, value);
  43. }
  44. }
  45. private string customPageRangeIsCheck = "False";
  46. public string CustomPageRangeIsCheck
  47. {
  48. get { return customPageRangeIsCheck; }
  49. set
  50. {
  51. SetProperty(ref customPageRangeIsCheck, value);
  52. }
  53. }
  54. private string averagePagesIsEnabled = "True";
  55. public string AveragePagesIsEnabled
  56. {
  57. get { return averagePagesIsEnabled; }
  58. set
  59. {
  60. SetProperty(ref averagePagesIsEnabled, value);
  61. }
  62. }
  63. private string averageFilesIsEnabled = "False";
  64. public string AverageFilesIsEnabled
  65. {
  66. get { return averageFilesIsEnabled; }
  67. set
  68. {
  69. SetProperty(ref averageFilesIsEnabled, value);
  70. }
  71. }
  72. private string customPageRangeIsEnabled = "False";
  73. public string CustomPageRangeIsEnabled
  74. {
  75. get { return customPageRangeIsEnabled; }
  76. set
  77. {
  78. SetProperty(ref customPageRangeIsEnabled, value);
  79. }
  80. }
  81. public string AveragePagesText { set; get; } = "1";
  82. public string AverageFilesText { set; get; } = "1";
  83. private string filePathName = "";
  84. public string FilePathName
  85. {
  86. get { return filePathName; }
  87. set
  88. {
  89. value = value + ".pdf";
  90. SetProperty(ref filePathName, value);
  91. }
  92. }
  93. public string FileNameLabelText { set; get; } = "part";
  94. public string FileNameDeimiterText { set; get; } = "-";
  95. public string FrontFileIsCheck { set; get; } = "True";
  96. public string FileNameLabelIsCheck { set; get; } = "True";
  97. public string FileNameDeimiterIsCheck { set; get; } = "True";
  98. private string fileNameLabelIsEnabled = "False";
  99. public string FileNameLabelIsEnabled
  100. {
  101. get { return fileNameLabelIsEnabled; }
  102. set
  103. {
  104. SetProperty(ref fileNameLabelIsEnabled, value);
  105. }
  106. }
  107. private string fileNameDeimiterIsEnabled = "False";
  108. public string FileNameDeimiterIsEnabled
  109. {
  110. get { return fileNameDeimiterIsEnabled; }
  111. set
  112. {
  113. SetProperty(ref fileNameDeimiterIsEnabled, value);
  114. }
  115. }
  116. private int maxPageRange = 0;
  117. public int MaxPageRange
  118. {
  119. get { return maxPageRange; }
  120. set
  121. {
  122. SetProperty(ref maxPageRange, value);
  123. }
  124. }
  125. #endregion
  126. #region 委托声明
  127. public DelegateCommand CancelCommand { get; set; }
  128. public DelegateCommand SplitCommand { get; set; }
  129. public DelegateCommand AveragePagesCommand { get; set; }
  130. public DelegateCommand AverageFilesCommand { get; set; }
  131. public DelegateCommand CustomPageRangeCommand { get; set; }
  132. public DelegateCommand FrontFileNameCommand { get; set; }
  133. public DelegateCommand FileNameLabelCommand { get; set; }
  134. public DelegateCommand FileNameDeimiterCommand { get; set; }
  135. public DelegateCommand FileNameDeimiterChangeCommand { get; set; }
  136. public DelegateCommand FileNameLabelChangeCommand { get; set; }
  137. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  138. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  139. #endregion
  140. public HomePageSplitDialogViewModel()
  141. {
  142. CancelCommand = new DelegateCommand(cancel);
  143. SplitCommand = new DelegateCommand(split);
  144. AveragePagesCommand = new DelegateCommand(averagePages);
  145. AverageFilesCommand = new DelegateCommand(averageFiles);
  146. CustomPageRangeCommand = new DelegateCommand(customPageRange);
  147. FrontFileNameCommand = new DelegateCommand(frontFileName);
  148. FileNameLabelCommand = new DelegateCommand(fileNameLabel);
  149. FileNameDeimiterCommand = new DelegateCommand(fileNameDeimiter);
  150. FileNameDeimiterChangeCommand = new DelegateCommand(fileNameDeimiter);
  151. FileNameLabelChangeCommand = new DelegateCommand(fileNameLabel);
  152. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  153. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  154. }
  155. #region 逻辑函数
  156. private void FileNameIsCheck(string saveSelectedPath)
  157. {
  158. if (FileNameDeimiterIsCheck == "True")
  159. {
  160. splitModel.FileNameDeimiter = FileNameDeimiterText;
  161. }
  162. else
  163. {
  164. splitModel.FileNameDeimiter = "";
  165. }
  166. if (FileNameLabelIsCheck == "True")
  167. {
  168. splitModel.FileNameLabel = FileNameLabelText;
  169. }
  170. else
  171. {
  172. splitModel.FileNameLabel = "";
  173. }
  174. if (FrontFileIsCheck == "True")
  175. {
  176. splitModel.FrontFileName = true;
  177. splitModel.FileName = (saveSelectedPath + currentViewer.Document.FileName + splitModel.FileNameDeimiter + splitModel.FileNameLabel);
  178. }
  179. else
  180. {
  181. splitModel.FrontFileName = false;
  182. splitModel.FileName = (saveSelectedPath + splitModel.FileNameLabel + splitModel.FileNameDeimiter + currentViewer.Document.FileName);
  183. }
  184. }
  185. private void cancel()
  186. {
  187. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  188. }
  189. private void split()
  190. {
  191. FolderBrowserDialog dlg = new FolderBrowserDialog();
  192. string saveSelectedPath = "";
  193. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  194. {
  195. saveSelectedPath = dlg.SelectedPath.Trim();
  196. saveSelectedPath = saveSelectedPath + "\\";
  197. }
  198. else { return; }
  199. FileNameIsCheck(saveSelectedPath);
  200. if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.CustomPageRange)
  201. {
  202. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref splitModel.PageRange, PageRangeText);
  203. if (splitModel.PageRange == "")
  204. {
  205. Trace.WriteLine("输入不对");
  206. MessageBoxEx.Show("输入不对");
  207. return;
  208. }
  209. char[] enumerationSeparator = new char[] { ',' };
  210. char[] rangeSeparator = new char[] { '-' };
  211. if (!CommonHelper.GetPagesInRange(ref splitModel.PageParm, splitModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  212. { //TODO
  213. Trace.WriteLine("输入不对");
  214. MessageBoxEx.Show("输入不对");
  215. return;
  216. }
  217. Trace.WriteLine("splitModel.PageRange: " + splitModel.PageRange + "-----");
  218. }
  219. if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.AveragePages)
  220. {
  221. if (AveragePagesText != "")
  222. {
  223. splitModel.GetModeCount = int.Parse(AveragePagesText);
  224. }
  225. int averagepagesremainder = 0;
  226. var averagepagesdocs = HomePageEditHelper.AverageSplitPages(currentViewer.Document.PageCount, splitModel.GetModeCount, ref averagepagesremainder);
  227. int averagepagesdoc;
  228. for (averagepagesdoc = 1; averagepagesdoc < averagepagesdocs; averagepagesdoc++)
  229. {
  230. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  231. extractdoc.ImportPages(currentViewer.Document, (1 + splitModel.GetModeCount * (averagepagesdoc - 1)).ToString() + "-" + (splitModel.GetModeCount * averagepagesdoc).ToString());
  232. string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagepagesdoc.ToString() + ".pdf");
  233. extractdoc.WriteToFilePath(createfilepath);
  234. extractdoc.Release();
  235. }
  236. if (averagepagesdoc == averagepagesdocs)
  237. {
  238. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  239. extractdoc.ImportPages(currentViewer.Document, (1 + splitModel.GetModeCount * (averagepagesdoc - 1)).ToString() + "-" + currentViewer.Document.PageCount.ToString());
  240. string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagepagesdoc.ToString() + ".pdf");
  241. extractdoc.WriteToFilePath(createfilepath);
  242. }
  243. }
  244. if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.AverageFiles)
  245. {
  246. if (AverageFilesText != "")
  247. {
  248. splitModel.GetModeCount = int.Parse(AverageFilesText);
  249. }
  250. int averagefilesremainder = 0;
  251. var averagefilespages = HomePageEditHelper.AverageSplitDocs(currentViewer.Document.PageCount, splitModel.GetModeCount, ref averagefilesremainder);
  252. int averagefilesdoc;
  253. if (splitModel.GetModeCount > currentViewer.Document.PageCount)
  254. {
  255. splitModel.GetModeCount = currentViewer.Document.PageCount;
  256. }
  257. for (averagefilesdoc = 1; averagefilesdoc <= splitModel.GetModeCount; averagefilesdoc++)
  258. {
  259. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  260. if (averagefilesremainder > 0)
  261. {
  262. extractdoc.ImportPages(currentViewer.Document, (1 + (averagefilespages + 1) * (averagefilesdoc - 1)).ToString() + "-" + ((averagefilespages + 1) * averagefilesdoc).ToString());
  263. string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagefilesdoc.ToString() + ".pdf");
  264. extractdoc.WriteToFilePath(createfilepath);
  265. averagefilesremainder--;
  266. }
  267. else
  268. {
  269. extractdoc.ImportPages(currentViewer.Document, (averagefilespages * averagefilesremainder + averagefilesremainder + 1 + averagefilespages * (averagefilesdoc - 1)).ToString() + "-" + (averagefilespages * averagefilesremainder + averagefilesremainder + (averagefilespages * averagefilesdoc)).ToString());
  270. string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagefilesdoc.ToString() + ".pdf");
  271. extractdoc.WriteToFilePath(createfilepath);
  272. }
  273. extractdoc.Release();
  274. }
  275. }
  276. if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.CustomPageRange)
  277. {
  278. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  279. extractdoc.ImportPages(currentViewer.Document, splitModel.PageRange);
  280. string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + "1" + ".pdf");
  281. extractdoc.WriteToFilePath(createfilepath);
  282. if (splitModel.PageRange != "1-" + currentViewer.Document.PageCount.ToString())
  283. {
  284. currentViewer.Document.RemovePages(splitModel.PageParm.ToArray());
  285. createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + "2" + ".pdf");
  286. currentViewer.Document.WriteToFilePath(createfilepath);
  287. }
  288. }
  289. Process.Start(saveSelectedPath);
  290. RequestClose.Invoke(new DialogResult(ButtonResult.OK));
  291. }
  292. private void averagePages()
  293. {
  294. CustomPageRangeIsCheck = "False";
  295. AverageFilesIsCheck = "False";
  296. AveragePagesIsCheck = "True";
  297. CustomPageRangeIsEnabled = "False";
  298. AverageFilesIsEnabled = "False";
  299. AveragePagesIsEnabled = "True";
  300. splitModel.Mode = HomePageSplitDialogModel.SplitMode.AveragePages;
  301. }
  302. private void averageFiles()
  303. {
  304. CustomPageRangeIsCheck = "False";
  305. AverageFilesIsCheck = "True";
  306. AveragePagesIsCheck = "False";
  307. CustomPageRangeIsEnabled = "False";
  308. AverageFilesIsEnabled = "True";
  309. AveragePagesIsEnabled = "False";
  310. splitModel.Mode = HomePageSplitDialogModel.SplitMode.AverageFiles;
  311. }
  312. private void customPageRange()
  313. {
  314. CustomPageRangeIsCheck = "True";
  315. AverageFilesIsCheck = "False";
  316. AveragePagesIsCheck = "False";
  317. CustomPageRangeIsEnabled = "True";
  318. AverageFilesIsEnabled = "False";
  319. AveragePagesIsEnabled = "False";
  320. splitModel.Mode = HomePageSplitDialogModel.SplitMode.CustomPageRange;
  321. }
  322. private void frontFileName()
  323. {
  324. string filenamelabeltext = "";
  325. string filenamedeimitertext = "";
  326. if (FileNameLabelIsCheck == "False")
  327. {
  328. filenamelabeltext = "";
  329. }
  330. else
  331. {
  332. filenamelabeltext = FileNameLabelText;
  333. }
  334. if (FileNameDeimiterIsCheck == "False")
  335. {
  336. filenamedeimitertext = "";
  337. }
  338. else
  339. {
  340. filenamedeimitertext = FileNameDeimiterText;
  341. }
  342. if (FrontFileIsCheck == "True")
  343. {
  344. FilePathName = savefilename + filenamedeimitertext + filenamelabeltext;
  345. splitModel.FrontFileName = true;
  346. }
  347. else
  348. {
  349. splitModel.FrontFileName = false;
  350. FilePathName = filenamelabeltext + filenamedeimitertext + savefilename;
  351. }
  352. }
  353. private void fileNameLabel()
  354. {
  355. string filenamelabeltext = "";
  356. string filenamedeimitertext = "";
  357. if (FileNameLabelIsCheck == "False")
  358. {
  359. filenamelabeltext = "";
  360. }
  361. else
  362. {
  363. filenamelabeltext = FileNameLabelText;
  364. }
  365. if (FileNameDeimiterIsCheck == "False")
  366. {
  367. filenamedeimitertext = "";
  368. }
  369. else
  370. {
  371. filenamedeimitertext = FileNameDeimiterText;
  372. }
  373. if (FileNameLabelIsCheck == "True")
  374. {
  375. FileNameLabelIsEnabled = "True";
  376. if (FrontFileIsCheck == "True")
  377. {
  378. FilePathName = savefilename + filenamedeimitertext + filenamelabeltext;
  379. splitModel.FrontFileName = true;
  380. }
  381. else
  382. {
  383. splitModel.FrontFileName = false;
  384. FilePathName = filenamelabeltext + filenamedeimitertext + savefilename;
  385. }
  386. }
  387. else
  388. {
  389. FileNameLabelIsEnabled = "False";
  390. if (FrontFileIsCheck == "True")
  391. {
  392. FilePathName = savefilename + filenamedeimitertext;
  393. splitModel.FrontFileName = true;
  394. }
  395. else
  396. {
  397. splitModel.FrontFileName = false;
  398. FilePathName = filenamedeimitertext + savefilename;
  399. }
  400. }
  401. }
  402. private void fileNameDeimiter()
  403. {
  404. string filenamelabeltext = "";
  405. string filenamedeimitertext = "";
  406. if (FileNameLabelIsCheck == "False")
  407. {
  408. filenamelabeltext = "";
  409. }
  410. else
  411. {
  412. filenamelabeltext = FileNameLabelText;
  413. }
  414. if (FileNameDeimiterIsCheck == "False")
  415. {
  416. filenamedeimitertext = "";
  417. }
  418. else
  419. {
  420. filenamedeimitertext = FileNameDeimiterText;
  421. }
  422. if (FileNameDeimiterIsCheck == "True")
  423. {
  424. FileNameDeimiterIsEnabled = "True";
  425. if (FrontFileIsCheck == "True")
  426. {
  427. FilePathName = savefilename + filenamedeimitertext + filenamelabeltext;
  428. }
  429. else
  430. {
  431. FilePathName = filenamelabeltext + filenamedeimitertext + savefilename;
  432. }
  433. }
  434. else
  435. {
  436. FileNameDeimiterIsEnabled = "False";
  437. if (FrontFileIsCheck == "True")
  438. {
  439. FilePathName = savefilename + filenamelabeltext;
  440. }
  441. else
  442. {
  443. FilePathName = filenamelabeltext + savefilename;
  444. }
  445. }
  446. }
  447. private void CmbPageSelectionChangedEvent(object e)
  448. {
  449. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  450. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  451. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  452. var SplitPreview = e as PageTurningPreview;
  453. if (SplitPreview != null)
  454. {
  455. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  456. var currentViewer = this.currentViewer;
  457. string PageRange = "";
  458. var PageRangeText = this.PageRangeText;
  459. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "1" || PageRangeSelectIndex == "2")
  460. {
  461. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText);
  462. char[] enumerationSeparator = new char[] { ',' };
  463. char[] rangeSeparator = new char[] { '-' };
  464. if (!CommonHelper.GetPagesInRange(ref SplitPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  465. { //TODO
  466. SplitPreview.PageIndexLists.Add(0);
  467. Trace.WriteLine("输入不对");
  468. MessageBoxEx.Show("输入不对");
  469. return;
  470. }
  471. }
  472. SplitPreview.PageIndex.Text = (SplitPreview.PageIndexLists.Last<int>() + 1).ToString();
  473. SplitPreview.CurrentIndex = 0;
  474. SplitPreview.CurrentPage.Text = (SplitPreview.PageIndexLists[SplitPreview.CurrentIndex] + 1).ToString();
  475. SplitPreview.AwaitRenderBitmap(SplitPreview.document);
  476. }
  477. }
  478. private void CmbPageTextChangedEvent(object e)
  479. {
  480. var SplitPreview = e as PageTurningPreview;
  481. if (SplitPreview != null)
  482. {
  483. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  484. var currentViewer = this.currentViewer;
  485. string PageRange = "";
  486. var PageRangeText = this.PageRangeText;
  487. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText);
  488. char[] enumerationSeparator = new char[] { ',' };
  489. char[] rangeSeparator = new char[] { '-' };
  490. if (!CommonHelper.GetPagesInRange(ref SplitPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  491. { //TODO
  492. SplitPreview.PageIndexLists.Add(0);
  493. return;
  494. }
  495. else
  496. {
  497. SplitPreview.PageIndex.Text = (SplitPreview.PageIndexLists.Last<int>() + 1).ToString();
  498. SplitPreview.CurrentIndex = 0;
  499. SplitPreview.CurrentPage.Text = (SplitPreview.PageIndexLists[SplitPreview.CurrentIndex] + 1).ToString();
  500. SplitPreview.AwaitRenderBitmap(SplitPreview.document);
  501. }
  502. }
  503. }
  504. #endregion
  505. #region 构架行为
  506. public string Title => "";
  507. public event Action<IDialogResult> RequestClose;
  508. public bool CanCloseDialog()
  509. {
  510. return true;
  511. }
  512. public void OnDialogClosed()
  513. {
  514. }
  515. public void OnDialogOpened(IDialogParameters parameters)
  516. {
  517. CPDFViewer viewer = null;
  518. string filepath = "";
  519. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
  520. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  521. if (viewer != null && viewer.Document != null)
  522. {
  523. currentViewer = viewer;
  524. MaxPageRange = viewer.Document.PageCount;
  525. document = viewer.Document;
  526. savefilename = currentViewer.Document.FileName;
  527. FilePathName = savefilename + "-" + "part";
  528. savefilepath = filepath.Replace(savefilename + ".pdf", "");
  529. }
  530. }
  531. #endregion
  532. }
  533. }