Forráskód Böngészése

ComPDFKit.Tool(win) - 文字编辑快捷键选中文字处理

liyuxuan 5 hónapja
szülő
commit
6d102c0938
1 módosított fájl, 213 hozzáadás és 16 törlés
  1. 213 16
      Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.TextEdit.cs

+ 213 - 16
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.TextEdit.cs

@@ -1,4 +1,5 @@
 using ComPDFKit.Import;
+using ComPDFKit.PDFDocument;
 using ComPDFKit.PDFPage;
 using ComPDFKit.PDFPage.Edit;
 using ComPDFKit.Tool.DrawTool;
@@ -12,6 +13,8 @@ using ComPDFKitViewer.Helper;
 using ComPDFKitViewer.Layer;
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
+using System.Net;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
@@ -55,6 +58,19 @@ namespace ComPDFKit.Tool
         public PointControlType ControlType { get; set; }
     }
 
+    public enum CEditingLocation
+    {
+        CEditingLocationLineBegin = 0,
+        CEditingLoadTypeLineEnd,
+        CEditingLoadTypeSectionBegin,
+        CEditingLoadTypeSectionEnd,
+        CEditingLoadTypePreWord,
+        CEditingLoadTypeNextWord,
+        CEditingLoadTypePreCharPlace,
+        CEditingLoadTypeNextCharPlace,
+        CEditingLoadTypeUpCharPlace,
+        CEditingLoadTypeDownCharPlace,
+    }
 
     public partial class CPDFViewerTool
     {
@@ -74,7 +90,7 @@ namespace ComPDFKit.Tool
         bool drawCaret = true;
         int selectedEditAreaIndex = -1;
         bool selectAllCharsForLine = false;
-
+        private CPoint RawHitPos { get; set; }
         private CPDFEditType contentEditType = CPDFEditType.EditText | CPDFEditType.EditImage;
 
         /// <summary>
@@ -658,6 +674,10 @@ namespace ComPDFKit.Tool
                         {
                             caretVisual.StopCaret();
                         }
+
+                        Point clickPoint = new Point((point.X - editObject.PageBound.X) / currentZoom, (point.Y - editObject.PageBound.Y) / currentZoom);
+                        Point rawPoint= DpiHelper.StandardPointToPDFPoint(clickPoint);
+                        RawHitPos = new CPoint((float)rawPoint.X, (float)rawPoint.Y);
                     }
                     else
                     {
@@ -825,40 +845,40 @@ namespace ComPDFKit.Tool
             switch (ke.Key)
             {
                 case Key.Left:
-                    if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
+                    if (CanMoveCaret())
                     {
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.GetPrevCharPlace();
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.ClearSelectChars();
                         DrawCaretVisualArea(currentEditAreaObject.cPDFEditArea as CPDFEditTextArea);
+                        ke.Handled = true;
                     }
-                    ke.Handled = true;
                     break;
                 case Key.Right:
-                    if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
+                    if (CanMoveCaret())
                     {
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.GetNextCharPlace();
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.ClearSelectChars();
                         DrawCaretVisualArea(currentEditAreaObject.cPDFEditArea as CPDFEditTextArea);
+                        ke.Handled = true;
                     }
-                    ke.Handled = true;
                     break;
                 case Key.Up:
-                    if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
+                    if (CanMoveCaret())
                     {
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.GetUpCharPlace();
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.ClearSelectChars();
                         DrawCaretVisualArea(currentEditAreaObject.cPDFEditArea as CPDFEditTextArea);
+                        ke.Handled = true;
                     }
-                    ke.Handled = true;
                     break;
                 case Key.Down:
-                    if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
+                    if (CanMoveCaret())
                     {
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.GetDownCharPlace();
                         (currentEditAreaObject.cPDFEditArea as CPDFEditTextArea)?.ClearSelectChars();
                         DrawCaretVisualArea(currentEditAreaObject.cPDFEditArea as CPDFEditTextArea);
+                        ke.Handled = true;
                     }
-                    ke.Handled = true;
                     break;
                 default:
                     {
@@ -903,6 +923,25 @@ namespace ComPDFKit.Tool
             }
         }
 
+        private bool CanMoveCaret()
+        {
+            if(currentEditAreaObject == null || currentEditAreaObject.cPDFEditPage == null)
+            {
+                return false;
+            }
+
+            if(currentEditAreaObject.ControlType != PointControlType.Body || currentEditAreaObject.cPDFEditArea== null)
+            {
+                return false; 
+            }
+
+            if(currentEditAreaObject.cPDFEditArea.Type!=CPDFEditType.EditText)
+            {
+                return false;
+            }
+            return true;
+        }
+
         /// <summary>
         /// Text input event
         /// </summary>
@@ -1367,14 +1406,57 @@ namespace ComPDFKit.Tool
             }
         }
 
