HomePageExtractDialogViewModel.cs 10.0 KB

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