HomePageExtractDialogViewModel.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs
  17. {
  18. public class HomePageExtractDialogViewModel : BindableBase, IDialogAware
  19. {
  20. #region 参数和属性
  21. public CPDFDocument document;
  22. public CPDFViewer currentViewer;
  23. private HomePageExtractDialogModel extractModel = new HomePageExtractDialogModel();
  24. private string savefilepath { get; set; }
  25. public string ExtractToSingleFileIsCheck {get;set;} = "False";
  26. public string DeleteAfterExtractIsCheck { get; set; } = "Fales";
  27. public string PageRangeText { get; set; } = "";
  28. public string PageRangeSelectIndex { get; set; } = "";
  29. #endregion
  30. #region 委托声明
  31. public DelegateCommand CancelCommand { get; set; }
  32. public DelegateCommand ExtractCommand { get; set; }
  33. public DelegateCommand ExtractToSingleFileCommand { get; set; }
  34. public DelegateCommand DeleteAfterExtractCommand { get; set; }
  35. #endregion
  36. public HomePageExtractDialogViewModel()
  37. {
  38. CancelCommand = new DelegateCommand(cancel);
  39. ExtractCommand = new DelegateCommand(extract);
  40. ExtractToSingleFileCommand = new DelegateCommand(extractToSingleFile);
  41. DeleteAfterExtractCommand = new DelegateCommand(deleteAfterExtract);
  42. }
  43. #region 逻辑函数
  44. private void cancel()
  45. {
  46. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  47. }
  48. private void extract()
  49. {
  50. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref extractModel.PageRange, PageRangeText);
  51. char[] enumerationSeparator = new char[] { ',' };
  52. char[] rangeSeparator = new char[] { '-' };
  53. if (!CommonHelper.GetPagesInRange(ref extractModel.PageParm, extractModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  54. { //TODO
  55. Trace.WriteLine("输入不对");
  56. MessageBoxEx.Show("输入不对");
  57. return;
  58. }
  59. if (extractModel.ExtractToSingleFile)
  60. {
  61. for (int i = 0; i < extractModel.PageParm.ToArray().Length; i++)
  62. {
  63. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  64. extractdoc.ImportPages(currentViewer.Document, (extractModel.PageParm.ToArray()[i] + 1).ToString());
  65. Trace.WriteLine(extractModel.PageParm.ToArray()[i].ToString());
  66. extractdoc.WriteToFilePath(savefilepath + currentViewer.Document.FileName + ".extract" + i.ToString() + ".pdf");
  67. extractdoc.Release();
  68. }
  69. if (extractModel.DeleteAfterExtract)
  70. {
  71. currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
  72. currentViewer.Document.WriteToLoadedPath();
  73. }
  74. }
  75. else
  76. {
  77. CPDFDocument extractdoc = CPDFDocument.CreateDocument();
  78. extractdoc.ImportPages(currentViewer.Document, extractModel.PageRange);
  79. extractdoc.WriteToFilePath(savefilepath + currentViewer.Document.FileName + ".extract.pdf");
  80. if (extractModel.DeleteAfterExtract)
  81. {
  82. currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
  83. currentViewer.Document.WriteToLoadedPath();
  84. }
  85. extractdoc.Release();
  86. }
  87. System.Diagnostics.Process.Start("Explorer", "/select," + currentViewer.Document.FilePath);
  88. RequestClose.Invoke(new DialogResult(ButtonResult.OK));
  89. }
  90. private void extractToSingleFile()
  91. {
  92. if (ExtractToSingleFileIsCheck == "True")
  93. {
  94. extractModel.ExtractToSingleFile = true;
  95. Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
  96. }
  97. else
  98. {
  99. extractModel.ExtractToSingleFile = false;
  100. Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
  101. }
  102. }
  103. private void deleteAfterExtract()
  104. {
  105. if (DeleteAfterExtractIsCheck == "True")
  106. {
  107. extractModel.DeleteAfterExtract = true;
  108. Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
  109. }
  110. else
  111. {
  112. extractModel.DeleteAfterExtract = false;
  113. Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
  114. }
  115. }
  116. #endregion
  117. #region 框架行为
  118. public string Title => "";
  119. public event Action<IDialogResult> RequestClose;
  120. public bool CanCloseDialog()
  121. {
  122. return true;
  123. }
  124. public void OnDialogClosed()
  125. {
  126. }
  127. public void OnDialogOpened(IDialogParameters parameters)
  128. {
  129. CPDFViewer viewer = null;
  130. string filepath = "";
  131. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
  132. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  133. if (viewer != null && viewer.Document != null)
  134. {
  135. currentViewer = viewer;
  136. savefilepath = filepath.Replace(currentViewer.Document.FileName + ".pdf", "");
  137. document = currentViewer.Document;
  138. }
  139. }
  140. #endregion
  141. }
  142. }