-        public void MoveEditArea(Point moveOffset, CPDFEditArea editArea)
+        public bool MoveEditArea(Point moveOffset)
         {
-            CRect cRect = editArea.GetFrame();
-            Rect rect = DataConversionForWPF.CRectConversionForRect(cRect);
-            rect.X += moveOffset.X;
-            rect.Y += moveOffset.Y;
-            editArea.SetFrame(DataConversionForWPF.RectConversionForCRect(rect));
-            UpdateSelectRect(editArea);
+            EditAreaObject areaObj = currentEditAreaObject;
+
+            if (PDFViewer == null || GetToolType() != ToolType.ContentEdit)
+            {
+                return false;
+            }
+
+            if (areaObj == null || areaObj.cPDFEditArea == null)
+            {
+                return false;
+            }
+
+            CPDFDocument pdfDoc = PDFViewer.GetDocument();
+            if (pdfDoc == null)
+            {
+                return false;
+            }
+            CPDFPage pdfPage = pdfDoc.PageAtIndex(areaObj.PageIndex);
+            if (pdfPage == null)
+            {
+                return false;
+            }
+            try
+            {
+                CPDFEditArea editArea = areaObj.cPDFEditArea;
+                CRect cRect = editArea.GetFrame();
+                Rect rect = DataConversionForWPF.CRectConversionForRect(cRect);
+                Rect preRect = rect;
+                rect.X = Math.Min(pdfPage.PageSize.width - rect.Width, rect.X + moveOffset.X);
+                rect.Y = Math.Min(pdfPage.PageSize.height - rect.Height, rect.Y + moveOffset.Y);
+
+                rect.X = Math.Max(rect.X, 0);
+                rect.Y = Math.Max(rect.Y, 0);
+
+                editArea.SetFrame(DataConversionForWPF.RectConversionForCRect(rect));
+                UpdateSelectRect(editArea);
+                if ((int)preRect.Left != (int)rect.Left ||
+                    (int)preRect.Top != (int)rect.Top ||
+                    (int)preRect.Width != (int)rect.Width ||
+                    (int)preRect.Height != (int)rect.Height)
+                {
+                    PDFViewer.UpdateRenderFrame();
+                }
+                return true;
+            }
+            catch (Exception ex)
+            {
+            }
+            return false;
         }
 
         public void CropImage(CPDFEditImageArea cPDFEditImageArea)
@@ -1468,6 +1550,7 @@ namespace ComPDFKit.Tool
             CPoint cursorCPoint = new CPoint(0, 0);
             CPoint highCpoint = new CPoint(0, 0);
             textArea.GetTextCursorPoints(ref cursorCPoint, ref highCpoint);
+            RawHitPos = cursorCPoint;
             CaretVisual caretVisual = CommonHelper.FindVisualChild<CaretVisual>(PDFViewer.GetViewForTag(textEditTag));
 
             Point cursorPoint = DataConversionForWPF.CPointConversionForPoint(cursorCPoint);
@@ -1519,5 +1602,119 @@ namespace ComPDFKit.Tool
             caretVisual.Draw(true, false);
             caretVisual.StopCaret();
         }
