WatermarkDialog.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using ComPDFKit.PDFWatermark;
  2. using Compdfkit_Tools.Common;
  3. using Compdfkit_Tools.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. namespace Compdfkit_Tools.PDFControl
  20. {
  21. /// <summary>
  22. /// Interaction logic for WatermarkDialog.xaml
  23. /// </summary>
  24. public partial class WatermarkDialog : Window, INotifyPropertyChanged
  25. {
  26. private bool _canChangeRange = false;
  27. public bool CanChangeRange
  28. {
  29. get => _canChangeRange;
  30. set => UpdateProper(ref _canChangeRange, value);
  31. }
  32. private C_Watermark_Type _watermarkType = C_Watermark_Type.WATERMARK_TYPE_TEXT;
  33. public C_Watermark_Type WatermarkType
  34. {
  35. get => _watermarkType;
  36. set
  37. {
  38. if (UpdateProper(ref _watermarkType, value))
  39. {
  40. watermarkData.Type = _watermarkType;
  41. }
  42. }
  43. }
  44. private string _watermarkText = "Watermark";
  45. public string WatermarkText
  46. {
  47. get => _watermarkText;
  48. set
  49. {
  50. if (UpdateProper(ref _watermarkText, value))
  51. {
  52. watermarkData.Text = _watermarkText;
  53. }
  54. }
  55. }
  56. private byte[] _brush = new byte[] { 255, 0, 0 };
  57. public byte[] Brush
  58. {
  59. get => _brush;
  60. set
  61. {
  62. if (UpdateProper(ref _brush, value))
  63. {
  64. watermarkData.Color = _brush;
  65. }
  66. }
  67. }
  68. private int _selectedTag = 4;
  69. public int SelectedTag
  70. {
  71. get => _selectedTag;
  72. set
  73. {
  74. if (UpdateProper(ref _selectedTag, value))
  75. {
  76. Vertalign = (C_Watermark_Vertalign)(SelectedTag / 3);
  77. Horizalign = (C_Watermark_Horizalign)(SelectedTag % 3);
  78. }
  79. }
  80. }
  81. private C_Watermark_Horizalign _horizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER;
  82. public C_Watermark_Horizalign Horizalign
  83. {
  84. get => _horizalign;
  85. set
  86. {
  87. if (UpdateProper(ref _horizalign, value))
  88. {
  89. watermarkData.Horizalign = _horizalign;
  90. }
  91. }
  92. }
  93. private C_Watermark_Vertalign _vertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER;
  94. public C_Watermark_Vertalign Vertalign
  95. {
  96. get => _vertalign;
  97. set
  98. {
  99. if (UpdateProper(ref _vertalign, value))
  100. {
  101. watermarkData.Vertalign = _vertalign;
  102. }
  103. }
  104. }
  105. private string _imagePath = "";
  106. public string ImagePath
  107. {
  108. get => _imagePath;
  109. set
  110. {
  111. if (UpdateProper(ref _imagePath, value))
  112. {
  113. watermarkData.ImagePath = _imagePath;
  114. }
  115. }
  116. }
  117. private string _imageScale = "100";
  118. public string ImageScale
  119. {
  120. get => _imageScale;
  121. set
  122. {
  123. if (string.IsNullOrEmpty(value))
  124. {
  125. return;
  126. }
  127. if (int.Parse(value) > ScaleNumericControl.Maxium)
  128. {
  129. value = ScaleNumericControl.Maxium.ToString();
  130. }
  131. if (int.Parse(value) < ScaleNumericControl.Minimum)
  132. {
  133. value = ScaleNumericControl.Minimum.ToString();
  134. }
  135. if (UpdateProper(ref _imageScale, value))
  136. {
  137. watermarkData.ImageScale = int.Parse(_imageScale);
  138. }
  139. }
  140. }
  141. private WatermarkData watermarkData = new();
  142. private string _fontName = "Helvetica";
  143. public string FontName
  144. {
  145. get => _fontName;
  146. set
  147. {
  148. if (UpdateProper(ref _fontName, value))
  149. {
  150. watermarkData.FontName = _fontName;
  151. }
  152. }
  153. }
  154. private string _textSize = "48";
  155. public string TextSize
  156. {
  157. get => _textSize;
  158. set
  159. {
  160. if (UpdateProper(ref _textSize, value))
  161. {
  162. watermarkData.FontSize = float.Parse(_textSize);
  163. }
  164. }
  165. }
  166. private bool _isFront = true;
  167. public bool IsFront
  168. {
  169. get => _isFront;
  170. set
  171. {
  172. if (UpdateProper(ref _isFront, value))
  173. {
  174. watermarkData.IsFront = _isFront;
  175. }
  176. }
  177. }
  178. private bool _isFullScreen;
  179. public bool IsFullScreen
  180. {
  181. get => _isFullScreen;
  182. set
  183. {
  184. if (UpdateProper(ref _isFullScreen, value))
  185. {
  186. watermarkData.IsFullScreen = _isFullScreen;
  187. }
  188. }
  189. }
  190. private int _rotation;
  191. public int Rotation
  192. {
  193. get => _rotation;
  194. set
  195. {
  196. if (UpdateProper(ref _rotation, value))
  197. {
  198. watermarkData.Rotation = _rotation;
  199. }
  200. }
  201. }
  202. private byte _opacityValue = 255;
  203. public byte OpacityValue
  204. {
  205. get => _opacityValue;
  206. set
  207. {
  208. if (UpdateProper(ref _opacityValue, value))
  209. {
  210. watermarkData.Opacity = _opacityValue;
  211. }
  212. }
  213. }
  214. private float _verticalSpacing;
  215. public float VerticalSpacing
  216. {
  217. get => _verticalSpacing;
  218. set
  219. {
  220. if (UpdateProper(ref _verticalSpacing, value))
  221. {
  222. watermarkData.VerticalSpacing = _verticalSpacing;
  223. }
  224. }
  225. }
  226. private float _horizontalSpacing;
  227. public float HorizontalSpacing
  228. {
  229. get => _horizontalSpacing;
  230. set
  231. {
  232. if (UpdateProper(ref _horizontalSpacing, value))
  233. {
  234. watermarkData.HorizontalSpacing = _horizontalSpacing;
  235. }
  236. }
  237. }
  238. private float _vertOffset;
  239. public float VertOffset
  240. {
  241. get => _vertOffset;
  242. set
  243. {
  244. if (UpdateProper(ref _vertOffset, value))
  245. {
  246. watermarkData.VertOffset = _vertOffset;
  247. }
  248. }
  249. }
  250. private float _horizOffset;
  251. public float HorizOffset
  252. {
  253. get => _horizOffset;
  254. set
  255. {
  256. if (UpdateProper<float>(ref _horizOffset, value))
  257. {
  258. watermarkData.HorizOffset = _horizOffset;
  259. }
  260. }
  261. }
  262. public WatermarkDialog()
  263. {
  264. DataContext = this;
  265. InitializeComponent();
  266. }
  267. public bool InitWithFileInfo(FileInfoWithRange fileInfo)
  268. {
  269. if (fileInfo == null)
  270. {
  271. return false;
  272. }
  273. InitPreviewControl(fileInfo);
  274. return true;
  275. }
  276. private void InitPreviewControl(FileInfoWithRange fileInfo)
  277. {
  278. PreviewControl.Document = fileInfo.Document;
  279. PreviewControl.MarkedPageList = fileInfo.PageRangeList;
  280. PreviewControl.PageRangeList = CommonHelper.GetDefaultPageList(PreviewControl.Document);
  281. }
  282. public event PropertyChangedEventHandler PropertyChanged;
  283. protected virtual void OnPropertyChanged(string propertyName = null)
  284. {
  285. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  286. }
  287. protected bool UpdateProper<T>(ref T properValue,
  288. T newValue,
  289. [CallerMemberName] string properName = "")
  290. {
  291. if (object.Equals(properValue, newValue))
  292. return false;
  293. properValue = newValue;
  294. OnPropertyChanged(properName);
  295. return true;
  296. }
  297. private void FontFamilyCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
  298. {
  299. var item = sender as ComboBox;
  300. if (item != null)
  301. {
  302. FontName = (item.SelectedItem as ComboBoxItem).Tag.ToString();
  303. }
  304. }
  305. private void FontSizeCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
  306. {
  307. var item = sender as ComboBox;
  308. if (item != null)
  309. {
  310. TextSize = (item.SelectedItem as ComboBoxItem).Tag.ToString();
  311. }
  312. }
  313. private void Window_Loaded(object sender, RoutedEventArgs e)
  314. {
  315. ColorPickerControl.SetIsChecked(1);
  316. InitWatermarkData(in watermarkData);
  317. PreviewControl.WatermarkData = watermarkData;
  318. watermarkData.ValueChanged +=
  319. (sender, e) => PreviewControl.WatermarkData = watermarkData;
  320. }
  321. private void InitWatermarkData(in WatermarkData watermarkData)
  322. {
  323. watermarkData.Type = WatermarkType;
  324. watermarkData.Text = WatermarkText;
  325. watermarkData.FontName = FontName;
  326. watermarkData.FontSize = float.Parse(TextSize);
  327. watermarkData.Color = Brush;
  328. watermarkData.ImagePath = ImagePath;
  329. watermarkData.ImageScale = int.Parse(ImageScale);
  330. watermarkData.Rotation = Rotation;
  331. watermarkData.Opacity = OpacityValue;
  332. watermarkData.Vertalign = Vertalign;
  333. watermarkData.Horizalign = Horizalign;
  334. watermarkData.VertOffset = VertOffset;
  335. watermarkData.Horizalign = Horizalign;
  336. watermarkData.IsFront = IsFront;
  337. watermarkData.IsFullScreen = IsFullScreen;
  338. watermarkData.VerticalSpacing = VerticalSpacing;
  339. watermarkData.HorizontalSpacing = HorizontalSpacing;
  340. }
  341. private void ImagePathBtn_Click(object sender, RoutedEventArgs e)
  342. {
  343. string filePath = CommonHelper.GetExistedPathOrEmpty("image (*.jpg;*.jpeg;*.png;)|*.jpg;*.jpeg;*.png;");
  344. if (!string.IsNullOrEmpty(filePath))
  345. {
  346. ImagePath = filePath;
  347. }
  348. }
  349. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  350. {
  351. var opacityControl = sender as CPDFOpacityControl;
  352. if (opacityControl != null)
  353. {
  354. OpacityValue = (byte)(opacityControl.OpacityValue / 100.0 * 255.0);
  355. }
  356. }
  357. private void LocationChk_Checked(object sender, RoutedEventArgs e)
  358. {
  359. var locationChk = sender as CheckBox;
  360. if (locationChk != null)
  361. {
  362. if (locationChk.Name == "LocationChk")
  363. {
  364. IsFront = true;
  365. }
  366. else
  367. {
  368. IsFront = false;
  369. }
  370. }
  371. }
  372. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  373. {
  374. var colorPicker = sender as ColorPickerControl;
  375. if (colorPicker != null)
  376. {
  377. Brush = CommonHelper.ConvertBrushToByteArray(colorPicker.Brush);
  378. }
  379. }
  380. private void TypeRdo_Checked(object sender, RoutedEventArgs e)
  381. {
  382. var rdo = sender as RadioButton;
  383. if (rdo != null)
  384. {
  385. if (rdo.Name == "TextRdo")
  386. {
  387. WatermarkType = C_Watermark_Type.WATERMARK_TYPE_TEXT;
  388. }
  389. else if (rdo.Name == "ImageRdo")
  390. {
  391. WatermarkType = C_Watermark_Type.WATERMARK_TYPE_IMG;
  392. }
  393. else
  394. {
  395. watermarkData.Type = C_Watermark_Type.WATERMARK_TYPE_UNKWON;
  396. }
  397. }
  398. }
  399. private void CPDFRotationControl_RotationChanged(object sender, EventArgs e)
  400. {
  401. var rotationControl = sender as CPDFRotationControl;
  402. if (rotationControl != null)
  403. {
  404. Rotation = int.Parse(rotationControl.RotationText);
  405. }
  406. }
  407. private void CPDFTileControl_HorizontalSpacingChanged(object sender, EventArgs e)
  408. {
  409. var tileControl = sender as CPDFTileControl;
  410. if (tileControl != null)
  411. {
  412. HorizontalSpacing = float.Parse(tileControl.HorizontalSpacingValue);
  413. }
  414. }
  415. private void CPDFTileControl_VerticalSpacingChanged(object sender, EventArgs e)
  416. {
  417. var tileControl = sender as CPDFTileControl;
  418. if (tileControl != null)
  419. {
  420. VerticalSpacing = float.Parse(tileControl.VerticalSpacingValue);
  421. }
  422. }
  423. private void CPDFTileControl_FullScreenChanged(object sender, EventArgs e)
  424. {
  425. var tileControl = sender as CPDFTileControl;
  426. if (tileControl != null)
  427. {
  428. IsFullScreen = tileControl.IsFullScreen;
  429. }
  430. }
  431. private void CPDFLocationControl_HorizOffsetChanged(object sender, EventArgs e)
  432. {
  433. var locationControl = sender as CPDFLocationControl;
  434. if (locationControl != null)
  435. {
  436. HorizOffset = float.Parse(locationControl.HorizOffsetValue);
  437. }
  438. }
  439. private void CPDFLocationControl_VertOffsetChanged(object sender, EventArgs e)
  440. {
  441. var locationControl = sender as CPDFLocationControl;
  442. if (locationControl != null)
  443. {
  444. VertOffset = float.Parse(locationControl.VertOffsetValue);
  445. }
  446. }
  447. }
  448. }