123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool.Help;
- using ComPDFKit.Tool.UndoManger;
- using ComPDFKitViewer;
- using ComPDFKitViewer.Annot;
- using ComPDFKitViewer.Helper;
- 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.Input;
- using System.Windows.Media;
- namespace ComPDFKit.Tool.FillTool
- {
- internal class FillCreateTool : BaseLayer
- {
- internal CPDFViewer PDFViewer { get; set; }
- internal int PageIndex { get; set; } = -1;
- internal Point MouseDownPos { get; set; } = new Point(-1, -1);
- internal Point CurrentMousePos { get; set; } = new Point(-1, -1);
- internal Rect PageBound { get; set; } = Rect.Empty;
- internal FillItem FillParam { get; private set; }
- internal bool IsMouseMoved { get; set; }
- private Rect DrawBound { get; set; }
- private double DPIScale { get; set; } = 96D / 72D;
- public override void Draw()
- {
- if (FillParam == null || PDFViewer == null)
- {
- return;
- }
- Point mousePos = Mouse.GetPosition(PDFViewer);
- Rect viewRect = new Rect(0, 0, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
- if (viewRect.Contains(mousePos) == false && Mouse.LeftButton == MouseButtonState.Released)
- {
- return;
- }
- if (CurrentMousePos.X == -1 || CurrentMousePos.Y == -1)
- {
- return;
- }
- DrawingContext drawDC = Open();
- switch (FillParam.Type)
- {
- case FillType.Square:
- DrawRect(drawDC);
- break;
- case FillType.Circle:
- DrawCircle(drawDC);
- break;
- case FillType.Line:
- DrawLine(drawDC);
- break;
- case FillType.Cross:
- DrawCross(drawDC);
- break;
- case FillType.Check:
- DrawCheck(drawDC);
- break;
- default:
- break;
- }
-
- Present();
- }
- internal void SetFillParam(FillItem param)
- {
- FillParam = param;
- }
- private void DrawCircle(DrawingContext drawDC)
- {
- if (FillParam == null || FillParam.Param == null)
- {
- return;
- }
- CircleParam circleParam = FillParam.Param as CircleParam;
- if (circleParam == null)
- {
- return;
- }
- try
- {
- double zoom = PDFViewer.GetZoom();
- SolidColorBrush fillBrush = Brushes.Black;
- if (circleParam.BgColor != null && circleParam.BgColor.Length == 3)
- {
- fillBrush = new SolidColorBrush(Color.FromRgb(circleParam.BgColor[0], circleParam.BgColor[1], circleParam.BgColor[2]));
- }
- if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
- {
- double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
- xPos = Math.Min(xPos, PageBound.Right);
- double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
- yPos = Math.Min(yPos, PageBound.Bottom);
- double centerX = (MouseDownPos.X + xPos) / 2;
- double centerY = (MouseDownPos.Y + yPos) / 2;
- double radioX = Math.Abs((MouseDownPos.X - xPos) / 2);
- double radioY = Math.Abs((MouseDownPos.Y - yPos) / 2);
- if (FillParam.KeepRate)
- {
- double minRadio = Math.Min(radioX, radioY);
- radioX = minRadio;
- radioY = minRadio;
- centerX = MouseDownPos.X + ((xPos - MouseDownPos.X) >= 0 ? 1 : -1) * radioX;
- centerY = MouseDownPos.Y + ((yPos - MouseDownPos.Y) >= 0 ? 1 : -1) * radioX;
- }
- DrawBound = new Rect(centerX - radioX, centerY - radioY, radioX * 2, radioY * 2);
- drawDC.DrawEllipse(fillBrush, null, new Point(centerX, centerY), radioX, radioY);
- return;
- }
- DrawBound = new Rect(CurrentMousePos.X - FillParam.Width * zoom / 2, CurrentMousePos.Y - FillParam.Height * zoom / 2, FillParam.Width * zoom, FillParam.Height * zoom);
- drawDC.DrawEllipse(fillBrush, null, CurrentMousePos, FillParam.Width * zoom/2, FillParam.Height * zoom/2);
- }
- catch (Exception ex)
- {
- }
- }
- private void DrawRect(DrawingContext drawDC)
- {
- if (FillParam == null || FillParam.Param == null)
- {
- return;
- }
- SquareParam squareParam = FillParam.Param as SquareParam;
- if (squareParam == null)
- {
- return;
- }
- try
- {
- double zoom = PDFViewer.GetZoom();
- SolidColorBrush lineBrush = Brushes.Black;
- if (squareParam.LineColor != null && squareParam.LineColor.Length == 3)
- {
- lineBrush = new SolidColorBrush(Color.FromRgb(squareParam.LineColor[0], squareParam.LineColor[1], squareParam.LineColor[2]));
- }
- Pen borderPen = new Pen(lineBrush, squareParam.LineWidth * zoom * DPIScale);
- int halfPenWidth = (int)Math.Ceiling(borderPen.Thickness / 2);
- if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
- {
- double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
- xPos = Math.Min(xPos, PageBound.Right);
- double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
- yPos = Math.Min(yPos, PageBound.Bottom);
- double centerX = (MouseDownPos.X + xPos) / 2;
- double centerY = (MouseDownPos.Y + yPos) / 2;
- double radioX = Math.Abs((MouseDownPos.X - xPos) / 2);
- double radioY = Math.Abs((MouseDownPos.Y - yPos) / 2);
- if (FillParam.KeepRate)
- {
- double minRadio = Math.Min(radioX, radioY);
- radioX = minRadio;
- radioY = minRadio;
- centerX = MouseDownPos.X + ((xPos - MouseDownPos.X) >= 0 ? 1 : -1) * radioX;
- centerY = MouseDownPos.Y + ((yPos - MouseDownPos.Y) >= 0 ? 1 : -1) * radioY;
- }
- DrawBound = new Rect(centerX - radioX, centerY - radioY, radioX * 2, radioY * 2);
- drawDC.DrawRectangle(null, borderPen, new Rect(DrawBound.Left + halfPenWidth, DrawBound.Top + halfPenWidth, DrawBound.Width - 2 * halfPenWidth, DrawBound.Height - 2 * halfPenWidth));
- return;
- }
- DrawBound = new Rect(
- CurrentMousePos.X - FillParam.Width / 2 * zoom,
- CurrentMousePos.Y - FillParam.Height / 2 * zoom,
- FillParam.Width * zoom, FillParam.Height * zoom);
- drawDC.DrawRectangle(null, borderPen, new Rect(DrawBound.Left + halfPenWidth, DrawBound.Top + halfPenWidth, DrawBound.Width - 2 * halfPenWidth, DrawBound.Height - 2 * halfPenWidth));
- }
- catch (Exception ex)
- {
- }
- }
- private void DrawLine(DrawingContext drawDC)
- {
- if (FillParam == null || FillParam.Param == null)
- {
- return;
- }
- LineParam lineParam = FillParam.Param as LineParam;
- if (lineParam == null)
- {
- return;
- }
- try
- {
- double zoom = PDFViewer.GetZoom();
- SolidColorBrush lineBrush = Brushes.Black;
- if (lineParam.LineColor != null && lineParam.LineColor.Length == 3)
- {
- lineBrush = new SolidColorBrush(Color.FromRgb(lineParam.LineColor[0], lineParam.LineColor[1], lineParam.LineColor[2]));
- }
- Pen borderPen = new Pen(lineBrush, lineParam.LineWidth * zoom);
- if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
- {
- double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
- xPos = Math.Min(xPos, PageBound.Right);
- DrawBound = new Rect(new Point(xPos, MouseDownPos.Y), MouseDownPos);
- drawDC.DrawLine(borderPen, new Point(xPos, MouseDownPos.Y), MouseDownPos);
- return;
- }
- Point leftPos = new Point(
- CurrentMousePos.X - FillParam.Width / 2 * zoom,
- CurrentMousePos.Y);
- Point rightPos = new Point(
- CurrentMousePos.X + FillParam.Width / 2 * zoom,
- CurrentMousePos.Y);
- DrawBound = new Rect(leftPos, rightPos);
- drawDC.DrawLine(borderPen, leftPos, rightPos);
- }
- catch (Exception ex)
- {
- }
- }
- private void GetPointsBound(List<CPoint> checkList, out Rect checkBound)
- {
- checkBound = new Rect(0, 0, 0, 0);
- if (checkList == null || checkList.Count == 0)
- {
- return;
- }
- int left = 0;
- int top = 0;
- int right = 0;
- int bottom = 0;
- foreach (CPoint point in checkList)
- {
- left = (int)Math.Min(left, point.x);
- top = (int)Math.Min(t, point.y);
- right = (int)Math.Max(right, point.x);
- bottom = (int)Math.Max(bottom, point.y);
- }
- checkBound = new Rect(left, top, right - left, bottom - top);
- }
- private void DrawCheck(DrawingContext drawDC)
- {
- if (FillParam == null || FillParam.Param == null)
- {
- return;
- }
- InkParam inkParam = FillParam.Param as InkParam;
- if (inkParam == null)
- {
- return;
- }
- try
- {
- double zoom = PDFViewer.GetZoom();
- SolidColorBrush inkBrush = Brushes.Black;
- if (inkParam.InkColor != null && inkParam.InkColor.Length == 3)
- {
- inkBrush = new SolidColorBrush(Color.FromRgb(inkParam.InkColor[0], inkParam.InkColor[1], inkParam.InkColor[2]));
- }
- Pen borderPen = new Pen(inkBrush, inkParam.Thickness * zoom);
- borderPen.StartLineCap=PenLineCap.Round;
- borderPen.EndLineCap=PenLineCap.Round;
- if (inkParam.InkPath == null || inkParam.InkPath.Count == 0)
- {
- int width = Math.Max(0, FillParam.Width);
- int height = Math.Max(0, FillParam.Height);
- if (width == 0 || height == 0)
- {
- width = 30;
- height = 30;
- }
- List<List<CPoint>> defaultList = new List<List<CPoint>>();
- List<CPoint> pointList = new List<CPoint>();
- defaultList.Add(pointList);
- pointList.Add(new CPoint(0, height * 2 / 3));
- pointList.Add(new CPoint(width / 3, height));
- pointList.Add(new CPoint(width, height / 3));
- inkParam.InkPath = defaultList;
- }
- if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
- {
- double minX = 0;
- double minY = 0;
- double maxX = 0;
- double maxY = 0;
- foreach (List<CPoint> loopItems in inkParam.InkPath)
- {
- GetPointsBound(loopItems, out Rect checkBound);
- minX = Math.Min(minX, checkBound.X);
- minY = Math.Min(minY, checkBound.Y);
- maxX = Math.Max(maxX, checkBound.Right);
- maxY = Math.Max(maxY, checkBound.Bottom);
- }
- Rect prevRect = new Rect(minX, minY, maxX - minX, maxY - minY);
- if (prevRect.Width == 0 || prevRect.Height == 0)
- {
- return;
- }
- List<List<Point>> drawPathList = new List<List<Point>>();
- foreach (List<CPoint> loopItems in inkParam.InkPath)
- {
- List<Point> drawPath = new List<Point>();
- foreach (CPoint item in loopItems)
- {
- drawPath.Add(new Point(item.x - minX, item.y - minY));
- }
- if (drawPath.Count >= 2)
- {
- drawPathList.Add(drawPath);
- }
- }
- double offsetX = (maxX - minX) / 2;
- double offsetY = (maxY - minY) / 2;
- Point centerPoint = CurrentMousePos;
- if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
- {
- double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
- xPos = Math.Min(xPos, PageBound.Right);
- double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
- yPos = Math.Min(yPos, PageBound.Bottom);
- if (FillParam.KeepRate)
- {
- if (GetRateMoveSize(prevRect, out Point ratePoint) == false)
- {
- return;
- }
- xPos = ratePoint.X;
- yPos = ratePoint.Y;
- }
- Rect drawRect = new Rect(MouseDownPos, new Point(xPos, yPos));
- if (drawRect.Width == 0 || drawRect.Height == 0)
- {
- return;
- }
- double rateX = drawRect.Width / prevRect.Width / zoom;
- double rateY = drawRect.Height / prevRect.Height / zoom;
- if (FillParam.KeepRate)
- {
- rateY = rateX * prevRect.Height / prevRect.Width;
- }
- offsetX = (offsetX * rateX);
- offsetY = (offsetY * rateY);
- List<List<Point>> rateList = new List<List<Point>>();
- foreach (List<Point> loopItems in drawPathList)
- {
- List<Point> ratePath = new List<Point>();
- foreach (Point item in loopItems)
- {
- ratePath.Add(new Point((item.X - minX) * rateX, (item.Y - minY) * rateY));
- }
- rateList.Add(ratePath);
- }
- drawPathList = rateList;
- centerPoint.X = (MouseDownPos.X + xPos) / 2;
- centerPoint.Y = (MouseDownPos.Y + yPos) / 2;
- }
- GeometryGroup drawGroup = new GeometryGroup();
- foreach (List<Point> loopItems in drawPathList)
- {
- PathGeometry pathGeometry = new PathGeometry();
- PathFigure path = new PathFigure();
- path.IsClosed = false;
- path.StartPoint = new Point((loopItems[0].X - offsetX) * zoom + centerPoint.X, (loopItems[0].Y - offsetY) * zoom + centerPoint.Y);
- for (int i = 1; i < loopItems.Count; i++)
- {
- LineSegment segment = new LineSegment();
- segment.IsSmoothJoin = true;
- segment.Point = new Point((loopItems[i].X - offsetX) * zoom + centerPoint.X, (loopItems[i].Y - offsetY) * zoom + centerPoint.Y);
- path.Segments.Add(segment);
- }
- pathGeometry.Figures.Add(path);
- drawGroup.Children.Add(pathGeometry);
- }
- DrawBound = drawGroup.Bounds;
- drawDC.DrawGeometry(null, borderPen, drawGroup);
- return;
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void DrawCross(DrawingContext drawDC)
- {
- if (FillParam == null || FillParam.Param == null)
- {
- return;
- }
- InkParam inkParam = FillParam.Param as InkParam;
- if (inkParam == null)
- {
- return;
- }
- try
- {
- double zoom = PDFViewer.GetZoom();
- SolidColorBrush inkBrush = Brushes.Black;
- if (inkParam.InkColor != null && inkParam.InkColor.Length == 3)
- {
- inkBrush = new SolidColorBrush(Color.FromRgb(inkParam.InkColor[0], inkParam.InkColor[1], inkParam.InkColor[2]));
- }
- Pen borderPen = new Pen(inkBrush, inkParam.Thickness * zoom);
- borderPen.StartLineCap = PenLineCap.Round;
- borderPen.EndLineCap = PenLineCap.Round;
- if (inkParam.InkPath == null || inkParam.InkPath.Count == 0)
- {
- int width = Math.Max(0, FillParam.Width);
- int height = Math.Max(0, FillParam.Height);
- if (width == 0 || height == 0)
- {
- width = 30;
- height = 30;
- }
- List<List<CPoint>> defaultList = new List<List<CPoint>>();
- List<CPoint> pointList = new List<CPoint>();
- defaultList.Add(pointList);
- pointList.Add(new CPoint(0, 0));
- pointList.Add(new CPoint(width / 2, height / 2));
- pointList.Add(new CPoint(width, height));
- pointList = new List<CPoint>();
- defaultList.Add(pointList);
- pointList.Add(new CPoint(0, height));
- pointList.Add(new CPoint(width / 2, height / 2));
- pointList.Add(new CPoint(width, 0));
- inkParam.InkPath = defaultList;
- }
- if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
- {
- double minX = 0;
- double minY = 0;
- double maxX = 0;
- double maxY = 0;
- foreach (List<CPoint> loopItems in inkParam.InkPath)
- {
- GetPointsBound(loopItems, out Rect checkBound);
- minX = Math.Min(minX, checkBound.X);
- minY = Math.Min(minY, checkBound.Y);
- maxX = Math.Max(maxX, checkBound.Right);
- maxY = Math.Max(maxY, checkBound.Bottom);
- }
- Rect prevRect = new Rect(minX, minY, maxX - minX, maxY - minY);
- if (prevRect.Width == 0 || prevRect.Height == 0)
- {
- return;
- }
- List<List<Point>> drawPathList = new List<List<Point>>();
- foreach (List<CPoint> loopItems in inkParam.InkPath)
- {
- List<Point> drawPath = new List<Point>();
- foreach (CPoint item in loopItems)
- {
- drawPath.Add(new Point(item.x - minX, item.y - minY));
- }
- if (drawPath.Count >= 2)
- {
- drawPathList.Add(drawPath);
- }
- }
- double offsetX = (maxX - minX) / 2;
- double offsetY = (maxY - minY) / 2;
- Point centerPoint = CurrentMousePos;
- if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
- {
- double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
- xPos = Math.Min(xPos, PageBound.Right);
- double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
- yPos = Math.Min(yPos, PageBound.Bottom);
- if (FillParam.KeepRate)
- {
- if(GetRateMoveSize(prevRect,out Point ratePoint)==false)
- {
- return;
- }
- xPos = ratePoint.X;
- yPos = ratePoint.Y;
- }
- Rect drawRect = new Rect(MouseDownPos, new Point(xPos, yPos));
- if (drawRect.Width == 0 || drawRect.Height == 0)
- {
- return;
- }
- double rateX = drawRect.Width / prevRect.Width / zoom;
- double rateY = drawRect.Height / prevRect.Height / zoom;
- if (FillParam.KeepRate)
- {
- rateY = rateX * prevRect.Height / prevRect.Width;
- }
- offsetX = (offsetX * rateX);
- offsetY = (offsetY * rateY);
- List<List<Point>> rateList = new List<List<Point>>();
- foreach (List<Point> loopItems in drawPathList)
- {
- List<Point> ratePath = new List<Point>();
- foreach (Point item in loopItems)
- {
- ratePath.Add(new Point((item.X - minX) * rateX, (item.Y - minY) * rateY));
- }
- rateList.Add(ratePath);
- }
- drawPathList = rateList;
- centerPoint.X = (MouseDownPos.X + xPos) / 2;
- centerPoint.Y = (MouseDownPos.Y + yPos) / 2;
- }
- GeometryGroup drawGroup = new GeometryGroup();
- foreach (List<Point> loopItems in drawPathList)
- {
- PathGeometry pathGeometry = new PathGeometry();
- PathFigure path = new PathFigure();
- path.IsClosed = false;
- path.StartPoint = new Point((loopItems[0].X - offsetX) * zoom + centerPoint.X, (loopItems[0].Y - offsetY) * zoom + centerPoint.Y);
- for (int i = 1; i < loopItems.Count; i++)
- {
- LineSegment segment = new LineSegment();
- segment.IsSmoothJoin=true;
- segment.Point = new Point((loopItems[i].X - offsetX) * zoom + centerPoint.X, (loopItems[i].Y - offsetY) * zoom + centerPoint.Y);
- path.Segments.Add(segment);
- }
- pathGeometry.Figures.Add(path);
- drawGroup.Children.Add(pathGeometry);
- }
- DrawBound = drawGroup.Bounds;
- drawDC.DrawGeometry(null, borderPen, drawGroup);
- return;
- }
- }
- catch (Exception ex)
- {
- }
- }
- private bool GetRateMoveSize(Rect oldRect ,out Point ratePoint)
- {
- ratePoint=new Point(0,0);
- double rateY = oldRect.Height / oldRect.Width;
- double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
- xPos = Math.Min(xPos, PageBound.Right);
- double moveX = xPos - MouseDownPos.X;
- double moveY = CurrentMousePos.Y - MouseDownPos.Y;
- if (moveX == 0)
- {
- return false;
- }
- if (moveY == 0)
- {
- moveY = 1;
- }
- double rateMoveY = Math.Abs(moveX * rateY);
- double yPos = MouseDownPos.Y + rateMoveY * Math.Abs(moveY) / moveY;
- if (yPos >= PageBound.Top && yPos <= PageBound.Bottom)
- {
- ratePoint= new Point(xPos, yPos);
- return true;
- }
- if(moveY>=0)
- {
- rateMoveY=PageBound.Bottom-MouseDownPos.Y;
- yPos=PageBound.Bottom;
- }
- else
- {
- rateMoveY=MouseDownPos.Y-PageBound.Top;
- yPos=PageBound.Top;
- }
- double rateMoveX = rateMoveY / rateY;
- xPos = MouseDownPos.X + rateMoveX * Math.Abs(moveX) / (moveX);
- ratePoint = new Point(xPos, yPos);
- return true;
- }
- internal FillItem SaveFillItem()
- {
- if (FillParam == null || PageIndex < 0 || PDFViewer == null)
- {
- return null;
- }
- try
- {
- switch (FillParam.Type)
- {
- case FillType.Square:
- return SaveRect();
- case FillType.Circle:
- return SaveCircle();
- case FillType.Line:
- return SaveLine();
- case FillType.Check:
- return SaveCheck();
- case FillType.Cross:
- return SaveCross();
- default:
- return null;
- }
- }
- catch (Exception ex)
- {
- }
- return null;
- }
- private FillItem SaveRect()
- {
- double zoom = PDFViewer.GetZoom();
- CPDFDocument pdfDoc = PDFViewer.GetDocument();
- if (zoom <= 0 || pdfDoc == null)
- {
- return null;
- }
- CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
- SquareParam param = FillParam.Param as SquareParam;
- if (pdfPage == null || param == null)
- {
- return null;
- }
- Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
- Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
- Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
- CPDFSquareAnnotation squareAnnot = (CPDFSquareAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE);
- squareAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
- squareAnnot.SetLineWidth((float)param.LineWidth);
- squareAnnot.SetLineColor(param.LineColor);
- squareAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
- if(string.IsNullOrEmpty(param.Author)==false)
- {
- squareAnnot.SetAuthor(param.Author);
- }
- squareAnnot.UpdateAp();
- FillItem cloneItem = FillParam.Clone();
- cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, squareAnnot);
- cloneItem.HistoryItem = new SquareAnnotHistory();
- cloneItem.HistoryItem.Action = HistoryAction.Add;
- cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, squareAnnot);
- cloneItem.HistoryItem.PDFDoc = pdfDoc;
- PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
- return cloneItem;
- }
- private FillItem SaveCircle()
- {
- double zoom = PDFViewer.GetZoom();
- CPDFDocument pdfDoc = PDFViewer.GetDocument();
- if (zoom <= 0 || pdfDoc == null)
- {
- return null;
- }
- CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
- CircleParam param = FillParam.Param as CircleParam;
- if (pdfPage == null || param == null)
- {
- return null;
- }
- Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
- Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
- Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
- CPDFCircleAnnotation circleAnnot = (CPDFCircleAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE);
- circleAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
- if (param.HasBgColor)
- {
- circleAnnot.SetBgColor(param.BgColor);
- circleAnnot.SetLineColor(param.BgColor);
- }
- circleAnnot.SetLineWidth(0);
- circleAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
- if (string.IsNullOrEmpty(param.Author) == false)
- {
- circleAnnot.SetAuthor(param.Author);
- }
- circleAnnot.UpdateAp();
- FillItem cloneItem = FillParam.Clone();
- cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, circleAnnot);
- cloneItem.HistoryItem = new CircleAnnotHistory();
- cloneItem.HistoryItem.Action = HistoryAction.Add;
- cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, circleAnnot);
- cloneItem.HistoryItem.PDFDoc = pdfDoc;
- PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
- return cloneItem;
- }
- private FillItem SaveLine()
- {
- double zoom = PDFViewer.GetZoom();
- CPDFDocument pdfDoc = PDFViewer.GetDocument();
- if (zoom <= 0 || pdfDoc == null)
- {
- return null;
- }
- CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
- LineParam param = FillParam.Param as LineParam;
- if (pdfPage == null || param == null)
- {
- return null;
- }
- Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
- Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
- Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
- CPDFLineAnnotation lineAnnot = (CPDFLineAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE);
- lineAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
- lineAnnot.SetLinePoints(new CPoint((float)pdfRect.Left,(float)(pdfRect.Top+pdfRect.Height/2)),new CPoint((float)pdfRect.Right,(float)(pdfRect.Top+pdfRect.Height/2)));
- lineAnnot.SetLineWidth((float)param.LineWidth);
- lineAnnot.SetLineColor(param.LineColor);
- lineAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
- if (string.IsNullOrEmpty(param.Author) == false)
- {
- lineAnnot.SetAuthor(param.Author);
- }
- lineAnnot.UpdateAp();
- FillItem cloneItem = FillParam.Clone();
- cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, lineAnnot);
- cloneItem.HistoryItem = new LineAnnotHistory();
- cloneItem.HistoryItem.Action = HistoryAction.Add;
- cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, lineAnnot);
- cloneItem.HistoryItem.PDFDoc = pdfDoc;
- PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
- return cloneItem;
- }
- private FillItem SaveCheck()
- {
- double zoom = PDFViewer.GetZoom();
- CPDFDocument pdfDoc = PDFViewer.GetDocument();
- if (zoom <= 0 || pdfDoc == null)
- {
- return null;
- }
- CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
- InkParam param = FillParam.Param as InkParam;
- if (pdfPage == null || param == null)
- {
- return null;
- }
- if(param==null || param.InkPath==null || param.InkPath.Count==0)
- {
- return null;
- }
- List<List<CPoint>> rawPointList = new List<List<CPoint>>();
- Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
- Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
- Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
- foreach (List<CPoint> segmentList in param.InkPath)
- {
- List<CPoint> pointList = new List<CPoint>();
- foreach (CPoint point in segmentList)
- {
- pointList.Add(new CPoint(point.x/96F*72F+(float)pdfRect.Left,point.y/96F*72F+(float)pdfRect.Top));
- }
- if (pointList.Count > 0)
- {
- rawPointList.Add(pointList);
- }
- }
- CPDFInkAnnotation inkAnnot = (CPDFInkAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
- inkAnnot.SetInkPath(rawPointList);
- inkAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
- inkAnnot.SetInkColor(param.InkColor);
- inkAnnot.SetThickness((float)param.Thickness);
- inkAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
- if (string.IsNullOrEmpty(param.Author) == false)
- {
- inkAnnot.SetAuthor(param.Author);
- }
- inkAnnot.UpdateAp();
- FillItem cloneItem = FillParam.Clone();
- cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
- cloneItem.HistoryItem = new InkAnnotHistory();
- cloneItem.HistoryItem.Action = HistoryAction.Add;
- cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
- cloneItem.HistoryItem.PDFDoc = pdfDoc;
- PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
- return cloneItem;
- }
- private FillItem SaveCross()
- {
- double zoom = PDFViewer.GetZoom();
- CPDFDocument pdfDoc = PDFViewer.GetDocument();
- if (zoom <= 0 || pdfDoc == null)
- {
- return null;
- }
- CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
- InkParam param = FillParam.Param as InkParam;
- if (pdfPage == null || param == null)
- {
- return null;
- }
- if (param == null || param.InkPath == null || param.InkPath.Count == 0)
- {
- return null;
- }
- List<List<CPoint>> rawPointList = new List<List<CPoint>>();
- Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
- Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
- Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
- foreach (List<CPoint> segmentList in param.InkPath)
- {
- List<CPoint> pointList = new List<CPoint>();
- foreach (CPoint point in segmentList)
- {
- pointList.Add(new CPoint(point.x / 96F * 72F + (float)pdfRect.Left, point.y / 96F * 72F + (float)pdfRect.Top));
- }
- if (pointList.Count > 0)
- {
- rawPointList.Add(pointList);
- }
- }
- CPDFInkAnnotation inkAnnot = (CPDFInkAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
- inkAnnot.SetInkPath(rawPointList);
- inkAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
- inkAnnot.SetInkColor(param.InkColor);
- inkAnnot.SetThickness((float)param.Thickness);
- inkAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
- if (string.IsNullOrEmpty(param.Author) == false)
- {
- inkAnnot.SetAuthor(param.Author);
- }
- inkAnnot.UpdateAp();
- FillItem cloneItem = FillParam.Clone();
- cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
- cloneItem.HistoryItem = new InkAnnotHistory();
- cloneItem.HistoryItem.Action = HistoryAction.Add;
- cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
- cloneItem.HistoryItem.PDFDoc = pdfDoc;
- PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
- return cloneItem;
- }
- }
- }
|