WatermarkCreateTextContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFWatermark;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.EventAggregators;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model.EditTools.Background;
  7. using PDF_Office.Model.EditTools.Watermark;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. namespace PDF_Office.ViewModels.EditTools.Watermark
  16. {
  17. public class WatermarkCreateTextContentViewModel : BindableBase,INavigationAware
  18. {
  19. public WatermarkInfo WatermarkInfo = new WatermarkInfo();
  20. IEventAggregator eventAggregator;
  21. public CPDFViewer currentViewer;
  22. private List<string> _opacityList = new List<string>();
  23. public List<string> OpacityList
  24. {
  25. get { return _opacityList; }
  26. set
  27. {
  28. SetProperty(ref _opacityList, value);
  29. }
  30. }
  31. private void InitOpacityList()
  32. {
  33. OpacityList.Clear();
  34. for (int temp = 0; temp <= 100; temp += 10)
  35. {
  36. OpacityList.Add(temp.ToString() + " %");
  37. }
  38. }
  39. private List<string> _rotationList = new List<string>();
  40. public List<string> RotationList
  41. {
  42. get { return _rotationList; }
  43. set
  44. {
  45. SetProperty(ref _rotationList, value);
  46. }
  47. }
  48. private void InitRotationList()
  49. {
  50. RotationList.Clear();
  51. for (int temp = -45; temp <= 45; temp += 15)
  52. {
  53. RotationList.Add(temp.ToString());
  54. }
  55. }
  56. private List<string> _fontNameList = new List<string>();
  57. public List<string> FontNameList
  58. {
  59. get { return _fontNameList; }
  60. set
  61. {
  62. SetProperty(ref _fontNameList, value);
  63. }
  64. }
  65. private void InitFontNameList()
  66. {
  67. FontNameList.Clear();
  68. FontNameList.Add("Courier");
  69. FontNameList.Add("Courier-Bold");
  70. FontNameList.Add("Courier-Oblique");
  71. FontNameList.Add("Courier-BoldOblique");
  72. FontNameList.Add("Helvetica");
  73. FontNameList.Add("Helvetica-Bold");
  74. FontNameList.Add("Helvetica-Oblique");
  75. FontNameList.Add("Helvetica-BoldOblique");
  76. FontNameList.Add("Times-Roman");
  77. FontNameList.Add("Times-Bold");
  78. FontNameList.Add("Times-Italic");
  79. FontNameList.Add("Times-BoldItalic");
  80. }
  81. private List<string> _fontSizeList = new List<string>();
  82. public List<string> FontSizeList
  83. {
  84. get { return _fontSizeList; }
  85. set
  86. {
  87. SetProperty(ref _fontSizeList, value);
  88. }
  89. }
  90. private void InitFontSizeList()
  91. {
  92. FontSizeList.Clear();
  93. FontSizeList.Add("自动");
  94. for (int temp = 8; temp <= 15; temp += 1)
  95. {
  96. FontSizeList.Add(temp.ToString() + "pt");
  97. }
  98. }
  99. private List<string> _scaleList = new List<string>();
  100. public List<string> ScaleList
  101. {
  102. get { return _scaleList; }
  103. set
  104. {
  105. SetProperty(ref _scaleList, value);
  106. }
  107. }
  108. private void InitScaleList()
  109. {
  110. ScaleList.Clear();
  111. for (int temp = 0; temp <= 100; temp += 10)
  112. {
  113. ScaleList.Add(temp.ToString() + " %");
  114. }
  115. }
  116. private List<string> _isFrontList = new List<string>();
  117. public List<string> IsFrontList
  118. {
  119. get { return _isFrontList; }
  120. set
  121. {
  122. SetProperty(ref _isFrontList, value);
  123. }
  124. }
  125. private void InitIsFrontList()
  126. {
  127. IsFrontList.Clear();
  128. IsFrontList.Add("位于页面上方");
  129. IsFrontList.Add("位于页面下方");
  130. }
  131. private int _rotationValue = 0;
  132. public int RotationValue
  133. {
  134. get { return _rotationValue; }
  135. set
  136. {
  137. SetProperty(ref _rotationValue, value);
  138. WatermarkInfo.Rotation = (float)RotationValue;
  139. }
  140. }
  141. private int _opacityValue = 100;
  142. public int OpacityValue
  143. {
  144. get { return _opacityValue; }
  145. set
  146. {
  147. SetProperty(ref _opacityValue, value);
  148. WatermarkInfo.Opacity = (byte)((float)(OpacityValue/100)*225);
  149. }
  150. }
  151. private int _relativeScaleValue = 50;
  152. public int RelativeScaleValue
  153. {
  154. get { return _relativeScaleValue; }
  155. set
  156. {
  157. SetProperty(ref _relativeScaleValue, value);
  158. }
  159. }
  160. private string _vertOffsetValue = "0";
  161. public string VertOffsetValue
  162. {
  163. get { return _vertOffsetValue; }
  164. set
  165. {
  166. SetProperty(ref _vertOffsetValue, value);
  167. WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
  168. }
  169. }
  170. private string _horizOffsetValue = "0";
  171. public string HorizOffsetValue
  172. {
  173. get { return _horizOffsetValue; }
  174. set
  175. {
  176. SetProperty(ref _horizOffsetValue, value);
  177. WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
  178. }
  179. }
  180. private string _verticalSpacingValue = "6";
  181. public string VerticalSpacingValue
  182. {
  183. get { return _verticalSpacingValue; }
  184. set
  185. {
  186. SetProperty(ref _verticalSpacingValue, value);
  187. WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
  188. }
  189. }
  190. private string _horizontalSpacingValue = "6";
  191. public string HorizontalSpacingValue
  192. {
  193. get { return _horizontalSpacingValue; }
  194. set
  195. {
  196. SetProperty(ref _horizontalSpacingValue, value);
  197. WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
  198. }
  199. }
  200. private int _fontSizeSelectedIndex=0;
  201. public int FontSizeSelectedIndex
  202. {
  203. get { return _fontSizeSelectedIndex; }
  204. set
  205. {
  206. SetProperty(ref _fontSizeSelectedIndex, value);
  207. SetFontSize(FontSizeSelectedIndex);
  208. }
  209. }
  210. private int _fontNameSelectedIndex=0;
  211. public int FontNameSelectedIndex
  212. {
  213. get { return _fontNameSelectedIndex; }
  214. set
  215. {
  216. SetProperty(ref _fontNameSelectedIndex, value);
  217. SetFontName(FontNameSelectedIndex);
  218. }
  219. }
  220. private int _isFrontSelectedIndex=0;
  221. public int IsFrontSelectedIndex
  222. {
  223. get { return _isFrontSelectedIndex; }
  224. set
  225. {
  226. SetProperty(ref _isFrontSelectedIndex, value);
  227. SetIsFront(IsFrontSelectedIndex);
  228. }
  229. }
  230. private string _stringColor = "#FF0000";
  231. public string StringColor
  232. {
  233. get
  234. {
  235. return _stringColor;
  236. }
  237. set
  238. {
  239. SetProperty(ref _stringColor, value);
  240. WatermarkInfo.TextColor = EditToolsHelper.ConvertColor(value);
  241. }
  242. }
  243. public string PageRangeText { get; set; } = "0";
  244. private int _pageRangeSelectIndex = 0;
  245. public int PageRangeSelectIndex
  246. {
  247. get { return _pageRangeSelectIndex; }
  248. set
  249. {
  250. SetProperty(ref _pageRangeSelectIndex, value);
  251. EditToolsHelper.GetPageRange(PageRangeSelectIndex, currentViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  252. }
  253. }
  254. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  255. public ObservableDictionary<string, bool> GetLocationFromNumber
  256. {
  257. get { return _getLocationFromNumber; }
  258. set { _getLocationFromNumber = value; }
  259. }
  260. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  261. public WatermarkCreateTextContentViewModel(IEventAggregator eventAggregator)
  262. {
  263. this.eventAggregator=eventAggregator;
  264. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  265. InitList();
  266. WatermarkInfo.WatermarkHorizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER;
  267. WatermarkInfo.WatermarkVertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER;
  268. InitLocationButtonMatrix();
  269. eventAggregator.GetEvent<ConfirmEditToolsWatermarkEvent>().Subscribe(ConfirmEditToolsWatermark);
  270. }
  271. private void InitLocationButtonMatrix()
  272. {
  273. GetLocationFromNumber.Clear();
  274. for (var temp = 0; temp <= 22; temp++)
  275. {
  276. GetLocationFromNumber.Add(temp.ToString(), true);
  277. if (temp % 10 == 2)
  278. {
  279. temp += 7;
  280. }
  281. }
  282. int Num = (int)WatermarkInfo.WatermarkVertalign * 10 + (int)WatermarkInfo.WatermarkHorizalign;
  283. GetLocationFromNumber[Num.ToString()] = false;
  284. }
  285. public void ChangeLocation(object e)
  286. {
  287. string args = e as string;
  288. if (args != null)
  289. {
  290. WatermarkInfo.WatermarkVertalign = (C_Watermark_Vertalign)(int.Parse(args) / 10);
  291. WatermarkInfo.WatermarkHorizalign = (C_Watermark_Horizalign)(int.Parse(args) % 10);
  292. InitLocationButtonMatrix();
  293. }
  294. }
  295. private void SetFontName(int Index)
  296. {
  297. WatermarkInfo.FontName = FontNameList[Index];
  298. }
  299. private void SetFontSize(int Index)
  300. {
  301. if (Index == 0)
  302. {
  303. WatermarkInfo.TextSize = "10";
  304. }
  305. else
  306. {
  307. FontSizeList.Add((Index+7).ToString());
  308. }
  309. }
  310. private void SetIsFront(int Index)
  311. {
  312. if (Index == 0)
  313. {
  314. WatermarkInfo.IsFront=false;
  315. }
  316. if (Index == 1) {
  317. WatermarkInfo.IsFront = true;
  318. }
  319. }
  320. public void ConfirmEditToolsWatermark(EnumTextOrFile enumTextOrFile)
  321. {
  322. if (enumTextOrFile == EnumTextOrFile.StatusText)
  323. {
  324. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo);
  325. }
  326. }
  327. private void InitList() {
  328. InitOpacityList();
  329. InitFontNameList();
  330. InitFontSizeList();
  331. InitIsFrontList();
  332. InitRotationList();
  333. InitScaleList();
  334. }
  335. public bool IsNavigationTarget(NavigationContext navigationContext)
  336. {
  337. return true;
  338. }
  339. public void OnNavigatedFrom(NavigationContext navigationContext)
  340. {
  341. }
  342. public void OnNavigatedTo(NavigationContext navigationContext)
  343. {
  344. }
  345. }
  346. }