WatermarkCreateFileContentViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using ComPDFKit.PDFWatermark;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.EventAggregators;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.EditTools.Watermark;
  8. using PDF_Office.Model.PageEdit;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Windows;
  19. namespace PDF_Office.ViewModels.EditTools.Watermark
  20. {
  21. public class WatermarkCreateFileContentViewModel : BindableBase, INavigationAware
  22. {
  23. public WatermarkInfo WatermarkInfo = new WatermarkInfo();
  24. private CPDFViewer PDFViewer;
  25. IEventAggregator eventAggregator;
  26. private List<string> _opacityList = new List<string>();
  27. public List<string> OpacityList
  28. {
  29. get { return _opacityList; }
  30. set
  31. {
  32. SetProperty(ref _opacityList, value);
  33. }
  34. }
  35. private void InitOpacityList()
  36. {
  37. OpacityList.Clear();
  38. for (int temp = 0; temp <= 100; temp += 10)
  39. {
  40. OpacityList.Add(temp.ToString() + " %");
  41. }
  42. }
  43. private List<string> _rotationList = new List<string>();
  44. public List<string> RotationList
  45. {
  46. get { return _rotationList; }
  47. set
  48. {
  49. SetProperty(ref _rotationList, value);
  50. }
  51. }
  52. private void InitRotationList()
  53. {
  54. RotationList.Clear();
  55. for (int temp = -45; temp <= 45; temp += 15)
  56. {
  57. RotationList.Add(temp.ToString());
  58. }
  59. }
  60. private List<string> _scaleList = new List<string>();
  61. public List<string> ScaleList
  62. {
  63. get { return _scaleList; }
  64. set
  65. {
  66. SetProperty(ref _scaleList, value);
  67. }
  68. }
  69. private void InitScaleList()
  70. {
  71. ScaleList.Clear();
  72. for (int temp = 0; temp <= 100; temp += 10)
  73. {
  74. ScaleList.Add(temp.ToString() + " %");
  75. }
  76. }
  77. private List<string> _isFrontList = new List<string>();
  78. public List<string> IsFrontList
  79. {
  80. get { return _isFrontList; }
  81. set
  82. {
  83. SetProperty(ref _isFrontList, value);
  84. }
  85. }
  86. private void InitIsFrontList()
  87. {
  88. IsFrontList.Clear();
  89. IsFrontList.Add("位于页面上方");
  90. IsFrontList.Add("位于页面下方");
  91. }
  92. private int _rotationValue = 0;
  93. public int RotationValue
  94. {
  95. get { return _rotationValue; }
  96. set
  97. {
  98. SetProperty(ref _rotationValue, value);
  99. WatermarkInfo.Rotation = (float)RotationValue;
  100. }
  101. }
  102. private int _opacityValue = 100;
  103. public int OpacityValue
  104. {
  105. get { return _opacityValue; }
  106. set
  107. {
  108. SetProperty(ref _opacityValue, value);
  109. WatermarkInfo.Opacity = (byte)((float)(OpacityValue / 100) * 225);
  110. }
  111. }
  112. private int _relativeScaleValue = 50;
  113. public int RelativeScaleValue
  114. {
  115. get { return _relativeScaleValue; }
  116. set
  117. {
  118. SetProperty(ref _relativeScaleValue, value);
  119. }
  120. }
  121. private string _vertOffsetValue = "0";
  122. public string VertOffsetValue
  123. {
  124. get { return _vertOffsetValue; }
  125. set
  126. {
  127. SetProperty(ref _vertOffsetValue, value);
  128. WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
  129. }
  130. }
  131. private string _horizOffsetValue = "0";
  132. public string HorizOffsetValue
  133. {
  134. get { return _horizOffsetValue; }
  135. set
  136. {
  137. SetProperty(ref _horizOffsetValue, value);
  138. WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
  139. }
  140. }
  141. private string _verticalSpacingValue = "6";
  142. public string VerticalSpacingValue
  143. {
  144. get { return _verticalSpacingValue; }
  145. set
  146. {
  147. SetProperty(ref _verticalSpacingValue, value);
  148. WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
  149. }
  150. }
  151. private string _horizontalSpacingValue = "6";
  152. public string HorizontalSpacingValue
  153. {
  154. get { return _horizontalSpacingValue; }
  155. set
  156. {
  157. SetProperty(ref _horizontalSpacingValue, value);
  158. WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
  159. }
  160. }
  161. public string PageRangeText { get; set; } = "0";
  162. private int _pageRangeSelectIndex = 0;
  163. public int PageRangeSelectIndex
  164. {
  165. get { return _pageRangeSelectIndex; }
  166. set
  167. {
  168. SetProperty(ref _pageRangeSelectIndex, value);
  169. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  170. }
  171. }
  172. private int _isFrontSelectedIndex = 0;
  173. public int IsFrontSelectedIndex
  174. {
  175. get { return _isFrontSelectedIndex; }
  176. set
  177. {
  178. SetProperty(ref _isFrontSelectedIndex, value);
  179. SetIsFront(IsFrontSelectedIndex);
  180. }
  181. }
  182. private string _fileNameText = "";
  183. public string FileNameText
  184. {
  185. get { return _fileNameText; }
  186. set
  187. {
  188. SetProperty(ref _fileNameText, value);
  189. }
  190. }
  191. private Visibility _creatFileVisible = Visibility.Collapsed;
  192. public Visibility CreatFileVisible
  193. {
  194. get { return _creatFileVisible; }
  195. set { SetProperty(ref _creatFileVisible, value); }
  196. }
  197. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  198. public ObservableDictionary<string, bool> GetLocationFromNumber
  199. {
  200. get { return _getLocationFromNumber; }
  201. set { _getLocationFromNumber = value; }
  202. }
  203. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  204. public DelegateCommand OpenFileCommand { get; set; }
  205. public WatermarkCreateFileContentViewModel(IEventAggregator eventAggregator)
  206. {
  207. this.eventAggregator = eventAggregator;
  208. WatermarkInfo.WatermarkType=C_Watermark_Type.WATERMARK_TYPE_IMG;
  209. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  210. OpenFileCommand = new DelegateCommand(OpenFile);
  211. InitList();
  212. WatermarkInfo.WatermarkHorizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER;
  213. WatermarkInfo.WatermarkVertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER;
  214. InitLocationButtonMatrix();
  215. eventAggregator.GetEvent<ConfirmEditToolsWatermarkEvent>().Subscribe(ConfirmEditToolsWatermark);
  216. }
  217. private void SetIsFront(int Index)
  218. {
  219. if (Index == 0)
  220. {
  221. WatermarkInfo.IsFront = false;
  222. }
  223. if (Index == 1)
  224. {
  225. WatermarkInfo.IsFront = true;
  226. }
  227. }
  228. private void InitList()
  229. {
  230. InitOpacityList();
  231. InitIsFrontList();
  232. InitRotationList();
  233. InitScaleList();
  234. }
  235. private void InitLocationButtonMatrix()
  236. {
  237. GetLocationFromNumber.Clear();
  238. for (var temp = 0; temp <= 22; temp++)
  239. {
  240. GetLocationFromNumber.Add(temp.ToString(), true);
  241. if (temp % 10 == 2)
  242. {
  243. temp += 7;
  244. }
  245. }
  246. int Num = (int)WatermarkInfo.WatermarkVertalign * 10 + (int)WatermarkInfo.WatermarkHorizalign;
  247. GetLocationFromNumber[Num.ToString()] = false;
  248. }
  249. public void ChangeLocation(object e)
  250. {
  251. string args = e as string;
  252. if (args != null)
  253. {
  254. WatermarkInfo.WatermarkVertalign = (C_Watermark_Vertalign)(int.Parse(args) / 10);
  255. WatermarkInfo.WatermarkHorizalign = (C_Watermark_Horizalign)(int.Parse(args) % 10);
  256. InitLocationButtonMatrix();
  257. }
  258. }
  259. public void ConfirmEditToolsWatermark(EnumTextOrFile enumTextOrFile)
  260. {
  261. if (enumTextOrFile == EnumTextOrFile.StatusFile)
  262. {
  263. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo);
  264. }
  265. }
  266. public void OpenFile()
  267. {
  268. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  269. dlg.Multiselect = false;
  270. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  271. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  272. {
  273. FileNameText = dlg.SafeFileName;
  274. FileInfo file = new FileInfo(dlg.FileName);
  275. if (file.Extension == ".pdf")
  276. {
  277. EditToolsHelper.ChooseFile(dlg.FileName, ref WatermarkInfo, PDFViewer.Document);
  278. }
  279. else
  280. {
  281. EditToolsHelper.ChooseFile(dlg.FileName, ref WatermarkInfo);
  282. }
  283. CreatFileVisible=Visibility.Visible;
  284. }
  285. }
  286. public bool IsNavigationTarget(NavigationContext navigationContext)
  287. {
  288. return true;
  289. }
  290. public void OnNavigatedFrom(NavigationContext navigationContext)
  291. {
  292. }
  293. public void OnNavigatedTo(NavigationContext navigationContext)
  294. {
  295. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  296. }
  297. }
  298. }