HomePageSplitDialogViewModel.cs 21 KB

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