HomePageInsertDialogViewModel.cs 7.1 KB

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