HomePageInsertDialogViewModel.cs 7.2 KB

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