HomePageExtractDialogViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 SQLitePCL;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using PDF_Master.Model.HomePageToolsDialogs;
  12. using PDF_Master.Helper;
  13. using System.Diagnostics;
  14. using PDF_Master.CustomControl;
  15. using PDF_Master.Model.Dialog.HomePageToolsDialogs;
  16. using ImTools;
  17. using System.Windows.Forms;
  18. using DialogResult = Prism.Services.Dialogs.DialogResult;
  19. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
  20. {
  21. public class HomePageExtractDialogViewModel : BindableBase, IDialogAware
  22. {
  23. #region 参数和属性
  24. public CPDFDocument document;
  25. public CPDFViewer currentViewer;
  26. private HomePageExtractDialogModel extractModel = new HomePageExtractDialogModel();
  27. private string savefilepath { get; set; }
  28. public string ExtractToSingleFileIsCheck {get;set;} = "False";
  29. public string DeleteAfterExtractIsCheck { get; set; } = "Fales";
  30. public string PageRangeText { get; set; } = "";
  31. public string PageRangeSelectIndex { get; set; } = "0";
  32. private int maxPageRange = 0;
  33. public int MaxPageRange
  34. {
  35. get { return maxPageRange; }
  36. set
  37. {
  38. SetProperty(ref maxPageRange, value);
  39. }
  40. }
  41. #endregion
  42. #region 委托声明
  43. public DelegateCommand CancelCommand { get; set; }
  44. public DelegateCommand ExtractCommand { get; set; }
  45. public DelegateCommand ExtractToSingleFileCommand { get; set; }
  46. public DelegateCommand DeleteAfterExtractCommand { get; set; }
  47. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  48. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  49. #endregion
  50. public HomePageExtractDialogViewModel()
  51. {
  52. CancelCommand = new DelegateCommand(cancel);
  53. ExtractCommand = new DelegateCommand(extract);
  54. ExtractToSingleFileCommand = new DelegateCommand(extractToSingleFile);
  55. DeleteAfterExtractCommand = new DelegateCommand(deleteAfterExtract);
  56. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  57. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  58. }
  59. #region 逻辑函数
  60. private void cancel()
  61. {
  62. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  63. }
  64. private void extract()
  65. {
  66. FolderBrowserDialog dlg = new FolderBrowserDialog();
  67. string saveSelectedPath = "";
  68. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  69. {
  70. saveSelectedPath = dlg.SelectedPath.Trim();
  71. saveSelectedPath = saveSelectedPath + "\\";
  72. }
  73. else { return; }
  74. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref extractModel.PageRange, PageRangeText);
  75. if (extractModel.PageRange == "")
  76. {
  77. Trace.WriteLine("输入不对");
  78. MessageBoxEx.Show("输入不对");
  79. return;
  80. }
  81. char[] enumerationSeparator = new char[] { ',' };
  82. char[] rangeSeparator = new char[] { '-' };
  83. if (!CommonHelper.GetPagesInRange(ref extractModel.PageParm, extractModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  84. { //TODO
  85. Trace.WriteLine("输入不对");
  86. MessageBoxEx.Show("输入不对");
  87. return;
  88. }
  89. if (extractModel.ExtractToSingleFile)
  90. {
  91. for (int i = 0; i < extractModel.PageParm.ToArray().Length; i++)
  92. {
  93. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  94. extractdoc.ImportPages(currentViewer.Document, (extractModel.PageParm.ToArray()[i] + 1).ToString());
  95. Trace.WriteLine(extractModel.PageParm.ToArray()[i].ToString());
  96. string createfilepath= CommonHelper.CreateFilePath(saveSelectedPath + currentViewer.Document.FileName + ".extract" + i.ToString() + ".pdf");
  97. extractdoc.WriteToFilePath(createfilepath);
  98. extractdoc.Release();
  99. }
  100. if (extractModel.DeleteAfterExtract)
  101. {
  102. currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
  103. currentViewer.Document.WriteToLoadedPath();
  104. }
  105. }
  106. else
  107. {
  108. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  109. extractdoc.ImportPages(currentViewer.Document, extractModel.PageRange);
  110. string createfilepath = CommonHelper.CreateFilePath(saveSelectedPath + currentViewer.Document.FileName + ".extract.pdf");
  111. extractdoc.WriteToFilePath(createfilepath);
  112. if (extractModel.DeleteAfterExtract)
  113. {
  114. currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
  115. currentViewer.Document.WriteToLoadedPath();
  116. }
  117. extractdoc.Release();
  118. }
  119. Process.Start(saveSelectedPath);
  120. RequestClose.Invoke(new DialogResult(ButtonResult.OK));
  121. }
  122. private void extractToSingleFile()
  123. {
  124. if (ExtractToSingleFileIsCheck == "True")
  125. {
  126. extractModel.ExtractToSingleFile = true;
  127. Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
  128. }
  129. else
  130. {
  131. extractModel.ExtractToSingleFile = false;
  132. Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
  133. }
  134. }
  135. private void deleteAfterExtract()
  136. {
  137. if (DeleteAfterExtractIsCheck == "True")
  138. {
  139. extractModel.DeleteAfterExtract = true;
  140. Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
  141. }
  142. else
  143. {
  144. extractModel.DeleteAfterExtract = false;
  145. Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
  146. }
  147. }
  148. private void CmbPageSelectionChangedEvent(object e)
  149. {
  150. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  151. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  152. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  153. var ExtractPreview = e as PageTurningPreview;
  154. if (ExtractPreview != null)
  155. {
  156. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  157. var currentViewer = this.currentViewer;
  158. string PageRange = "";
  159. var PageRangeText = this.PageRangeText;
  160. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "1" || PageRangeSelectIndex == "2")
  161. {
  162. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText);
  163. char[] enumerationSeparator = new char[] { ',' };
  164. char[] rangeSeparator = new char[] { '-' };
  165. if (!CommonHelper.GetPagesInRange(ref ExtractPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  166. { //TODO
  167. ExtractPreview.PageIndexLists.Add(0);
  168. Trace.WriteLine("输入不对");
  169. MessageBoxEx.Show("输入不对");
  170. return;
  171. }
  172. }
  173. ExtractPreview.PageIndex.Text = (ExtractPreview.PageIndexLists.Last<int>() + 1).ToString();
  174. ExtractPreview.CurrentIndex = 0;
  175. ExtractPreview.CurrentPage.Text = (ExtractPreview.PageIndexLists[ExtractPreview.CurrentIndex] + 1).ToString();
  176. ExtractPreview.AwaitRenderBitmap(ExtractPreview.document);
  177. }
  178. }
  179. private void CmbPageTextChangedEvent(object e)
  180. {
  181. var ExtractPreview = e as PageTurningPreview;
  182. if (ExtractPreview != null)
  183. {
  184. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  185. var currentViewer = this.currentViewer;
  186. string PageRange = "";
  187. var PageRangeText = this.PageRangeText;
  188. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText);
  189. char[] enumerationSeparator = new char[] { ',' };
  190. char[] rangeSeparator = new char[] { '-' };
  191. if (!CommonHelper.GetPagesInRange(ref ExtractPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  192. { //TODO
  193. ExtractPreview.PageIndexLists.Add(0);
  194. return;
  195. }
  196. else
  197. {
  198. ExtractPreview.PageIndex.Text = (ExtractPreview.PageIndexLists.Last<int>() + 1).ToString();
  199. ExtractPreview.CurrentIndex = 0;
  200. ExtractPreview.CurrentPage.Text = (ExtractPreview.PageIndexLists[ExtractPreview.CurrentIndex] + 1).ToString();
  201. ExtractPreview.AwaitRenderBitmap(ExtractPreview.document);
  202. }
  203. }
  204. }
  205. #endregion
  206. #region 框架行为
  207. public string Title => "";
  208. public event Action<IDialogResult> RequestClose;
  209. public bool CanCloseDialog()
  210. {
  211. return true;
  212. }
  213. public void OnDialogClosed()
  214. {
  215. }
  216. public void OnDialogOpened(IDialogParameters parameters)
  217. {
  218. CPDFViewer viewer = null;
  219. string filepath = "";
  220. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
  221. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  222. if (viewer != null && viewer.Document != null)
  223. {
  224. currentViewer = viewer;
  225. MaxPageRange = viewer.Document.PageCount;
  226. savefilepath = filepath.Replace(currentViewer.Document.FileName + ".pdf", "");
  227. document = currentViewer.Document;
  228. }
  229. }
  230. #endregion
  231. }
  232. }