Browse Source

ComPDFKit.Tool(Win) - FreeText虚线框支持

liyuxuan 6 months ago
parent
commit
e1fed2ee81

+ 19 - 6
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.Annot.cs

@@ -321,7 +321,7 @@ namespace ComPDFKit.Tool
             }
             Point point = Mouse.GetPosition(this);
             BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
-           (baseLayer as CreateAnnotTool).CreateTextBox();
+            (baseLayer as CreateAnnotTool).CreateTextBox();
         }
 
         public Rect EndDrawAnnot()
@@ -512,7 +512,7 @@ namespace ComPDFKit.Tool
                             break;
                         case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
                             {
-                                LinkAnnotHistory linkHistory= new LinkAnnotHistory();
+                                LinkAnnotHistory linkHistory = new LinkAnnotHistory();
                                 AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
                                 linkHistory.Action = HistoryAction.Add;
                                 linkHistory.CurrentParam = (LinkParam)annotParam;
@@ -600,7 +600,7 @@ namespace ComPDFKit.Tool
                 {
                     return;
                 }
-                AnnotData annotData = currentAnnot.GetAnnotData(); 
+                AnnotData annotData = currentAnnot.GetAnnotData();
 
                 if (annotData.PaintRect == Rect.Empty)
                 {
@@ -640,7 +640,7 @@ namespace ComPDFKit.Tool
                     double Height = DpiHelper.PDFNumToStandardNum(drawRect.height());
                     textui = new TextBox();
                     textui.Name = "PdfViewerTextBox";
-                    textBorder = new Border();
+                    textBorder = new DashedBorder();
                     textBorder.Child = textui;
                     textBorder.MinWidth = Width * PDFViewer.GetZoom();
                     textBorder.MinHeight = Height * PDFViewer.GetZoom();
@@ -678,9 +678,22 @@ namespace ComPDFKit.Tool
 
                     textBorder.Padding = new Thickness(0);
                     textBorder.BorderBrush = new SolidColorBrush(borderColor);
-                    textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(textWidget.GetBorderWidth() * annotData.CurrentZoom));
+                    double rawWidth = textWidget.GetBorderWidth();
+                    double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * annotData.CurrentZoom);
+                    textBorder.BorderThickness = new Thickness(drawWidth);
                     textui.BorderThickness = new Thickness(0);
                     textui.Text = textWidget.Content;
+                    if (textWidget.BorderStyle != C_BORDER_STYLE.BS_SOLID && textWidget.Dash != null && textWidget.Dash.Length > 0)
+                    {
+                        //补充保存虚线样式
+                        DashedBorder dashBorder = textBorder as DashedBorder;
+                        DoubleCollection dashCollection = new DoubleCollection();
+                        foreach (float num in textWidget.Dash)
+                        {
+                            dashCollection.Add(num);
+                        }
+                        dashBorder?.DrawDashBorder(true, drawWidth, rawWidth, dashCollection);
+                    }
 
                     string fontName = string.Empty;
                     string fontFamily = string.Empty;
@@ -794,7 +807,7 @@ namespace ComPDFKit.Tool
                                 viewer.UpdateAnnotFrame();
                             }
                             RemovePopTextUI();
-                        } 
+                        }
                     };
 
                     BaseLayer createAnnotTool = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;

+ 7 - 1
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.DataMethod.cs

@@ -258,7 +258,6 @@ namespace ComPDFKit.Tool
             {
                 freeTextAnnotation.SetLineColor(FreeTextParam.LineColor);
             }
-            freeTextAnnotation.SetBorderWidth(1);
             freeTextAnnotation.SetTransparency(FreeTextParam.Transparency);
             freeTextAnnotation.SetLineWidth((float)FreeTextParam.LineWidth);
 
@@ -266,6 +265,13 @@ namespace ComPDFKit.Tool
             {
                 freeTextAnnotation.SetBgColor(FreeTextParam.BgColor);
             }
+
+            if(FreeTextParam.Dash!=null && FreeTextParam.Dash.Length>0)
+            {
+                //补充保存虚线样式
+                freeTextAnnotation.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, FreeTextParam.Dash);
+            }
+
             CTextAttribute textAttr = new CTextAttribute();
             textAttr.FontColor = FreeTextParam.FontColor;
             textAttr.FontSize = (float)FreeTextParam.FontSize;

