WatermarkDialog.xaml.cs 15 KB

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