+
+
+        /// <summary>
+        /// Jump cursor to a specific position in a text area.
+        /// </summary>
+        /// <param name="editingLocation">Cursor position.</param>
+        /// <param name="isSelectRanage"> Whether to select text from the current cursor position till the end of the cursor position.</param>
+        public void JumpEditingLoction(CEditingLocation editingLocation, bool isSelectRanage)
+        {
+            EditAreaObject areaObj = currentEditAreaObject;
+
+            if (areaObj == null || !(areaObj.cPDFEditArea is CPDFEditTextArea))
+            {
+                return;
+            }
+            if (PDFViewer == null)
+            {
+                return;
+            }
+
+            CPDFEditTextArea textArea = areaObj.cPDFEditArea as CPDFEditTextArea;
+
+            try
+            {
+                switch (editingLocation)
+                {
+                    case CEditingLocation.CEditingLocationLineBegin:
+                        textArea.GetLineBeginCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypeLineEnd:
+                        textArea.GetLineEndCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypeSectionBegin:
+                        textArea.GetSectionBeginCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypeSectionEnd:
+                        textArea.GetSectionEndCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypePreWord:
+                        textArea.GetPreWordCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypeNextWord:
+                        textArea.GetNextWordCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypePreCharPlace:
+                        textArea.GetPrevCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypeNextCharPlace:
+                        textArea.GetNextCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypeUpCharPlace:
+                        textArea.GetUpCharPlace();
+                        break;
+                    case CEditingLocation.CEditingLoadTypeDownCharPlace:
+                        textArea.GetDownCharPlace();
+                        break;
+                    default:
+                        return;
+                }
+
+                CPoint cursorPoint = new CPoint(-1, -1);
+                CPoint pointHigh = new CPoint(0, 0);
+
+                textArea.GetTextCursorPoints(ref cursorPoint, ref pointHigh);
+                textArea.ClearSelectChars();
+
+                Rect caretRect = new Rect(new Point(cursorPoint.x, cursorPoint.y), new Point(pointHigh.x, pointHigh.y));
+                Point endPoint = DpiHelper.PDFPointToStandardPoint(new Point(caretRect.Left, (caretRect.Top + caretRect.Bottom) / 2));
+                Point startPoint = DpiHelper.PDFPointToStandardPoint(new Point(RawHitPos.x, RawHitPos.y));
+
+                startPoint.X = startPoint.X * currentZoom;
+                startPoint.Y = startPoint.Y * currentZoom;
+
+                endPoint.X = endPoint.X * currentZoom;
+                endPoint.Y = endPoint.Y * currentZoom;
+
+                if (isSelectRanage)
+                {
+                    SelectText(textArea, startPoint, endPoint);
+                    CleanDraw();
+                }
+                else
+                {
+                    DrawCaretVisualArea(currentEditAreaObject.cPDFEditArea as CPDFEditTextArea);
+                    RawHitPos = cursorPoint;
+                }
+
+                Point caretPoint = new Point(endPoint.X + areaObj.PageBound.Left, endPoint.Y + areaObj.PageBound.Top);
+                int direction = 1;
+                if (caretPoint.X < 0 || caretPoint.X > PDFViewer.ViewportWidth)
+                {
+                    if (caretPoint.X < 0)
+                    {
+                        direction = -1;
+                    }
+                    double horizontal = PDFViewer.HorizontalOffset + PDFViewer.ViewportWidth / 2 * direction;
+                    PDFViewer.SetHorizontalOffset(horizontal);
+                }
+                if (caretPoint.Y < 0 || caretPoint.Y > PDFViewer.ViewportHeight)
+                {
+                    if (caretPoint.Y < 0)
+                    {
+                        direction = -1;
+                    }
+                    double vertical = PDFViewer.VerticalOffset + PDFViewer.ViewportHeight / 2 * direction;
+                    PDFViewer.SetVerticalOffset(vertical);
+                }
+
+            }
+            catch (Exception ex) 
+            {
+
+            }
+        }
     }
 }