Explorar o código

ComPDFKit.Tool(win) - 修复折线和多边形只有两点时的创建问题

TangJinZhou hai 2 semanas
pai
achega
c808f6bfad

+ 3 - 2
Demo/Examples/ComPDFKit.Tool/CPDFToolManager.cs

@@ -2180,12 +2180,13 @@ namespace ComPDFKit.Tool
                                     {
                                         if (!viewerTool.IsCanSave())
                                         {
-                                            viewerTool.SetIsCanSave(true);
+                                            BaseLayer baseLayer = viewerTool.PDFViewer.GetViewForTag(viewerTool.CreateAnnotTag);
+                                            bool finshed = (baseLayer as CreateAnnotTool).IsCreateFinshed();
+                                            viewerTool.SetIsCanSave(finshed);
                                         }
                                     }
                                     break;
                             }
-
                         }
                         else if (currentToolType == ToolType.WidgetEdit)
                         {

+ 25 - 25
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.Annot.cs

@@ -10,8 +10,6 @@ using ComPDFKitViewer.Layer;
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 using System.Windows.Input;
 using System.Windows;
 using ComPDFKit.Import;
@@ -20,12 +18,9 @@ using System.Windows.Controls;
 using System.Windows.Media;
 using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
 using ComPDFKitViewer.Helper;
-using Microsoft.SqlServer.Server;
 using ComPDFKit.Tool.Help;
-using System.Xml.Linq;
 using ComPDFKit.Tool.SettingParam;
 using ComPDFKit.Tool.UndoManger;
-using System.Windows.Controls.Primitives;
 using ComPDFKit.Measure;
 using System.Dynamic;
 
@@ -53,13 +48,14 @@ namespace ComPDFKit.Tool
         BaseAnnot caheMoveAnnot;
         BaseAnnot cacheHitTestAnnot;
         bool isCacheRedaction = false;
-        int createAnnotTag = -1;
         int selectedPageIndex = -1;
         int selectedAnnotIndex = -1;
         bool canSave = true;
         bool isHitTestLink = false;
         bool isHitTestRedact = false;
+
         public event EventHandler<MeasureEventArgs> MeasureChanged;
+        internal int CreateAnnotTag { get; private set; } = -1;
 
         public void InvokeMeasureChangeEvent(object sender, MeasureEventArgs e)
         {
@@ -127,7 +123,7 @@ namespace ComPDFKit.Tool
 
         protected bool AnnotHitTest()
         {
-            BaseAnnot baseAnnot = PDFViewer.AnnotHitTest(true);
+            BaseAnnot baseAnnot = PDFViewer.AnnotHitTest();
             if (baseAnnot != null)
             {
                 if ((baseAnnot as BaseWidget) != null)
@@ -135,11 +131,15 @@ namespace ComPDFKit.Tool
                     cacheHitTestAnnot = null;
                     return false;
                 }
+
                 cacheHitTestAnnot = baseAnnot;
                 return true;
             }
-            cacheHitTestAnnot = baseAnnot;
-            return false;
+            else
+            {
+                cacheHitTestAnnot = null;
+                return false;
+            }
         }
 
         public void SelectedAnnotForIndex(int pageIndex, int annotIndex)
@@ -166,7 +166,7 @@ namespace ComPDFKit.Tool
             CreateAnnotTool createAnnotTool = new CreateAnnotTool(GetMeasureSetting(), GetDefaultDrawParam(), GetDefaultSettingParam());
             int annotViewindex = PDFViewer.GetMaxViewIndex();
             PDFViewer.InsertView(annotViewindex, createAnnotTool);
-            createAnnotTag = createAnnotTool.GetResTag();
+            CreateAnnotTag = createAnnotTool.GetResTag();
             createAnnotTool.UpdateAnnotHandler += CreateAnnotTool_UpdateAnnotHandler;
             createAnnotTool.CreateFreetextCanceled += CreateAnnotTool_CreateFreetextCanceled;
             createAnnotTool.MeasureChanged += CreateAnnotTool_MeasureChanged;
@@ -206,7 +206,7 @@ namespace ComPDFKit.Tool
             {
                 return;
             }
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             (baseLayer as CreateAnnotTool).SetIsProportionalScaling(isProportionalScaling);
         }
 
@@ -216,7 +216,7 @@ namespace ComPDFKit.Tool
             {
                 return 0;
             }
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             return (baseLayer as CreateAnnotTool).GetMoveLength();
         }
 
@@ -228,7 +228,7 @@ namespace ComPDFKit.Tool
             }
 
             Point point = Mouse.GetPosition(this);
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             PDFViewer.GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
             if (index < 0)
             {
@@ -271,7 +271,7 @@ namespace ComPDFKit.Tool
                 return;
             }
             Point point = Mouse.GetPosition(this);
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             PDFViewer.GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
             if (index < 0)
             {
@@ -309,7 +309,7 @@ namespace ComPDFKit.Tool
                 return;
             }
             Point point = Mouse.GetPosition(this);
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             (baseLayer as CreateAnnotTool).MoveDraw(point, PDFViewer.GetZoom());
         }
 
@@ -320,7 +320,7 @@ namespace ComPDFKit.Tool
                 return;
             }
             Point point = Mouse.GetPosition(this);
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             (baseLayer as CreateAnnotTool).CreateTextBox();
         }
 
