HomePageInsertDialogViewModel.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 System.Collections.Generic;
  9. using System.Linq;
  10. using PDF_Office.Model.HomePageToolsDialogs;
  11. using System.Diagnostics;
  12. using System.Globalization;
  13. using System.Windows;
  14. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
  15. using System.Windows.Forms;
  16. using DialogResult = Prism.Services.Dialogs.DialogResult;
  17. using PDF_Office.CustomControl;
  18. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs
  19. {
  20. public class HomePageInsertDialogViewModel : BindableBase,IDialogAware
  21. {
  22. private CPDFViewer currentViewer;
  23. private string saveFilePath { get; set; }
  24. private HomePageInsertDialogModel insertModel = new HomePageInsertDialogModel();
  25. public DelegateCommand CancelCommand { get; set; }
  26. public DelegateCommand InsertCommand { get; set; }
  27. public DelegateCommand SelectFileCommand { get; set; }
  28. public DelegateCommand FirstPageCommand { get; set; }
  29. public DelegateCommand LastPageCommand { get; set; }
  30. public DelegateCommand CustomPageCommand { get; set; }
  31. private string selectFilePath = "选择文件";
  32. public string SelectFilePath
  33. {
  34. get { return selectFilePath; }
  35. set
  36. {
  37. SetProperty(ref selectFilePath, value);
  38. }
  39. }
  40. private string pageLocation = "0";
  41. public string PageLocation
  42. {
  43. get { return pageLocation; }
  44. set
  45. {
  46. pageLocation = value;
  47. }
  48. }
  49. private string pageNumber="1";
  50. public string PageNumber
  51. {
  52. get { return pageNumber; }
  53. set
  54. {
  55. SetProperty(ref pageNumber, value);
  56. }
  57. }
  58. private string pageInsertIndex = "1";
  59. public string PageInsertIndex
  60. {
  61. get {
  62. return pageInsertIndex; }
  63. set
  64. {
  65. if (int.Parse(pageInsertIndex) > currentViewer.Document.PageCount)
  66. { pageInsertIndex= currentViewer.Document.PageCount.ToString(); }
  67. SetProperty(ref pageInsertIndex, value);
  68. }
  69. }
  70. private string firstIsCheck = "True";
  71. public string FirstIsCheck
  72. {
  73. get { return firstIsCheck; }
  74. set
  75. {
  76. SetProperty(ref firstIsCheck, value);
  77. }
  78. }
  79. private string lastIsCheck = "False";
  80. public string LastIsCheck
  81. {
  82. get { return lastIsCheck; }
  83. set
  84. {
  85. SetProperty(ref lastIsCheck, value);
  86. }
  87. }
  88. private string customIsCheck = "False";
  89. public string CustomIsCheck
  90. {
  91. get { return customIsCheck; }
  92. set
  93. {
  94. SetProperty(ref customIsCheck, value);
  95. }
  96. }
  97. private string customIsEnabled = "False";
  98. public string CustomIsEnabled
  99. {
  100. get { return customIsEnabled; }
  101. set
  102. {
  103. SetProperty(ref customIsEnabled, value);
  104. }
  105. }
  106. public HomePageInsertDialogViewModel()
  107. {
  108. CancelCommand = new DelegateCommand(cancel);
  109. InsertCommand = new DelegateCommand(insert);
  110. SelectFileCommand = new DelegateCommand(selectFile);
  111. FirstPageCommand = new DelegateCommand(firstPage);
  112. LastPageCommand = new DelegateCommand(lastPage);
  113. CustomPageCommand = new DelegateCommand(customPage);
  114. }
  115. private void cancel()
  116. {
  117. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  118. }
  119. private void insert()
  120. {
  121. CPDFDocument insertdocument = CPDFDocument.InitWithFilePath(insertModel.FilePath);
  122. if (insertdocument == null)
  123. {
  124. Trace.WriteLine("Document==null");
  125. //TODO
  126. return;
  127. }
  128. if (insertdocument.IsEncrypted) {
  129. Trace.WriteLine("youmima");
  130. //TODO
  131. return;
  132. }
  133. if (PageLocation == "1") {
  134. insertModel.InsertIndex=insertModel.InsertIndex - 1;
  135. }
  136. currentViewer.Document.ImportPagesAtIndex(insertdocument, insertModel.InserPageRange(insertdocument), insertModel.InsertIndex);
  137. currentViewer.UndoManager.ClearHistory();
  138. currentViewer.UndoManager.CanSave = true;
  139. currentViewer.ReloadDocument();
  140. currentViewer.Document.WriteToLoadedPath();
  141. insertdocument.Release();
  142. System.Diagnostics.Process.Start("Explorer", "/select," + currentViewer.Document.FilePath);
  143. RequestClose.Invoke(new DialogResult(ButtonResult.OK));
  144. }
  145. private void selectFile() {
  146. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  147. dlg.Multiselect = false;
  148. dlg.Filter = "PDF|*.pdf;*.PDF;";
  149. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  150. {
  151. insertModel.FilePath= dlg.FileName;
  152. SelectFilePath = dlg.FileName;
  153. }
  154. }
  155. private void firstPage()
  156. {
  157. insertModel.InsertIndex = 1;
  158. CustomIsEnabled = "False";
  159. FirstIsCheck = "True";
  160. LastIsCheck = "False";
  161. CustomIsCheck = "False";
  162. }
  163. private void lastPage()
  164. {
  165. insertModel.InsertIndex = currentViewer.Document.PageCount;
  166. CustomIsEnabled = "False";
  167. FirstIsCheck = "False";
  168. LastIsCheck = "True";
  169. CustomIsCheck = "False";
  170. }
  171. private void customPage()
  172. {
  173. CustomIsEnabled = "True";
  174. FirstIsCheck = "False";
  175. LastIsCheck = "False";
  176. CustomIsCheck = "True";
  177. }
  178. public string Title => "";
  179. public event Action<IDialogResult> RequestClose;
  180. public bool CanCloseDialog()
  181. {
  182. return true;
  183. }
  184. public void OnDialogClosed()
  185. {
  186. }
  187. public void OnDialogOpened(IDialogParameters parameters)
  188. {
  189. CPDFViewer viewer = null;
  190. string filepath = "";
  191. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
  192. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  193. if (viewer != null && viewer.Document != null)
  194. {
  195. currentViewer = viewer;
  196. saveFilePath = filepath;
  197. PageNumber=currentViewer.Document.PageCount.ToString();
  198. PageTurningPreview.document = currentViewer.Document;
  199. }
  200. }
  201. }
  202. }