WatermarkPreviewControl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKit.PDFWatermark;
  3. using Compdfkit_Tools.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using System.Xml.Linq;
  20. namespace Compdfkit_Tools.PDFControl
  21. {
  22. public class WatermarkPreviewControl : PreviewControl
  23. {
  24. public List<int> MarkedPageList = new List<int>();
  25. private CPDFWatermark watermark;
  26. private WatermarkData _watermarkData;
  27. public WatermarkData WatermarkData
  28. {
  29. get => _watermarkData;
  30. set
  31. {
  32. _watermarkData = value;
  33. if (watermark != null)
  34. {
  35. watermark?.ClearWatermark();
  36. watermark?.Release();
  37. lock (queueLock)
  38. {
  39. Document.ReleasePages();
  40. }
  41. }
  42. watermark = Document.InitWatermark(WatermarkData.Type);
  43. if (IsLoaded)
  44. {
  45. OnWatermarkChanged();
  46. }
  47. }
  48. }
  49. public event EventHandler WatermarkChanged;
  50. public WatermarkPreviewControl()
  51. {
  52. InitializeComponent();
  53. Loaded -= WatermarkPreviewControl_Loaded;
  54. Loaded += WatermarkPreviewControl_Loaded;
  55. }
  56. private void WatermarkPreviewControl_Loaded(object sender, RoutedEventArgs e)
  57. {
  58. CurrentIndexChanged += WatermarkPreviewControl_CurrentIndexChanged;
  59. }
  60. private void WatermarkPreviewControl_CurrentIndexChanged(object sender, EventArgs e)
  61. {
  62. DeleteDisplayWatermark();
  63. }
  64. private void OnWatermarkChanged()
  65. {
  66. WatermarkChanged?.Invoke(this, EventArgs.Empty);
  67. }
  68. public void UpdateByWatermarkData()
  69. {
  70. if (WatermarkData == null || !MarkedPageList.Contains(CurrentIndex))
  71. {
  72. return;
  73. }
  74. if (WatermarkData.Type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_TEXT)
  75. {
  76. UpdateByTextWatermarkData();
  77. }
  78. else if (WatermarkData.Type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_IMG && !string.IsNullOrEmpty(WatermarkData.ImagePath))
  79. {
  80. UpdateByImageWatermarkData();
  81. }
  82. else
  83. {
  84. return;
  85. }
  86. }
  87. private void UpdateByImageWatermarkData()
  88. {
  89. Bitmap bitmap = new Bitmap(WatermarkData.ImagePath);
  90. bitmap = CommonHelper.ConvertTo32bppArgb(bitmap);
  91. byte[] byteArray = CommonHelper.ConvertBitmapToByteArray(bitmap);
  92. watermark.SetImage(byteArray, bitmap.Width, bitmap.Height);
  93. watermark.SetScale((float)(WatermarkData.ImageScale / 100.0));
  94. UpdateByCommonWatermarkData();
  95. }
  96. private void UpdateByTextWatermarkData()
  97. {
  98. watermark.SetText(WatermarkData.Text);
  99. watermark.SetFontName(WatermarkData.FontName);
  100. watermark.SetTextRGBColor(WatermarkData.Color);
  101. watermark.SetFontSize(WatermarkData.FontSize);
  102. watermark.SetScale(1);
  103. UpdateByCommonWatermarkData();
  104. }
  105. private void UpdateByCommonWatermarkData()
  106. {
  107. watermark.SetPages((PageRangeList[CurrentIndex - 1] - 1).ToString());
  108. watermark.SetRotation((float)(WatermarkData.Rotation * Math.PI / 180.0));
  109. watermark.SetOpacity(WatermarkData.Opacity);
  110. watermark.SetVertalign((C_Watermark_Vertalign)(WatermarkData.Align / 3));
  111. watermark.SetHorizalign((C_Watermark_Horizalign)(WatermarkData.Align % 3));
  112. watermark.SetVertOffset(WatermarkData.VertOffset);
  113. watermark.SetHorizOffset(WatermarkData.HorizOffset);
  114. watermark.SetFront(WatermarkData.IsFront);
  115. watermark.SetFullScreen(WatermarkData.IsFullScreen);
  116. watermark.SetVerticalSpacing(WatermarkData.VerticalSpacing);
  117. watermark.SetHorizontalSpacing(WatermarkData.HorizontalSpacing);
  118. watermark.CreateWatermark();
  119. watermark.UpdateWatermark();
  120. }
  121. public void DeleteDisplayWatermark()
  122. {
  123. if (watermark != null)
  124. {
  125. Document.ReleasePages();
  126. watermark?.ClearWatermark();
  127. }
  128. }
  129. override public void BeginLoadImageThread(int pageIndex)
  130. {
  131. UpdateByWatermarkData();
  132. base.BeginLoadImageThread(pageIndex);
  133. }
  134. override public void AttachLoaded()
  135. {
  136. WatermarkChanged += WatermarkPreviewControl_WatermarkChanged;
  137. OnWatermarkChanged();
  138. }
  139. private void WatermarkPreviewControl_WatermarkChanged(object sender, EventArgs e)
  140. {
  141. BeginLoadImageThread(PageRangeList[CurrentIndex - 1] - 1);
  142. }
  143. }
  144. }