using ComPDFKit.PDFDocument; using ComPDFKit.Controls.Data; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using static ComPDFKit.Controls.Helper.CommonHelper; namespace ComPDFKit.Controls.PDFControl { public partial class CPDFAnnotationPreviewerControl : UserControl { public CPDFAnnotationPreviewerControl() { InitializeComponent(); } public void CollapsedAll() { MarkupGrid.Visibility = Visibility.Collapsed; HighlightPath.Visibility = Visibility.Collapsed; UnderlinePath.Visibility = Visibility.Collapsed; StrikeoutPath.Visibility = Visibility.Collapsed; SquigglyPath.Visibility = Visibility.Collapsed; FreehandGrid.Visibility = Visibility.Collapsed; FreeTextGrid.Visibility = Visibility.Collapsed; ShapeGrid.Visibility = Visibility.Collapsed; Ellipse.Visibility = Visibility.Collapsed; Rectangle.Visibility = Visibility.Collapsed; Line.Visibility = Visibility.Collapsed; NoteGrid.Visibility = Visibility.Collapsed; StampGrid.Visibility = Visibility.Collapsed; } public void DrawMarkUpPreview(CPDFAnnotationData annotationData) { CPDFMarkupData markupData = annotationData as CPDFMarkupData; CollapsedAll(); CPDFAnnotationType annotationType = annotationData.AnnotationType; switch (annotationType) { case CPDFAnnotationType.Highlight: MarkupGrid.Visibility = Visibility.Visible; HighlightPath.Visibility = Visibility.Visible; HighlightPath.Stroke = new SolidColorBrush(markupData.Color); HighlightPath.Opacity = markupData.Opacity; break; case CPDFAnnotationType.Underline: MarkupGrid.Visibility = Visibility.Visible; UnderlinePath.Visibility = Visibility.Visible; UnderlinePath.Stroke = new SolidColorBrush(markupData.Color); UnderlinePath.Opacity = markupData.Opacity; break; case CPDFAnnotationType.Squiggly: MarkupGrid.Visibility = Visibility.Visible; SquigglyPath.Visibility = Visibility.Visible; SquigglyPath.Stroke = new SolidColorBrush(markupData.Color); SquigglyPath.Opacity = markupData.Opacity; break; case CPDFAnnotationType.Strikeout: MarkupGrid.Visibility = Visibility.Visible; StrikeoutPath.Visibility = Visibility.Visible; StrikeoutPath.Stroke = new SolidColorBrush(markupData.Color); StrikeoutPath.Opacity = markupData.Opacity; break; } } public void DrawShapePreview(CPDFAnnotationData annotationData) { CPDFAnnotationType annotationType = annotationData.AnnotationType; switch (annotationType) { case CPDFAnnotationType.Circle: CPDFShapeData circleData = annotationData as CPDFShapeData; ShapeGrid.Visibility = Visibility.Visible; Ellipse.Visibility = Visibility.Visible; Ellipse.StrokeThickness = circleData.Thickness; Ellipse.Opacity = circleData.Opacity; Ellipse.Fill = new SolidColorBrush(circleData.FillColor); Ellipse.Stroke = new SolidColorBrush(circleData.BorderColor); DashStyle circleDash = new DashStyle(); if (circleData.DashStyle.Dashes.Count == 2) { circleDash.Dashes.Add(circleData.DashStyle.Dashes[0] / Math.Max(Ellipse.StrokeThickness, 1)); circleDash.Dashes.Add(circleData.DashStyle.Dashes[1] / Math.Max(Ellipse.StrokeThickness, 1)); } Ellipse.StrokeDashArray = circleDash.Dashes; break; case CPDFAnnotationType.Square: CPDFShapeData squareData = annotationData as CPDFShapeData; ShapeGrid.Visibility = Visibility.Visible; Rectangle.Visibility = Visibility.Visible; Rectangle.StrokeThickness = squareData.Thickness; Rectangle.Opacity = squareData.Opacity; Rectangle.Fill = new SolidColorBrush(squareData.FillColor); Rectangle.Stroke = new SolidColorBrush(squareData.BorderColor); DashStyle squareDash = new DashStyle(); if (squareData.DashStyle.Dashes.Count == 2) { squareDash.Dashes.Add(squareData.DashStyle.Dashes[0] / Math.Max(Rectangle.StrokeThickness, 1)); squareDash.Dashes.Add(squareData.DashStyle.Dashes[1] / Math.Max(Rectangle.StrokeThickness, 1)); } Rectangle.StrokeDashArray = squareDash.Dashes; break; case CPDFAnnotationType.Line: case CPDFAnnotationType.Arrow: CPDFLineShapeData lineShapeData = annotationData as CPDFLineShapeData; ShapeGrid.Visibility = Visibility.Visible; Line.Visibility = Visibility.Visible; ArrowHelper arrowLine = new ArrowHelper(); arrowLine.ArrowLength = 8; arrowLine.LineStart = new Point(20, 50); arrowLine.LineEnd = new Point(50, 20); arrowLine.StartSharp = lineShapeData.LineType.HeadLineType; arrowLine.EndSharp = lineShapeData.LineType.TailLineType; Line.Stroke = new SolidColorBrush(lineShapeData.BorderColor); Line.StrokeThickness = lineShapeData.Thickness; Line.Opacity = lineShapeData.Opacity; Line.Data = arrowLine.BuildArrowBody(); DashStyle lineDash = new DashStyle(); if (lineShapeData.DashStyle.Dashes.Count == 2) { lineDash.Dashes.Add(lineShapeData.DashStyle.Dashes[0] / Math.Max(Line.StrokeThickness, 1)); lineDash.Dashes.Add(lineShapeData.DashStyle.Dashes[1] / Math.Max(Line.StrokeThickness, 1)); } Line.StrokeDashArray = lineDash.Dashes; break; } } public void DrawFreehandPreview(CPDFAnnotationData annotationData) { CPDFFreehandData freehandData = annotationData as CPDFFreehandData; CollapsedAll(); FreehandGrid.Visibility = Visibility.Visible; SharpPath.Stroke = new SolidColorBrush(freehandData.BorderColor); SharpPath.Opacity = freehandData.Opacity; SharpPath.StrokeThickness = freehandData.Thickness; } public void DrawCloudPreview() { CollapsedAll(); gdPolygon.Visibility = Visibility.Visible; } public void DrawNotePreview(CPDFAnnotationData annotationData) { CollapsedAll(); NoteGrid.Visibility = Visibility.Visible; CPDFNoteData noteData = annotationData as CPDFNoteData; NotePath.Fill = new SolidColorBrush(noteData.BorderColor); } public void DrawStampPreview(WriteableBitmap writeableBitmap) { CollapsedAll(); StampGrid.Visibility = Visibility.Visible; StampImage.Source = writeableBitmap; } public void DrawFreeTextPreview(CPDFFreeTextData freeTextData) { CollapsedAll(); FreeTextGrid.Visibility = Visibility.Visible; string fontFamily = string.Empty; string fontStyle = string.Empty; CPDFFont.GetFamilyStyleName(freeTextData.FontFamily, ref fontFamily, ref fontStyle); FreeText.FontFamily = new FontFamily(fontFamily); FreeText.FontSize = freeTextData.FontSize / 1.2; FreeText.Foreground = new SolidColorBrush(freeTextData.BorderColor); FreeText.Opacity = freeTextData.Opacity; } private Path DrawCloudLine(Point startPoint, Point endPoint, double radius, double angle, Brush strokeColor, double strokeThickness, bool isUpward) { double lineLength = Math.Sqrt(Math.Pow(endPoint.X - startPoint.X, 2) + Math.Pow(endPoint.Y - startPoint.Y, 2)); double arcLength = 2 * Math.PI * radius * (angle / 360); int arcCount = (int)Math.Ceiling(lineLength / arcLength); Vector direction = new Vector(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y); direction.Normalize(); direction *= arcLength; Point currentPoint = startPoint; // 创建一个完整的PathGeometry PathGeometry pathGeometry = new PathGeometry(); PathFigure pathFigure = new PathFigure { StartPoint = currentPoint, IsClosed = false }; for (int i = 0; i < arcCount; i++) { // Calculate the end point of the arc Point nextPoint = new Point(currentPoint.X + direction.X, currentPoint.Y + direction.Y); ArcSegment arcSegment = new ArcSegment { Point = nextPoint, Size = new Size(radius, radius), SweepDirection = isUpward ? SweepDirection.Clockwise : SweepDirection.Counterclockwise, // 控制圆弧方向 IsLargeArc = angle > 180 }; // Add the ArcSegment to the path pathFigure.Segments.Add(arcSegment); // Update currentPoint to be the end point of the arc currentPoint = nextPoint; } // 将所有圆弧段添加到一个PathFigure中 pathGeometry.Figures.Add(pathFigure); // 创建最终的Path对象 Path cloudPath = new Path { Stroke = strokeColor, StrokeThickness = strokeThickness, Data = pathGeometry }; return cloudPath; } } }