WatermarkCreateFileContentViewModel.cs 9.0 KB

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