HomePageInsertDialogViewModel.cs 6.7 KB

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