WatermarkCreateTextContentViewModel.cs 13 KB

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