WatermarkDialog.xaml.cs 15 KB

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