HomePageInsertDialogViewModel.cs 7.4 KB

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