+ 1 - 0
Demo/Examples/ComPDFKit.Tool/ComPDFKit.Tool.csproj

@@ -120,6 +120,7 @@
     <Compile Include="DrawTool\CreateAnnotTool.cs" />
     <Compile Include="DrawTool\CreateCustomizeTool.cs" />
     <Compile Include="DrawTool\CreateWidgetTool.cs" />
+    <Compile Include="DrawTool\DashBorder.cs" />
     <Compile Include="DrawTool\FrameSelectTool.cs" />
     <Compile Include="DrawTool\MultiSelectedRect.cs" />
     <Compile Include="DrawTool\PageSelectedRect.cs" />

+ 30 - 3
Demo/Examples/ComPDFKit.Tool/DrawTool/CreateAnnotTool.cs

@@ -28,6 +28,7 @@ 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;
 
@@ -1463,7 +1464,7 @@ namespace ComPDFKit.Tool.DrawTool
 
                     TextBox textui = new TextBox();
 
-                    Border textBorder = new Border();
+                    DashedBorder textBorder = new DashedBorder();
                     textBorder.Child = textui;
                     textui.Width = 200;
                     CTextAttribute textAttribute = annotFreeText.FreeTextDa;
@@ -1498,7 +1499,20 @@ namespace ComPDFKit.Tool.DrawTool
 
                     textBorder.Padding = new Thickness(0);
                     textBorder.BorderBrush = new SolidColorBrush(borderColor);
-                    textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(annotFreeText.GetBorderWidth() * zoomFactor));
+                    double rawWidth = annotFreeText.GetBorderWidth();
+                    double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * zoomFactor);
+                    textBorder.BorderThickness = new Thickness(drawWidth);
+                    if (annotFreeText.BorderStyle != C_BORDER_STYLE.BS_SOLID && annotFreeText.Dash != null && annotFreeText.Dash.Length > 0)
+                    {
+                        //补充保存虚线样式
+                        DoubleCollection dashCollection = new DoubleCollection();
+                        foreach (float num in annotFreeText.Dash)
+                        {
+                            dashCollection.Add(num);
+                        }
+                        textBorder?.DrawDashBorder(true, drawWidth,rawWidth, dashCollection);
+                    }
+
                     textui.BorderThickness = new Thickness(0);
                     textui.Text = annotFreeText.Content;
 
@@ -1724,10 +1738,23 @@ namespace ComPDFKit.Tool.DrawTool
                     lastTextui.Background = new SolidColorBrush(backgroundColor);
                     lastTextBorder.Padding = new Thickness(0);
                     lastTextBorder.BorderBrush = new SolidColorBrush(borderColor);
-                    lastTextBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(annotFreeText.GetBorderWidth() * zoomFactor));
+                    double rawWidth = annotFreeText.GetBorderWidth();
+                    double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * zoomFactor);
+                    lastTextBorder.BorderThickness = new Thickness(drawWidth);
                     lastTextui.BorderThickness = new Thickness(0);
                     lastTextui.Text = annotFreeText.Content;
                     lastTextui.Opacity = annotFreeText.Transparency;
+                    if (annotFreeText.BorderStyle != C_BORDER_STYLE.BS_SOLID && annotFreeText.Dash != null && annotFreeText.Dash.Length > 0)
+                    {
+                        //补充保存虚线样式
+                        DashedBorder dashBorder = (DashedBorder)lastTextBorder;
+                        DoubleCollection dashCollection = new DoubleCollection();
+                        foreach (float num in annotFreeText.Dash)
+                        {
+                            dashCollection.Add(num);
+                        }
+                        dashBorder.DrawDashBorder(true, drawWidth,rawWidth, dashCollection);
+                    }
 
                     string fontName = string.Empty;
                     string fontFamily = string.Empty;

+ 47 - 0
Demo/Examples/ComPDFKit.Tool/DrawTool/DashBorder.cs