@@ -330,7 +330,7 @@ namespace ComPDFKit.Tool
             {
                 return new Rect();
             }
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             return (baseLayer as CreateAnnotTool).EndDraw();
         }
 
@@ -539,25 +539,25 @@ namespace ComPDFKit.Tool
 
         public Point GetStartPoint()
         {
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             return (baseLayer as CreateAnnotTool).GetStartPoint();
         }
 
         public Point GetEndPoint()
         {
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             return (baseLayer as CreateAnnotTool).GetEndPoint();
         }
 
         public List<Point> GetInkDrawPoints()
         {
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             return (baseLayer as CreateAnnotTool).GetInkDrawPoints();
         }
 
         public List<Point> GetMeasureDrawPoints()
         {
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             return (baseLayer as CreateAnnotTool).GetMeasureDrawPoints();
         }
 
@@ -568,7 +568,7 @@ namespace ComPDFKit.Tool
                 return new Rect();
             }
             Point point = Mouse.GetPosition(this);
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             return (baseLayer as CreateAnnotTool).GetMaxRect();
         }
 
@@ -579,7 +579,7 @@ namespace ComPDFKit.Tool
                 return;
             }
             Point point = Mouse.GetPosition(this);
-            BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+            BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
             (baseLayer as CreateAnnotTool).ClearDraw();
         }
 
@@ -810,7 +810,7 @@ namespace ComPDFKit.Tool
                         }
                     };
 
-                    BaseLayer createAnnotTool = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
+                    BaseLayer createAnnotTool = PDFViewer?.GetView(CreateAnnotTag) as CreateAnnotTool;
                     if (createAnnotTool != null)
                     {
                         createAnnotTool.Children.Add(textBorder);
@@ -931,7 +931,7 @@ namespace ComPDFKit.Tool
             {
                 return;
             }
-            BaseLayer removeLayer = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
+            BaseLayer removeLayer = PDFViewer?.GetView(CreateAnnotTag) as CreateAnnotTool;
             removeLayer.Children.Remove(textBorder);
         }
 

+ 1 - 1
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.PageSelected.cs

@@ -213,7 +213,7 @@ namespace ComPDFKit.Tool
             if (pageSelectedRect != null)
             {
                 Point point = Mouse.GetPosition(this);
-                BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
+                BaseLayer baseLayer = PDFViewer.GetViewForTag(CreateAnnotTag);
                 PDFViewer.GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
                 if (index < 0)
                 {

+ 27 - 14
Demo/Examples/ComPDFKit.Tool/DrawTool/CreateAnnotTool.cs

@@ -9,8 +9,6 @@ using ComPDFKit.Tool.UndoManger;
 using ComPDFKit.Viewer.Helper;
 using ComPDFKit.Viewer.Layer;
 using ComPDFKitViewer;
-using ComPDFKitViewer.Annot;
-using ComPDFKitViewer.BaseObject;
 using ComPDFKitViewer.Helper;
 using ComPDFKitViewer.Layer;
 using System;
@@ -18,19 +16,10 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Globalization;
 using System.Linq;
-using System.Net;
-using System.Text;
-using System.Threading.Tasks;
 using System.Windows;
-using System.Windows.Annotations;
 using System.Windows.Controls;
-using System.Windows.Input;
 using System.Windows.Media;
-using System.Windows.Media.Animation;
-using System.Windows.Media.Media3D;
-using System.Windows.Shapes;
 using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
-using static ComPDFKit.Tool.Help.ImportWin32;
 
 namespace ComPDFKit.Tool.DrawTool
 {
@@ -301,7 +290,17 @@ namespace ComPDFKit.Tool.DrawTool
                         DPIRect = drawRect = new Rect(mouseEndPoint.X, mouseEndPoint.Y, 32 * zoomFactor, 32 * zoomFactor);
                     }
                 }
-                Rect StandardRect = new Rect(
+
+                if (cPDFAnnotation is CPDFPolylineAnnotation)
+                {
+                    double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
+                    double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
+                    double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
+                    double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
+                    DPIRect = new Rect(left, top, right - left, bottom - top);
+                }
+
+                Rect standardRect = new Rect(
                         (DPIRect.Left - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (DPIRect.Top - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor,
                         DPIRect.Width / zoomFactor, DPIRect.Height / zoomFactor);
                 isDrawAnnot = false;
@@ -314,7 +313,7 @@ namespace ComPDFKit.Tool.DrawTool
                 cPDFAnnotation = null;
                 inkDrawPoints.Clear();
                 drawPoints.Clear();
-                return DpiHelper.StandardRectToPDFRect(StandardRect);
+                return DpiHelper.StandardRectToPDFRect(standardRect);
             }
             return new Rect();
         }
@@ -1488,7 +1487,10 @@ namespace ComPDFKit.Tool.DrawTool
         #endregion
         public void MultipleClick(Point downPoint)
         {
-            drawPoints.Add(downPoint);
+            if(!drawPoints.Contains(downPoint))
+            {
+                drawPoints.Add(downPoint);
+            }
         }
 
         public Rect GetMaxRect()
@@ -1953,5 +1955,16 @@ namespace ComPDFKit.Tool.DrawTool
             }
             return anglePoint;
         }
+
+        public bool IsCreateFinshed()
+        {
+            if (cPDFAnnotation is CPDFPolygonAnnotation)
+            {
+                if (drawPoints.Count <= 2)
+                    return false;
+            }
+
+            return true;
+        }
     }
 }