WatermarkDialog.xaml.cs 17 KB

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