@@ -0,0 +1,47 @@
+using ComPDFKitViewer.Annot;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Media;
+using System.Windows.Shapes;
+
+namespace ComPDFKit.Tool.DrawTool
+{
+    internal class DashedBorder : Border
+    {
+        private bool DrawDash {  get; set; }
+        private double DashThickness {  get; set; }
+        private double DashWidth { get; set; }
+        private DoubleCollection DashArray { get; set; }
+        protected override void OnRender(DrawingContext dc)
+        {
+            if (DrawDash)
+            {
+                Pen dashPen = new Pen(BorderBrush, DashThickness);
+                dashPen.DashCap = PenLineCap.Flat;
+                DashStyle dash = new DashStyle();
+                foreach (double offset in DashArray)
+                {
+                    dash.Dashes.Add(offset / DashWidth);
+                }
+                dashPen.DashStyle = dash;
+                dc.DrawRectangle(null, dashPen, new Rect(0, 0, ActualWidth, ActualHeight));
+                return;
+            }
+            base.OnRender(dc);
+        }
+        public void DrawDashBorder(bool isDash,double dashWidth,double rawWidth, DoubleCollection dashArray)
+        {
+            DrawDash = isDash;
+            DashArray = dashArray;
+            DashThickness=dashWidth;
+            DashWidth=rawWidth;
+            InvalidateVisual();
+        }
+    }
+}

+ 7 - 0
Demo/Examples/ComPDFKit.Tool/Help/ParamConverter.cs

@@ -9,6 +9,7 @@ using ComPDFKit.PDFPage.Edit;
 using ComPDFKit.Tool.SettingParam;
 using ComPDFKit.Tool.UndoManger;
 using ComPDFKit.Viewer.Helper;
+using ComPDFKitViewer.Annot;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
@@ -1386,6 +1387,12 @@ namespace ComPDFKit.Tool.Help
                 freetextParam.Alignment = freetextAnnot.Alignment;
             }
 
+            if (freetextAnnot.Dash != null && freetextAnnot.Dash.Length > 0)
+            {
+                freetextParam.Dash = new float[freetextAnnot.Dash.Length];
+                freetextAnnot.Dash.CopyTo(freetextParam.Dash, 0);
+            }
+
             GetAnnotCommonParam(freetextAnnot, freetextParam);
             return freetextParam;
         }

+ 9 - 1
Demo/Examples/ComPDFKit.Tool/SettingParam/AnnotParam/FreeTextParam.cs

@@ -21,10 +21,10 @@ namespace ComPDFKit.Tool
         public string FontName { get; set; } = string.Empty;
         public double FontSize { get; set; }
         public C_TEXT_ALIGNMENT Alignment {  get; set; }
+        public float[] Dash { get; set; }
 
         public override bool CopyTo(AnnotParam transfer)
         {
-            
             FreeTextParam freetextTransfer = transfer as FreeTextParam;
             if (freetextTransfer == null)
             {
@@ -51,6 +51,14 @@ namespace ComPDFKit.Tool
                 freetextTransfer.FontColor = (byte[])FontColor.Clone();
             }
 
+            freetextTransfer.Dash = null;
+            if (Dash != null)
+            {
+                float[] DashCopy = new float[Dash.Length];
+                Dash.CopyTo(DashCopy, 0);
+                freetextTransfer.Dash = DashCopy;
+            }
+
             freetextTransfer.HasBgColor = HasBgColor;
             freetextTransfer.LineWidth = LineWidth;
             freetextTransfer.IsBold = IsBold;

+ 16 - 0
Demo/Examples/ComPDFKit.Tool/UndoManger/AnnotHistory/FreeTextAnnotHistory.cs

@@ -1,6 +1,7 @@
 using ComPDFKit.PDFAnnotation;
 using ComPDFKit.PDFPage;
 using ComPDFKit.Tool.Help;
+using ComPDFKitViewer.Annot;
 using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
 
 namespace ComPDFKit.Tool.UndoManger
@@ -98,6 +99,12 @@ namespace ComPDFKit.Tool.UndoManger
                 {
                     textAnnot.SetContent(currentParam.Content);
                 }
+
+                if (currentParam.Dash != null && currentParam.Dash.Length > 0)
+                {
+                    textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, currentParam.Dash);
+                }
+
                 textAnnot.SetIsLocked(currentParam.Locked);
                 textAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
                 textAnnot.UpdateAp();
@@ -238,6 +245,15 @@ namespace ComPDFKit.Tool.UndoManger
                     textAnnot.SetIsLocked(updateParam.Locked);
                 }
 
+                if (updateParam.Dash != null && updateParam.Dash.Length > 0)
+                {
+                    textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, updateParam.Dash);
+                }
+                else
+                {
+                    textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
+                }
+
                 textAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
                 textAnnot.UpdateAp();