CPDFAnnotationPreviewerControl.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.Controls.Data;
  3. using System;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Imaging;
  8. using System.Windows.Shapes;
  9. using static ComPDFKit.Controls.Helper.CommonHelper;
  10. namespace ComPDFKit.Controls.PDFControl
  11. {
  12. public partial class CPDFAnnotationPreviewerControl : UserControl
  13. {
  14. public CPDFAnnotationPreviewerControl()
  15. {
  16. InitializeComponent();
  17. }
  18. public void CollapsedAll()
  19. {
  20. MarkupGrid.Visibility = Visibility.Collapsed;
  21. HighlightPath.Visibility = Visibility.Collapsed;
  22. UnderlinePath.Visibility = Visibility.Collapsed;
  23. StrikeoutPath.Visibility = Visibility.Collapsed;
  24. SquigglyPath.Visibility = Visibility.Collapsed;
  25. FreehandGrid.Visibility = Visibility.Collapsed;
  26. FreeTextGrid.Visibility = Visibility.Collapsed;
  27. ShapeGrid.Visibility = Visibility.Collapsed;
  28. Ellipse.Visibility = Visibility.Collapsed;
  29. Rectangle.Visibility = Visibility.Collapsed;
  30. Line.Visibility = Visibility.Collapsed;
  31. NoteGrid.Visibility = Visibility.Collapsed;
  32. StampGrid.Visibility = Visibility.Collapsed;
  33. }
  34. public void DrawMarkUpPreview(CPDFAnnotationData annotationData)
  35. {
  36. CPDFMarkupData markupData = annotationData as CPDFMarkupData;
  37. CollapsedAll();
  38. CPDFAnnotationType annotationType = annotationData.AnnotationType;
  39. switch (annotationType)
  40. {
  41. case CPDFAnnotationType.Highlight:
  42. MarkupGrid.Visibility = Visibility.Visible;
  43. HighlightPath.Visibility = Visibility.Visible;
  44. HighlightPath.Stroke = new SolidColorBrush(markupData.Color);
  45. HighlightPath.Opacity = markupData.Opacity;
  46. break;
  47. case CPDFAnnotationType.Underline:
  48. MarkupGrid.Visibility = Visibility.Visible;
  49. UnderlinePath.Visibility = Visibility.Visible;
  50. UnderlinePath.Stroke = new SolidColorBrush(markupData.Color);
  51. UnderlinePath.Opacity = markupData.Opacity;
  52. break;
  53. case CPDFAnnotationType.Squiggly:
  54. MarkupGrid.Visibility = Visibility.Visible;
  55. SquigglyPath.Visibility = Visibility.Visible;
  56. SquigglyPath.Stroke = new SolidColorBrush(markupData.Color);
  57. SquigglyPath.Opacity = markupData.Opacity;
  58. break;
  59. case CPDFAnnotationType.Strikeout:
  60. MarkupGrid.Visibility = Visibility.Visible;
  61. StrikeoutPath.Visibility = Visibility.Visible;
  62. StrikeoutPath.Stroke = new SolidColorBrush(markupData.Color);
  63. StrikeoutPath.Opacity = markupData.Opacity;
  64. break;
  65. }
  66. }
  67. public void DrawShapePreview(CPDFAnnotationData annotationData)
  68. {
  69. CPDFAnnotationType annotationType = annotationData.AnnotationType;
  70. switch (annotationType)
  71. {
  72. case CPDFAnnotationType.Circle:
  73. CPDFShapeData circleData = annotationData as CPDFShapeData;
  74. ShapeGrid.Visibility = Visibility.Visible;
  75. Ellipse.Visibility = Visibility.Visible;
  76. Ellipse.StrokeThickness = circleData.Thickness;
  77. Ellipse.Opacity = circleData.Opacity;
  78. Ellipse.Fill = new SolidColorBrush(circleData.FillColor);
  79. Ellipse.Stroke = new SolidColorBrush(circleData.BorderColor);
  80. DashStyle circleDash = new DashStyle();
  81. if (circleData.DashStyle.Dashes.Count == 2)
  82. {
  83. circleDash.Dashes.Add(circleData.DashStyle.Dashes[0] / Math.Max(Ellipse.StrokeThickness, 1));
  84. circleDash.Dashes.Add(circleData.DashStyle.Dashes[1] / Math.Max(Ellipse.StrokeThickness, 1));
  85. }
  86. Ellipse.StrokeDashArray = circleDash.Dashes;
  87. break;
  88. case CPDFAnnotationType.Square:
  89. CPDFShapeData squareData = annotationData as CPDFShapeData;
  90. ShapeGrid.Visibility = Visibility.Visible;
  91. Rectangle.Visibility = Visibility.Visible;
  92. Rectangle.StrokeThickness = squareData.Thickness;
  93. Rectangle.Opacity = squareData.Opacity;
  94. Rectangle.Fill = new SolidColorBrush(squareData.FillColor);
  95. Rectangle.Stroke = new SolidColorBrush(squareData.BorderColor);
  96. DashStyle squareDash = new DashStyle();
  97. if (squareData.DashStyle.Dashes.Count == 2)
  98. {
  99. squareDash.Dashes.Add(squareData.DashStyle.Dashes[0] / Math.Max(Rectangle.StrokeThickness, 1));
  100. squareDash.Dashes.Add(squareData.DashStyle.Dashes[1] / Math.Max(Rectangle.StrokeThickness, 1));
  101. }
  102. Rectangle.StrokeDashArray = squareDash.Dashes;
  103. break;
  104. case CPDFAnnotationType.Line:
  105. case CPDFAnnotationType.Arrow:
  106. CPDFLineShapeData lineShapeData = annotationData as CPDFLineShapeData;
  107. ShapeGrid.Visibility = Visibility.Visible;
  108. Line.Visibility = Visibility.Visible;
  109. ArrowHelper arrowLine = new ArrowHelper();
  110. arrowLine.ArrowLength = 8;
  111. arrowLine.LineStart = new Point(20, 50);
  112. arrowLine.LineEnd = new Point(50, 20);
  113. arrowLine.StartSharp = lineShapeData.LineType.HeadLineType;
  114. arrowLine.EndSharp = lineShapeData.LineType.TailLineType;
  115. Line.Stroke = new SolidColorBrush(lineShapeData.BorderColor);
  116. Line.StrokeThickness = lineShapeData.Thickness;
  117. Line.Opacity = lineShapeData.Opacity;
  118. Line.Data = arrowLine.BuildArrowBody();
  119. DashStyle lineDash = new DashStyle();
  120. if (lineShapeData.DashStyle.Dashes.Count == 2)
  121. {
  122. lineDash.Dashes.Add(lineShapeData.DashStyle.Dashes[0] / Math.Max(Line.StrokeThickness, 1));
  123. lineDash.Dashes.Add(lineShapeData.DashStyle.Dashes[1] / Math.Max(Line.StrokeThickness, 1));
  124. }
  125. Line.StrokeDashArray = lineDash.Dashes;
  126. break;
  127. }
  128. }
  129. public void DrawFreehandPreview(CPDFAnnotationData annotationData)
  130. {
  131. CPDFFreehandData freehandData = annotationData as CPDFFreehandData;
  132. CollapsedAll();
  133. FreehandGrid.Visibility = Visibility.Visible;
  134. SharpPath.Stroke = new SolidColorBrush(freehandData.BorderColor);
  135. SharpPath.Opacity = freehandData.Opacity;
  136. SharpPath.StrokeThickness = freehandData.Thickness;
  137. }
  138. public void DrawCloudPreview()
  139. {
  140. CollapsedAll();
  141. gdPolygon.Visibility = Visibility.Visible;
  142. }
  143. public void DrawNotePreview(CPDFAnnotationData annotationData)
  144. {
  145. CollapsedAll();
  146. NoteGrid.Visibility = Visibility.Visible;
  147. CPDFNoteData noteData = annotationData as CPDFNoteData;
  148. NotePath.Fill = new SolidColorBrush(noteData.BorderColor);
  149. }
  150. public void DrawStampPreview(WriteableBitmap writeableBitmap)
  151. {
  152. CollapsedAll();
  153. StampGrid.Visibility = Visibility.Visible;
  154. StampImage.Source = writeableBitmap;
  155. }
  156. public void DrawFreeTextPreview(CPDFFreeTextData freeTextData)
  157. {
  158. CollapsedAll();
  159. FreeTextGrid.Visibility = Visibility.Visible;
  160. string fontFamily = string.Empty;
  161. string fontStyle = string.Empty;
  162. CPDFFont.GetFamilyStyleName(freeTextData.FontFamily, ref fontFamily, ref fontStyle);
  163. FreeText.FontFamily = new FontFamily(fontFamily);
  164. FreeText.FontSize = freeTextData.FontSize / 1.2;
  165. FreeText.Foreground = new SolidColorBrush(freeTextData.BorderColor);
  166. FreeText.Opacity = freeTextData.Opacity;
  167. }
  168. private Path DrawCloudLine(Point startPoint, Point endPoint, double radius, double angle, Brush strokeColor, double strokeThickness, bool isUpward)
  169. {
  170. double lineLength = Math.Sqrt(Math.Pow(endPoint.X - startPoint.X, 2) + Math.Pow(endPoint.Y - startPoint.Y, 2));
  171. double arcLength = 2 * Math.PI * radius * (angle / 360);
  172. int arcCount = (int)Math.Ceiling(lineLength / arcLength);
  173. Vector direction = new Vector(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
  174. direction.Normalize();
  175. direction *= arcLength;
  176. Point currentPoint = startPoint;
  177. // 创建一个完整的PathGeometry
  178. PathGeometry pathGeometry = new PathGeometry();
  179. PathFigure pathFigure = new PathFigure
  180. {
  181. StartPoint = currentPoint,
  182. IsClosed = false
  183. };
  184. for (int i = 0; i < arcCount; i++)
  185. {
  186. // Calculate the end point of the arc
  187. Point nextPoint = new Point(currentPoint.X + direction.X, currentPoint.Y + direction.Y);
  188. ArcSegment arcSegment = new ArcSegment
  189. {
  190. Point = nextPoint,
  191. Size = new Size(radius, radius),
  192. SweepDirection = isUpward ? SweepDirection.Clockwise : SweepDirection.Counterclockwise, // 控制圆弧方向
  193. IsLargeArc = angle > 180
  194. };
  195. // Add the ArcSegment to the path
  196. pathFigure.Segments.Add(arcSegment);
  197. // Update currentPoint to be the end point of the arc
  198. currentPoint = nextPoint;
  199. }
  200. // 将所有圆弧段添加到一个PathFigure中
  201. pathGeometry.Figures.Add(pathFigure);
  202. // 创建最终的Path对象
  203. Path cloudPath = new Path
  204. {
  205. Stroke = strokeColor,
  206. StrokeThickness = strokeThickness,
  207. Data = pathGeometry
  208. };
  209. return cloudPath;
  210. }
  211. }
  212. }