HomePageInsertDialogViewModel.cs 7.4 KB

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