123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using ComPDFKitViewer;
- using ComPDFKitViewer.Layer;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media;
- namespace ComPDFKit.Tool.DrawTool
- {
- internal class AnnotSelector:BaseLayer
- {
- public CPDFViewer PDFViewer { get; set; }
- public List<AnnotData> SelectAnnots { get; set; } = new List<AnnotData>();
- public Pen DrawPen { get; set; }
- public Brush DrawBrush { get; set; }
- private List<Point> ControlPoints { get; set; } = new List<Point>();
- private double RectPadding = 4;
- public AnnotSelector()
- {
- DrawPen = new Pen(new SolidColorBrush(Color.FromArgb(255, 71, 126, 222)), 2);
- DrawBrush = new SolidColorBrush(Color.FromArgb(40, 119, 180, 227));
- }
- public override void Draw()
- {
- DrawingContext drawDC = Open();
- DrawOutBound(drawDC);
- DrawIndividual(drawDC);
- DrawControlPoint(drawDC);
- Present();
- }
- public int GetSelectCount()
- {
- if(SelectAnnots!=null)
- {
- return SelectAnnots.Count;
- }
- return 0;
- }
- public bool AddItem(AnnotData item)
- {
- if (item == null)
- {
- return false;
- }
- List<int> pageIndexList = SelectAnnots.Select(x => x.PageIndex).Distinct().ToList();
- if(pageIndexList.Count > 1 )
- {
- SelectAnnots.Clear();
- }
- if( pageIndexList.Count==1 && pageIndexList[0]!=item.PageIndex)
- {
- SelectAnnots.Clear();
- }
- if (RemoveItem(item))
- {
- SelectAnnots.Add(item);
- return true;
- }
- return false;
- }
- public bool HasItem(AnnotData item)
- {
- if (item == null)
- {
- return false;
- }
- if(SelectAnnots!=null && SelectAnnots.Count > 0)
- {
- return SelectAnnots.Where(x => x.AnnotIndex == item.AnnotIndex && x.PageIndex == item.PageIndex).Count() > 0;
- }
- return false;
- }
- public bool RemoveItem(AnnotData item)
- {
- if (item == null || item.PageIndex < 0 || item.AnnotIndex < 0)
- {
- return false;
- }
- List<AnnotData> checkList = SelectAnnots.Where(x => x.AnnotIndex == item.AnnotIndex && x.PageIndex == item.PageIndex).ToList();
- if (checkList != null && checkList.Count > 0)
- {
- foreach (AnnotData removeItem in checkList)
- {
- SelectAnnots.Remove(removeItem);
- }
- }
- return true;
- }
- public void ClearItem()
- {
- SelectAnnots?.Clear();
- }
- public Rect GetOutBoundRect()
- {
- if (SelectAnnots.Count > 0)
- {
- double left = 0;
- double top = 0;
- double right = 0;
- double bottom = 0;
- for (int i=0;i< SelectAnnots.Count;i++)
- {
- AnnotData checkItem= SelectAnnots[i];
- if (i == 0)
- {
- left=checkItem.PaintRect.Left;
- top=checkItem.PaintRect.Top;
- right=checkItem.PaintRect.Right;
- bottom=checkItem.PaintRect.Bottom;
- continue;
- }
- left = Math.Min(left, checkItem.PaintRect.Left);
- top = Math.Min(top, checkItem.PaintRect.Top);
- right = Math.Max(right, checkItem.PaintRect.Right);
- bottom = Math.Max(bottom, checkItem.PaintRect.Bottom);
- }
- if(right<left || bottom<top)
- {
- return Rect.Empty;
- }
- return new Rect(left, top, right-left, bottom-top);
- }
-
- return Rect.Empty;
- }
- public void DrawOutBound(DrawingContext drawDC)
- {
- Rect boudRect = GetOutBoundRect();
- if (boudRect.IsEmpty || boudRect.Width == 0 || boudRect.Height == 0)
- {
- return;
- }
- drawDC.DrawRectangle(Brushes.Transparent, DrawPen, boudRect);
- }
- public void DrawIndividual(DrawingContext drawDC)
- {
- if(SelectAnnots==null || SelectAnnots.Count==0)
- {
- return;
- }
- foreach(AnnotData checkItem in SelectAnnots)
- {
- drawDC.DrawRectangle(null, DrawPen, checkItem.PaintRect);
- }
- }
- public void DrawControlPoint(DrawingContext drawDC)
- {
- Rect boudRect = GetOutBoundRect();
- ControlPoints.Clear();
- double centerX = (boudRect.Left + boudRect.Right) / 2;
- double centerY = (boudRect.Top + boudRect.Bottom) / 2;
- ControlPoints.Add(new Point(boudRect.Left, boudRect.Top));
- ControlPoints.Add(new Point(boudRect.Left, centerY));
- ControlPoints.Add(new Point(boudRect.Left, boudRect.Bottom));
- ControlPoints.Add(new Point(centerX, boudRect.Bottom));
- ControlPoints.Add(new Point(boudRect.Right, boudRect.Bottom));
- ControlPoints.Add(new Point(boudRect.Right, centerY));
- ControlPoints.Add(new Point(boudRect.Right, boudRect.Top));
- ControlPoints.Add(new Point(centerX, boudRect.Top));
- foreach (Point point in ControlPoints)
- {
- drawDC.DrawRectangle(Brushes.White, DrawPen, new Rect(point.X- RectPadding,point.Y- RectPadding, RectPadding*2, RectPadding*2));
- }
- }
- public PointControlType GetHitControlIndex(Point point)
- {
- for (int i = 0; i < ControlPoints.Count; i++)
- {
- Point checkPoint = ControlPoints[i];
- Rect checkBound = new Rect(checkPoint.X - RectPadding, checkPoint.Y - RectPadding, RectPadding * 2, RectPadding * 2);
- if (checkBound.Contains(point))
- {
- return (PointControlType)i;
- }
- }
- Rect outBound = GetOutBoundRect();
- if (outBound.Contains(point))
- {
- return PointControlType.Body;
- }
- return PointControlType.None;
- }
- }
- }
|