HomePageInsertDialogViewModel.cs 6.6 KB

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