1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915 |
- using ComPDFKit.Import;
- using ComPDFKit.Measure;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool.Help;
- using ComPDFKit.Tool.SettingParam;
- 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;
- 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
- {
- internal class CreateAnnotTool : CustomizeLayer
- {
- public event EventHandler<bool> UpdateAnnotHandler;
- public event EventHandler<AnnotParam> CreateFreetextCanceled;
- public event EventHandler<MeasureEventArgs> MeasureChanged;
- public static DependencyProperty PopupTextAttachDataProperty = DependencyProperty.Register("PopupTextAttachData", typeof(CPDFAnnotation), typeof(CPDFViewerTool));
- #region Attributes
- /// <summary>
- /// Indicates whether proportional scaling is required
- /// </summary>
- protected bool isProportionalScaling { get; set; } = false;
- /// <summary>
- /// Mouse start point
- /// </summary>
- protected Point mouseStartPoint { get; set; }
- /// <summary>
- /// Mouse end point
- /// </summary>
- protected Point mouseEndPoint { get; set; }
- /// <summary>
- /// Crop point
- /// </summary>
- protected Point cropPoint { get; set; }
- /// <summary>
- ///Is drawing annotation
- /// </summary>
- protected bool isDrawAnnot { get; set; }
- /// <summary>
- /// Current zoom factor
- /// </summary>
- private double zoomFactor { get; set; } = 1;
- /// <summary>
- /// Draw rectangle
- /// </summary>
- protected Rect drawRect { get; set; }
- /// <summary>
- /// The rectangle representing the maximum drawing area
- /// </summary>
- protected Rect maxRect { get; set; }
- /// <summary>
- /// The rectangle representing the original page range (calculated offset in continuous mode)
- /// </summary>
- protected Rect pageBound { get; set; }
- /// <summary>
- /// The rectangle at standard DPI (without subtracting half of the pen thickness)
- /// </summary>
- protected Rect DPIRect { get; set; }
- /// <summary>
- /// The offset value during movement
- /// </summary>
- protected Point moveOffset { get; set; } = new Point(0, 0);
- protected DrawingContext drawDC { get; set; }
- protected CPDFAnnotation cPDFAnnotation
- {
- get;
- set;
- }
- protected CPDFViewer cPDFViewer { get; set; }
- protected List<Point> inkDrawPoints = new List<Point>();
- /// <summary>
- /// The collection of points measured for annotation drawing
- /// </summary>
- protected PointCollection drawPoints { get; set; } = new PointCollection();
- protected double textPadding { get; set; } = 10;
- protected Border lastTextBorder;
- protected TextBox lastTextui;
- protected Point freeTextPoint { get; set; }
- protected MeasureSetting measureSetting = new MeasureSetting();
- protected DefaultDrawParam defaultDrawParam = new DefaultDrawParam();
- protected DefaultSettingParam defaultSettingParam = new DefaultSettingParam();
- #endregion
- public CreateAnnotTool(MeasureSetting setting, DefaultDrawParam drawParam, DefaultSettingParam settingParam)
- {
- measureSetting = setting;
- defaultDrawParam = drawParam;
- defaultSettingParam = settingParam;
- }
- public Point GetStartPoint()
- {
- return DpiHelper.StandardPointToPDFPoint(new Point((mouseStartPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseStartPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
- }
- public Point GetEndPoint()
- {
- if (moveOffset == new Point())
- {
- return new Point(-1, -1);
- }
- else
- {
- return DpiHelper.StandardPointToPDFPoint(new Point((mouseEndPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseEndPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
- }
- }
- public double GetMoveLength()
- {
- if (mouseEndPoint == new Point())
- {
- return 0;
- }
- Point checkPoint = mouseEndPoint;
- checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
- checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
- checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
- checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
- Vector moveOffset = checkPoint - mouseStartPoint;
- return moveOffset.Length;
- }
- public List<Point> GetInkDrawPoints()
- {
- List<Point> points = new List<Point>
- {
- new Point((mouseStartPoint.X - pageBound.Left+(cropPoint.X*zoomFactor)) / zoomFactor,
- (mouseStartPoint.Y - pageBound.Top + (cropPoint.Y*zoomFactor)) / zoomFactor)
- };
- foreach (Point item in inkDrawPoints)
- {
- points.Add(new Point((item.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
- (item.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor));
- }
- return points;
- }
- public List<Point> GetMeasureDrawPoints()
- {
- List<Point> points = new List<Point>();
- foreach (Point item in drawPoints)
- {
- points.Add(new Point((item.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
- (item.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor));
- }
- if (defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- if (points.Count == 2)
- {
- Rect rect = new Rect(points[0], points[1]);
- points.Clear();
- points.Add(rect.TopLeft);
- points.Add(rect.BottomLeft);
- points.Add(rect.BottomRight);
- points.Add(rect.TopRight);
- }
- else if (points.Count == 1)
- {
- Rect checkRect = pageBound;
- // Current drawing during the movement process.
- Point checkPoint = mouseEndPoint;
- checkPoint.X = Math.Max(checkRect.Left, checkPoint.X);
- checkPoint.X = Math.Min(checkRect.Right, checkPoint.X);
- checkPoint.Y = Math.Max(checkRect.Top, checkPoint.Y);
- checkPoint.Y = Math.Min(checkRect.Bottom, checkPoint.Y);
- List<Point> drawPointsList = new List<Point>
- {
- new Point((drawPoints[0].X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
- (drawPoints[0].Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor),
- new Point((checkPoint.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
- (checkPoint.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor)
- };
- Rect rect = new Rect(drawPointsList[0], drawPointsList[1]);
- points.Clear();
- points.Add(rect.TopLeft);
- points.Add(rect.BottomLeft);
- points.Add(rect.BottomRight);
- points.Add(rect.TopRight);
- }
- }
- return points;
- }
- public void SetIsProportionalScaling(bool isOpen)
- {
- isProportionalScaling = isOpen;
- }
- #region Draw
- public CPDFAnnotation StartDraw(Point downPoint, Point cropPoint, CPDFPage cPDFPage, Rect maxRect, Rect pageBound, C_ANNOTATION_TYPE annotType, CPDFViewer viewer, double zoom)
- {
- RemoveTextBox();
- mouseStartPoint = downPoint;
- mouseEndPoint = downPoint;
- isDrawAnnot = true;
- this.maxRect = maxRect;
- zoomFactor = zoom;
- moveOffset = new Point();
- int newIndex = cPDFPage.GetAnnotCount();
- cPDFAnnotation = cPDFPage.CreateAnnot(annotType);
- if (cPDFAnnotation != null)
- {
- cPDFAnnotation.SetCreationDate(PDFHelp.GetCurrentPdfTime());
- cPDFAnnotation.SetModifyDate(PDFHelp.GetCurrentPdfTime());
- List<CPDFAnnotation> annotList = cPDFPage.GetAnnotations();
- cPDFAnnotation = annotList[newIndex];
- cPDFViewer = viewer;
- drawPoints.Add(downPoint);
- this.cropPoint = cropPoint;
- this.pageBound = pageBound;
- DPIRect = new Rect();
- if (annotType != C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON)
- {
- defaultSettingParam.IsCreateSquarePolygonMeasure = false;
- }
- }
- return cPDFAnnotation;
- }
- public void MoveDraw(Point downPoint, double zoom)
- {
- if (isDrawAnnot)
- {
- moveOffset = new Point(
- mouseEndPoint.X - downPoint.X,
- mouseEndPoint.Y - downPoint.Y
- );
- mouseEndPoint = downPoint;
- zoomFactor = zoom;
- Draw();
- }
- }
- public Rect EndDraw()
- {
- if (isDrawAnnot)
- {
- if (cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
- {
- if (DPIRect.Equals(new Rect()))
- {
- DPIRect = drawRect = new Rect(mouseStartPoint.X, mouseStartPoint.Y, 32 * zoomFactor, 32 * zoomFactor);
- }
- else
- {
- DPIRect = drawRect = new Rect(mouseEndPoint.X, mouseEndPoint.Y, 32 * zoomFactor, 32 * zoomFactor);
- }
- }
- 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;
- freeTextPoint = new Point((mouseStartPoint.X - pageBound.X) / zoomFactor, (mouseStartPoint.Y - pageBound.Y) / zoomFactor);
- mouseStartPoint = new Point();
- mouseEndPoint = new Point();
- moveOffset = new Point();
- pageBound = new Rect();
- DPIRect = new Rect();
- cPDFAnnotation = null;
- inkDrawPoints.Clear();
- drawPoints.Clear();
- return DpiHelper.StandardRectToPDFRect(StandardRect);
- }
- return new Rect();
- }
- public override void Draw()
- {
- Dispatcher.Invoke(() =>
- {
- if (cPDFAnnotation == null)
- {
- return;
- }
- drawDC = Open();
- switch (cPDFAnnotation.Type)
- {
- case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
- DPIRect = drawRect = new Rect(mouseStartPoint.X, mouseStartPoint.Y, 32, 32);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
- DrawText();
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
- if ((cPDFAnnotation as CPDFLineAnnotation).IsMeasured())
- {
- DrawLineMeasure(drawDC);
- }
- else
- {
- DrawLine(drawDC);
- }
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
- DrawSquare(drawDC);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
- DrawCircle(drawDC);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
- DrawInk(drawDC);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
- DrawLink(drawDC);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
- DrawPolygonMeasure(drawDC);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
- DrawPolyLineMeasure(drawDC);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
- DrawRedact(drawDC);
- break;
- default:
- break;
- }
- Present();
- });
- }
- public virtual void ClearDraw()
- {
- RemoveTextBox();
- Open();
- Present();
- }
- private void DrawCircle(DrawingContext drawingContext)
- {
- CPDFCircleAnnotation circleAnnot = (cPDFAnnotation as CPDFCircleAnnotation);
- Pen DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(circleAnnot.LineColor[0], circleAnnot.LineColor[1], circleAnnot.LineColor[2])), (int)Math.Ceiling(circleAnnot.LineWidth / 72 * 96 * zoomFactor));
- SolidColorBrush FillBrush = new SolidColorBrush(Colors.Transparent);
- if (circleAnnot.HasBgColor)
- {
- FillBrush = new SolidColorBrush(Color.FromRgb(circleAnnot.BgColor[0], circleAnnot.BgColor[1], circleAnnot.BgColor[2]));
- }
- DrawPen.Brush.Opacity = circleAnnot.Transparency / 255D;
- FillBrush.Opacity = circleAnnot.Transparency / 255D;
- if (circleAnnot.Dash != null && circleAnnot.Dash.Length > 0 && circleAnnot.LineWidth > 0)
- {
- DashStyle dash = new DashStyle();
- foreach (var offset in circleAnnot.Dash)
- {
- dash.Dashes.Add(offset / circleAnnot.LineWidth);
- }
- DrawPen.DashStyle = dash;
- DrawPen.DashCap = PenLineCap.Flat;
- }
- if (isProportionalScaling)
- {
- Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
- if (mouseOffset.X < 0)
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- }
- else
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- }
- }
- Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
- double mLeft = rect.Left;
- double mRight = rect.Right;
- double mUp = rect.Top;
- double mDown = rect.Bottom;
- if (rect.Left < maxRect.Left)
- {
- mLeft = maxRect.Left;
- }
- if (rect.Right > maxRect.Right)
- {
- mRight = maxRect.Right;
- }
- if (rect.Top < maxRect.Top)
- {
- mUp = maxRect.Top;
- }
- if (rect.Bottom > maxRect.Bottom)
- {
- mDown = maxRect.Bottom;
- }
- DPIRect = drawRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
- double centerX = (drawRect.Left + drawRect.Width / 2);
- double centerY = (drawRect.Top + drawRect.Height / 2);
- double radiusX = drawRect.Width / 2 - DrawPen.Thickness;
- double radiusY = drawRect.Height / 2 - DrawPen.Thickness;
- if (radiusX <= 0 || radiusY <= 0)
- {
- drawingContext.DrawEllipse(DrawPen.Brush, null, new Point(centerX, centerY), (drawRect.Width / 2), (drawRect.Height / 2));
- }
- else
- {
- drawingContext?.DrawEllipse(null, DrawPen, new Point(centerX, centerY), radiusX, radiusY);
- if ((int)(drawRect.Width / 2 - DrawPen.Thickness) > 0 && (int)(drawRect.Height / 2 - DrawPen.Thickness) > 0)
- {
- drawingContext?.DrawEllipse(FillBrush, null, new Point(centerX, centerY), (drawRect.Width / 2 - DrawPen.Thickness), (drawRect.Height / 2 - DrawPen.Thickness));
- }
- }
- }
- private void DrawSquare(DrawingContext drawingContext)
- {
- CPDFSquareAnnotation squareAnnot = (cPDFAnnotation as CPDFSquareAnnotation);
- Pen DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(squareAnnot.LineColor[0], squareAnnot.LineColor[1], squareAnnot.LineColor[2])), (int)Math.Ceiling(squareAnnot.LineWidth / 72 * 96 * zoomFactor));
- SolidColorBrush FillBrush = new SolidColorBrush(Colors.Transparent);
- if (squareAnnot.HasBgColor)
- {
- FillBrush = new SolidColorBrush(Color.FromRgb(squareAnnot.BgColor[0], squareAnnot.BgColor[1], squareAnnot.BgColor[2]));
- }
- DrawPen.Brush.Opacity = squareAnnot.Transparency / 255D;
- FillBrush.Opacity = squareAnnot.Transparency / 255D;
- if (squareAnnot.Dash != null && squareAnnot.Dash.Length > 0 && squareAnnot.LineWidth > 0)
- {
- DashStyle dash = new DashStyle();
- foreach (var offset in squareAnnot.Dash)
- {
- dash.Dashes.Add(offset / squareAnnot.LineWidth);
- }
- DrawPen.DashStyle = dash;
- DrawPen.DashCap = PenLineCap.Flat;
- }
- if (isProportionalScaling)
- {
- Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
- if (mouseOffset.X < 0)
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- }
- else
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- }
- }
- Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
- double mLeft = rect.Left;
- double mRight = rect.Right;
- double mUp = rect.Top;
- double mDown = rect.Bottom;
- if (rect.Left < maxRect.Left)
- {
- mLeft = maxRect.Left;
- }
- if (rect.Right > maxRect.Right)
- {
- mRight = maxRect.Right;
- }
- if (rect.Top < maxRect.Top)
- {
- mUp = maxRect.Top;
- }
- if (rect.Bottom > maxRect.Bottom)
- {
- mDown = maxRect.Bottom;
- }
- DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
- int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
- double drawWidth = DPIRect.Width - halfPenWidth * 2;
- double drawHeight = DPIRect.Height - halfPenWidth * 2;
- if (drawWidth > 0 && drawHeight > 0)
- {
- drawRect = new Rect(
- (int)DPIRect.Left + halfPenWidth,
- (int)DPIRect.Top + halfPenWidth,
- (int)DPIRect.Width - halfPenWidth * 2,
- (int)DPIRect.Height - halfPenWidth * 2);
- drawingContext?.DrawRectangle(null, DrawPen, drawRect);
- halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
- if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
- {
- Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
- drawingContext?.DrawRectangle(FillBrush, null, innerRect);
- }
- }
- }
- private void DrawLine(DrawingContext drawingContext)
- {
- CPDFLineAnnotation annotLine = (cPDFAnnotation as CPDFLineAnnotation);
- Pen DrawPen = new Pen(new SolidColorBrush(
- Color.FromRgb(
- annotLine.LineColor[0],
- annotLine.LineColor[1],
- annotLine.LineColor[2])),
- (int)Math.Ceiling(annotLine.LineWidth == 0 ? 0.5 : annotLine.LineWidth * zoomFactor));
- DrawPen.Brush.Opacity = annotLine.Transparency / 255D;
- if (annotLine.Dash != null && annotLine.Dash.Length > 0 && annotLine.LineWidth > 0)
- {
- DashStyle dash = new DashStyle();
- foreach (var offset in annotLine.Dash)
- {
- dash.Dashes.Add(offset / annotLine.LineWidth);
- }
- DrawPen.DashStyle = dash;
- DrawPen.DashCap = PenLineCap.Flat;
- }
- ArrowHelper drawLine = new ArrowHelper();
- if (isProportionalScaling)
- {
- mouseEndPoint = CalcAnglePoint(mouseEndPoint, mouseStartPoint, pageBound);
- DPIRect = new Rect(mouseStartPoint, mouseEndPoint);
- drawLine.LineStart = mouseStartPoint;
- drawLine.LineEnd = mouseEndPoint;
- drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor);
- drawLine.StartSharp = annotLine.HeadLineType;
- drawLine.EndSharp = annotLine.TailLineType;
- drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
- }
- else
- {
- Point checkPoint = mouseEndPoint;
- if (mouseEndPoint.X < maxRect.Left)
- {
- checkPoint.X = maxRect.Left;
- }
- if (mouseEndPoint.X > maxRect.Right)
- {
- checkPoint.X = maxRect.Right;
- }
- if (mouseEndPoint.Y < maxRect.Top)
- {
- checkPoint.Y = maxRect.Top;
- }
- if (mouseEndPoint.Y > maxRect.Bottom)
- {
- checkPoint.Y = maxRect.Bottom;
- }
- mouseEndPoint = checkPoint;
- DPIRect = new Rect(mouseStartPoint, mouseEndPoint);
- drawLine.LineStart = mouseStartPoint;
- drawLine.LineEnd = mouseEndPoint;
- drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor);
- drawLine.StartSharp = annotLine.HeadLineType;
- drawLine.EndSharp = annotLine.TailLineType;
- drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
- }
- }
- private void DrawInk(DrawingContext drawingContext)
- {
- CPDFInkAnnotation annotLine = (cPDFAnnotation as CPDFInkAnnotation);
- if (annotLine == null || annotLine.IsValid() == false)
- {
- return;
- }
- byte transparent = annotLine.GetTransparency();
- Pen DrawPen = new Pen(new SolidColorBrush(Color.FromArgb(transparent, annotLine.InkColor[0], annotLine.InkColor[1], annotLine.InkColor[2])), annotLine.Thickness * zoomFactor);
- DrawPen.StartLineCap = PenLineCap.Round;
- DrawPen.EndLineCap = PenLineCap.Round;
- PathGeometry pathDraw = new PathGeometry();
- Point CurrentPoint = mouseEndPoint;
- Rect MaxRect = pageBound;
- if (CurrentPoint.X > MaxRect.Right)
- {
- CurrentPoint.X = MaxRect.Right;
- }
- if (CurrentPoint.X < MaxRect.Left)
- {
- CurrentPoint.X = MaxRect.Left;
- }
- if (CurrentPoint.Y > MaxRect.Bottom)
- {
- CurrentPoint.Y = MaxRect.Bottom;
- }
- if (CurrentPoint.Y < MaxRect.Top)
- {
- CurrentPoint.Y = MaxRect.Top;
- }
- inkDrawPoints.Add(CurrentPoint);
- pathDraw.Figures = new PathFigureCollection();
- PathFigure pathFigure = new PathFigure();
- pathDraw.Figures.Add(pathFigure);
- pathFigure.StartPoint = mouseStartPoint;
- foreach (Point addPoint in inkDrawPoints)
- {
- LineSegment lineSegment = new LineSegment(addPoint, true);
- lineSegment.IsSmoothJoin = true;
- pathFigure.Segments.Add(lineSegment);
- }
- if(annotLine.Dash!=null && annotLine.Dash.Length>0)
- {
- DashStyle dash = new DashStyle();
- foreach (var offset in annotLine.Dash)
- {
- dash.Dashes.Add(offset);
- }
- DrawPen.DashStyle = dash;
- DrawPen.DashCap = PenLineCap.Flat;
- }
- Rect checkRect = pageBound;
- RectangleGeometry rectGeometry = new RectangleGeometry();
- drawRect = rectGeometry.Rect = checkRect;
- drawingContext?.PushClip(rectGeometry);
- drawingContext?.DrawGeometry(null, DrawPen, pathDraw);
- }
- private void DrawLink(DrawingContext drawingContext)
- {
- Pen DrawPen = defaultDrawParam.LinkPen;
- DrawPen.Thickness *= zoomFactor;
- SolidColorBrush FillBrush = defaultDrawParam.LinkBrush;
- if (isProportionalScaling)
- {
- Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
- if (mouseOffset.X < 0)
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- }
- else
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- }
- }
- Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
- double mLeft = rect.Left;
- double mRight = rect.Right;
- double mUp = rect.Top;
- double mDown = rect.Bottom;
- if (rect.Left < maxRect.Left)
- {
- mLeft = maxRect.Left;
- }
- if (rect.Right > maxRect.Right)
- {
- mRight = maxRect.Right;
- }
- if (rect.Top < maxRect.Top)
- {
- mUp = maxRect.Top;
- }
- if (rect.Bottom > maxRect.Bottom)
- {
- mDown = maxRect.Bottom;
- }
- DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
- int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
- double drawWidth = DPIRect.Width - halfPenWidth * 2;
- double drawHeight = DPIRect.Height - halfPenWidth * 2;
- if (drawWidth > 0 && drawHeight > 0)
- {
- drawRect = new Rect(
- (int)DPIRect.Left + halfPenWidth,
- (int)DPIRect.Top + halfPenWidth,
- (int)DPIRect.Width - halfPenWidth * 2,
- (int)DPIRect.Height - halfPenWidth * 2);
- drawingContext?.DrawRectangle(null, DrawPen, drawRect);
- halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
- if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
- {
- Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
- drawingContext?.DrawRectangle(FillBrush, null, innerRect);
- }
- }
- }
- private void DrawRedact(DrawingContext drawingContext)
- {
- Pen DrawPen = defaultDrawParam.RedactPen;
- if (cPDFAnnotation != null && cPDFAnnotation.IsValid() && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
- {
- CPDFRedactAnnotation redactAnnot = cPDFAnnotation as CPDFRedactAnnotation;
- if (redactAnnot.OutlineColor != null && redactAnnot.OutlineColor.Length == 3)
- {
- DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(redactAnnot.OutlineColor[0], redactAnnot.OutlineColor[1], redactAnnot.OutlineColor[2])), DrawPen.Thickness);
- }
- }
- SolidColorBrush FillBrush = new SolidColorBrush(Color.FromArgb(0x46, 0x46, 0x82, 0xB4));
- if (isProportionalScaling)
- {
- Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
- if (mouseOffset.X < 0)
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- }
- else
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- }
- }
- Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
- double mLeft = rect.Left;
- double mRight = rect.Right;
- double mUp = rect.Top;
- double mDown = rect.Bottom;
- if (rect.Left < maxRect.Left)
- {
- mLeft = maxRect.Left;
- }
- if (rect.Right > maxRect.Right)
- {
- mRight = maxRect.Right;
- }
- if (rect.Top < maxRect.Top)
- {
- mUp = maxRect.Top;
- }
- if (rect.Bottom > maxRect.Bottom)
- {
- mDown = maxRect.Bottom;
- }
- DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
- int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
- double drawWidth = DPIRect.Width - halfPenWidth * 2;
- double drawHeight = DPIRect.Height - halfPenWidth * 2;
- if (drawWidth > 0 && drawHeight > 0)
- {
- drawRect = new Rect(
- (int)DPIRect.Left + halfPenWidth,
- (int)DPIRect.Top + halfPenWidth,
- (int)DPIRect.Width - halfPenWidth * 2,
- (int)DPIRect.Height - halfPenWidth * 2);
- drawingContext?.DrawRectangle(null, DrawPen, drawRect);
- halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
- if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
- {
- Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
- drawingContext?.DrawRectangle(FillBrush, null, innerRect);
- }
- }
- }
- private void DrawPolyLineMeasure(DrawingContext drawingContext)
- {
- CPDFPolylineAnnotation polyLine = (cPDFAnnotation as CPDFPolylineAnnotation);
- byte[] bytes = polyLine.LineColor;
- Color color = ParamConverter.ConverterByteForColor(bytes);
- color.A = polyLine.GetTransparency();
- Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
- SolidColorBrush TextBrush = Brushes.Red;
- if (polyLine.IsMeasured())
- {
- CPDFPerimeterMeasure measureInfo = polyLine.GetPerimeterMeasure();
- if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
- {
- byte[] fontColor = measureInfo.TextAttribute.FontColor;
- TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
- }
- if (polyLine.Dash != null && polyLine.Dash.Length > 0)
- {
- DashStyle dash = new DashStyle();
- foreach (var offset in polyLine.Dash)
- {
- dash.Dashes.Add(offset / polyLine.LineWidth);
- }
- DrawPen.DashStyle = dash;
- DrawPen.DashCap = PenLineCap.Flat;
- }
- }
- if (isProportionalScaling)
- {
- if (drawPoints != null && drawPoints.Count > 0)
- {
- mouseEndPoint = CalcAnglePoint(mouseEndPoint, drawPoints[drawPoints.Count - 1], pageBound);
- }
- }
- Point checkPoint = mouseEndPoint;
- checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
- checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
- checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
- checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
- if (drawPoints.Count > 0)
- {
- PathGeometry drawPath = new PathGeometry();
- PathFigure drawFigure = new PathFigure();
- drawFigure.StartPoint = drawPoints[0];
- PolyLineSegment polySegment = new PolyLineSegment();
- for (int i = 1; i < drawPoints.Count; i++)
- {
- polySegment.Points.Add(drawPoints[i]);
- }
- polySegment.Points.Add(checkPoint);
- if (polySegment.Points.Count > 0)
- {
- drawFigure.Segments.Add(polySegment);
- }
- if (drawFigure.Segments.Count > 0)
- {
- drawPath.Figures.Add(drawFigure);
- }
- double totalInch = 0;
- if (drawPoints.Count > 1)
- {
- for (int i = 0; i < drawPoints.Count - 1; i++)
- {
- totalInch += measureSetting.GetMeasureLength(drawPoints[i], drawPoints[i + 1], zoomFactor);
- }
- }
- double currentInch = measureSetting.GetMeasureLength(drawPoints[drawPoints.Count - 1], checkPoint, zoomFactor);
- totalInch += currentInch;
- drawingContext?.DrawGeometry(null, DrawPen, drawPath);
- Point closePoint = drawPoints[drawPoints.Count - 1];
- Vector movevector = checkPoint - closePoint;
- FormattedText moveText = new FormattedText(
- string.Format("{0} {1}", measureSetting.GetPrecisionData(currentInch), measureSetting.RulerTranslateUnit),
- CultureInfo.GetCultureInfo("en-us"),
- FlowDirection.LeftToRight,
- new Typeface("YaHei"),
- 16,
- TextBrush);
- FormattedText totalText = new FormattedText(
- string.Format("{0} {1}", measureSetting.GetPrecisionData(totalInch), measureSetting.RulerTranslateUnit),
- CultureInfo.GetCultureInfo("en-us"),
- FlowDirection.LeftToRight,
- new Typeface("YaHei"),
- 16,
- TextBrush);
- if (movevector.Length > moveText.Width + textPadding)
- {
- if (checkPoint.X >= closePoint.X)
- {
- Point linePoint = new Point(closePoint.X + movevector.Length, closePoint.Y);
- Point drawPoint = new Point(
- linePoint.X - moveText.Width - textPadding,
- linePoint.Y - moveText.Height);
- Vector anglevector = linePoint - closePoint;
- RotateTransform transform = new RotateTransform();
- transform.CenterX = closePoint.X;
- transform.CenterY = closePoint.Y;
- double angle = Vector.AngleBetween(movevector, anglevector);
- transform.Angle = -angle;
- drawingContext?.PushTransform(transform);
- drawingContext?.DrawText(moveText, drawPoint);
- if (totalInch > currentInch)
- {
- drawingContext?.DrawText(totalText, new Point(
- drawPoint.X + moveText.Width + textPadding * 2,
- drawPoint.Y
- ));
- }
- drawingContext.Pop();
- }
- else
- {
- Point linePoint = new Point(closePoint.X - movevector.Length, closePoint.Y);
- Point drawPoint = new Point(
- linePoint.X + textPadding,
- linePoint.Y - moveText.Height);
- Vector anglevector = linePoint - closePoint;
- RotateTransform transform = new RotateTransform();
- transform.CenterX = closePoint.X;
- transform.CenterY = closePoint.Y;
- double angle = Vector.AngleBetween(movevector, anglevector);
- transform.Angle = -angle;
- drawingContext?.PushTransform(transform);
- drawingContext?.DrawText(moveText, drawPoint);
- if (totalInch > currentInch)
- {
- drawingContext?.DrawText(totalText,
- new Point(
- drawPoint.X - totalText.Width - textPadding * 2,
- drawPoint.Y
- ));
- }
- drawingContext.Pop();
- }
- }
- 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);
- MeasureEventArgs measureEvent = new MeasureEventArgs();
- if (drawPoints.Count < 2)
- {
- measureEvent.Angle = 0;
- }
- else
- {
- Vector standVector = drawPoints[drawPoints.Count - 1] - drawPoints[drawPoints.Count - 2];
- Vector endvector = closePoint - checkPoint;
- measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, standVector));
- }
- measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
- measureEvent.RulerTranslate = measureSetting.RulerTranslate;
- measureEvent.RulerBase = measureSetting.RulerBase;
- measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
- measureEvent.Precision = measureSetting.Precision;
- measureEvent.Type = CPDFMeasureType.CPDF_PERIMETER_MEASURE;
- measureEvent.Distance = totalText.Text;
- MeasureChanged?.Invoke(this, measureEvent);
- }
- }
- private void DrawPolygonMeasure(DrawingContext drawingContext)
- {
- CPDFPolygonAnnotation polyLine = (cPDFAnnotation as CPDFPolygonAnnotation);
- byte[] bytes = polyLine.LineColor;
- Color color = ParamConverter.ConverterByteForColor(bytes);
- color.A = polyLine.GetTransparency();
- Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
- Pen EndDrawPen = new Pen(Brushes.Black, polyLine.GetBorderWidth());
- SolidColorBrush TextBrush = Brushes.Red;
- if (polyLine.IsMeasured())
- {
- CPDFAreaMeasure measureInfo = polyLine.GetAreaMeasure();
- if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
- {
- byte[] fontColor = measureInfo.TextAttribute.FontColor;
- TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
- }
- if (polyLine.Dash != null && polyLine.Dash.Length > 0)
- {
- DashStyle dash = new DashStyle();
- foreach (var offset in polyLine.Dash)
- {
- dash.Dashes.Add(offset / polyLine.LineWidth);
- }
- DrawPen.DashStyle = dash;
- DrawPen.DashCap = PenLineCap.Flat;
- }
- }
- if (isProportionalScaling)
- {
- if (drawPoints != null && drawPoints.Count > 0)
- {
- mouseEndPoint = CalcAnglePoint(mouseEndPoint, drawPoints[drawPoints.Count - 1], pageBound);
- }
- }
- Point checkPoint = mouseEndPoint;
- checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
- checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
- checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
- checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
- PointCollection points = drawPoints.Clone();
- if (defaultSettingParam.IsCreateSquarePolygonMeasure && drawPoints.Count == 1)
- {
- Point star = points[0];
- Rect rect = new Rect(star, checkPoint);
- points.Clear();
- points.Add(rect.TopLeft);
- points.Add(rect.BottomLeft);
- points.Add(rect.BottomRight);
- points.Add(rect.TopRight);
- }
- if (points.Count > 0)
- {
- PathGeometry drawPath = new PathGeometry();
- PathFigure drawFigure = new PathFigure();
- drawFigure.StartPoint = points[0];
- PolyLineSegment polySegment = new PolyLineSegment();
- for (int i = 1; i < points.Count; i++)
- {
- polySegment.Points.Add(points[i]);
- }
- if (defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- polySegment.Points.Add(points[0]);
- }
- else
- {
- //Add the current point during the movement.
- polySegment.Points.Add(checkPoint);
- }
- if (polySegment.Points.Count > 0)
- {
- drawFigure.Segments.Add(polySegment);
- }
- if (drawFigure.Segments.Count > 0)
- {
- drawPath.Figures.Add(drawFigure);
- }
- //Draw the line segment.
- drawingContext?.DrawGeometry(null, DrawPen, drawPath);
- //Draw the example line connected by the start point and the end point.
- if (points.Count > 1)
- {
- if (defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- drawingContext?.DrawLine(DrawPen, points[0], polySegment.Points.Last());
- }
- else
- {
- drawingContext?.DrawLine(EndDrawPen, points[0], polySegment.Points.Last());
- }
- }
- //Calculate the length.
- double totalInch = 0;
- if (points.Count > 1)
- {
- for (int i = 0; i < points.Count - 1; i++)
- {
- totalInch += measureSetting.GetMeasureLength(points[i], points[i + 1], zoomFactor);
- }
- }
- double currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], checkPoint, zoomFactor);
- if (defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], points[0], zoomFactor);
- }
- totalInch += currentInch;
- Point closePoint = points[points.Count - 1];
- Vector movevector = checkPoint - closePoint;
- FormattedText moveText = new FormattedText(
- string.Format("{0} {1}", measureSetting.GetPrecisionData(currentInch), measureSetting.RulerTranslateUnit),
- CultureInfo.GetCultureInfo("en-us"),
- FlowDirection.LeftToRight,
- new Typeface("YaHei"),
- 16,
- TextBrush);
- FormattedText totalText = new FormattedText(
- string.Format("{0} {1}", measureSetting.GetPrecisionData(totalInch), measureSetting.RulerTranslateUnit),
- CultureInfo.GetCultureInfo("en-us"),
- FlowDirection.LeftToRight,
- new Typeface("YaHei"),
- 16,
- TextBrush);
- //Judge the text display form.
- if (movevector.Length > moveText.Width + textPadding || defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- if (checkPoint.X >= closePoint.X)
- {
- Point linePoint = new Point(closePoint.X + movevector.Length, closePoint.Y);
- Point drawPoint = new Point(
- linePoint.X - moveText.Width - textPadding,
- linePoint.Y - moveText.Height);
- Vector anglevector = linePoint - closePoint;
- RotateTransform transform = new RotateTransform();
- transform.CenterX = closePoint.X;
- transform.CenterY = closePoint.Y;
- double angle = Vector.AngleBetween(movevector, anglevector);
- transform.Angle = -angle;
- drawingContext?.PushTransform(transform);
- if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- drawingContext?.DrawText(moveText, drawPoint);
- }
- if (totalInch > currentInch)
- {
- drawingContext?.DrawText(totalText, new Point(
- drawPoint.X + moveText.Width + textPadding * 2,
- drawPoint.Y
- ));
- }
- drawingContext.Pop();
- }
- else
- {
- Point linePoint = new Point(closePoint.X - movevector.Length, closePoint.Y);
- Point drawPoint = new Point(
- linePoint.X + textPadding,
- linePoint.Y - moveText.Height);
- Vector anglevector = linePoint - closePoint;
- RotateTransform transform = new RotateTransform();
- transform.CenterX = closePoint.X;
- transform.CenterY = closePoint.Y;
- double angle = Vector.AngleBetween(movevector, anglevector);
- transform.Angle = -angle;
- drawingContext?.PushTransform(transform);
- if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- drawingContext?.DrawText(moveText, drawPoint);
- }
- if (totalInch > currentInch)
- {
- drawingContext?.DrawText(totalText,
- new Point(
- drawPoint.X - totalText.Width - textPadding * 2,
- drawPoint.Y
- ));
- }
- drawingContext.Pop();
- }
- }
- 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);
- if (defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- double deleft = points.AsEnumerable().Select(x => x.X).Min();
- double deright = points.AsEnumerable().Select(x => x.X).Max();
- double detop = points.AsEnumerable().Select(x => x.Y).Min();
- double debottom = points.AsEnumerable().Select(x => x.Y).Max();
- DPIRect = new Rect(deleft, detop, deright - deleft, debottom - detop);
- }
- MeasureEventArgs measureEvent = new MeasureEventArgs();
- if (points.Count < 2)
- {
- measureEvent.Angle = 0;
- }
- else
- {
- Vector standVector = points[points.Count - 1] - points[points.Count - 2];
- Vector endvector = closePoint - checkPoint;
- measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, standVector));
- if (defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- measureEvent.Angle = 90;
- }
- }
- List<Point> pon = new List<Point>();
- if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
- {
- points.Add(checkPoint);
- }
- foreach (Point drawPoint in points)
- {
- Point savePoint = new Point(
- (drawPoint.X - pageBound.Left) + cropPoint.X,
- (drawPoint.Y - pageBound.Top) + cropPoint.Y);
- pon.Add(DpiHelper.StandardPointToPDFPoint(new Point(
- (float)drawPoint.X / zoomFactor,
- (float)drawPoint.Y / zoomFactor
- )));
- }
- double area = measureSetting.ComputePolygonArea(pon.ToList());
- double ratio = measureSetting.GetMeasureAreaRatio();
- double rate = measureSetting.RulerTranslate / measureSetting.RulerBase;
- double inch = area * ratio * ratio * rate * rate;
- measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
- measureEvent.RulerTranslate = measureSetting.RulerTranslate;
- measureEvent.RulerBase = measureSetting.RulerBase;
- measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
- measureEvent.Precision = measureSetting.Precision;
- measureEvent.Type = CPDFMeasureType.CPDF_AREA_MEASURE;
- measureEvent.Distance = totalText.Text;
- measureEvent.Area = string.Format("{0} sq {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit);
- MeasureChanged?.Invoke(this, measureEvent);
- }
- }
- private void DrawLineMeasure(DrawingContext drawingContext)
- {
- CPDFLineAnnotation polyLine = (cPDFAnnotation as CPDFLineAnnotation);
- byte[] bytes = polyLine.LineColor;
- Color color = ParamConverter.ConverterByteForColor(bytes);
- color.A = polyLine.GetTransparency();
- Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
- SolidColorBrush TextBrush = Brushes.Red;
- if (polyLine.IsMeasured())
- {
- CPDFDistanceMeasure measureInfo = polyLine.GetDistanceMeasure();
- if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
- {
- byte[] fontColor = measureInfo.TextAttribute.FontColor;
- TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
- }
- if (polyLine.Dash != null && polyLine.Dash.Length > 0)
- {
- DashStyle dash = new DashStyle();
- foreach (var offset in polyLine.Dash)
- {
- dash.Dashes.Add(offset / polyLine.LineWidth);
- }
- DrawPen.DashStyle = dash;
- DrawPen.DashCap = PenLineCap.Flat;
- }
- }
- if (isProportionalScaling)
- {
- mouseEndPoint = CalcAnglePoint(mouseEndPoint, mouseStartPoint, pageBound);
- }
- Point checkPoint = mouseEndPoint;
- checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
- checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
- checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
- checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
- double inch = measureSetting.GetMeasureLength(mouseStartPoint, checkPoint, zoomFactor);
- ArrowHelper drawLine = new ArrowHelper();
- drawLine.LineStart = mouseStartPoint;
- drawLine.LineEnd = checkPoint;
- drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor * zoomFactor);
- drawLine.StartSharp = polyLine.HeadLineType;
- drawLine.EndSharp = polyLine.TailLineType;
- drawLine.BuildArrowBody();
- drawingContext?.DrawGeometry(null, DrawPen, drawLine.Body);
- drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
- FormattedText formattedText = new FormattedText(
- string.Format("{0} {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit),
- CultureInfo.GetCultureInfo("en-us"),
- FlowDirection.LeftToRight,
- new Typeface("YaHei"),
- 16,
- TextBrush);
- Vector movevector = checkPoint - mouseStartPoint;
- if (movevector.Length > formattedText.Width + textPadding)
- {
- if (checkPoint.X >= mouseStartPoint.X)
- {
- Point linePoint = new Point(mouseStartPoint.X + movevector.Length, mouseStartPoint.Y);
- Point drawPoint = new Point(
- linePoint.X - formattedText.Width - textPadding,
- linePoint.Y - formattedText.Height);
- Vector anglevector = linePoint - mouseStartPoint;
- RotateTransform transform = new RotateTransform();
- transform.CenterX = mouseStartPoint.X;
- transform.CenterY = mouseStartPoint.Y;
- double angle = Vector.AngleBetween(movevector, anglevector);
- transform.Angle = -angle;
- drawingContext?.PushTransform(transform);
- drawingContext?.DrawText(formattedText, drawPoint);
- drawingContext.Pop();
- }
- else
- {
- Point linePoint = new Point(mouseStartPoint.X - movevector.Length, mouseStartPoint.Y);
- Point drawPoint = new Point(
- linePoint.X + textPadding,
- linePoint.Y - formattedText.Height);
- Vector anglevector = linePoint - mouseStartPoint;
- RotateTransform transform = new RotateTransform();
- transform.CenterX = mouseStartPoint.X;
- transform.CenterY = mouseStartPoint.Y;
- double angle = Vector.AngleBetween(movevector, anglevector);
- transform.Angle = -angle;
- drawingContext?.PushTransform(transform);
- drawingContext?.DrawText(formattedText, drawPoint);
- drawingContext.Pop();
- }
- }
- DPIRect = new Rect(mouseStartPoint, checkPoint);
- if (drawPoints.Count <= 1)
- {
- drawPoints.Add(checkPoint);
- }
- else
- {
- drawPoints[1] = checkPoint;
- }
- Vector standVector = new Vector(1, 0);
- MeasureEventArgs measureEvent = new MeasureEventArgs();
- measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(movevector, standVector));
- measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
- measureEvent.RulerTranslate = measureSetting.RulerTranslate;
- measureEvent.RulerBase = measureSetting.RulerBase;
- measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
- measureEvent.Precision = measureSetting.Precision;
- Vector moveVector = checkPoint - mouseStartPoint;
- measureEvent.MousePos = new Point(
- (int)Math.Abs(moveVector.X / zoomFactor / 96D * 72D),
- (int)Math.Abs(moveVector.Y / zoomFactor / 96D * 72D));
- measureEvent.Type = CPDFMeasureType.CPDF_DISTANCE_MEASURE;
- measureEvent.Distance = formattedText.Text;
- MeasureChanged?.Invoke(this, measureEvent);
- }
- #endregion
- public void MultipleClick(Point downPoint)
- {
- drawPoints.Add(downPoint);
- }
- public Rect GetMaxRect()
- {
- return maxRect;
- }
- public void CreateTextBox()
- {
- try
- {
- if (cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
- {
- CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
- TextBox textui = new TextBox();
- DashedBorder textBorder = new DashedBorder();
- textBorder.Child = textui;
- textui.Width = 200;
- CTextAttribute textAttribute = annotFreeText.FreeTextDa;
- byte transparency = annotFreeText.GetTransparency();
- textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
- Color textColor = Color.FromArgb(
- transparency,
- textAttribute.FontColor[0],
- textAttribute.FontColor[1],
- textAttribute.FontColor[2]);
- Color borderColor = Colors.Transparent;
- Color backgroundColor = Colors.White;
- byte[] colorArray = new byte[3];
- if (annotFreeText.Transparency > 0)
- {
- borderColor = Color.FromRgb(annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
- }
- if (annotFreeText.HasBgColor)
- {
- backgroundColor = Color.FromRgb(annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
- }
- Point MousePoint = new Point((mouseStartPoint.X - pageBound.X), (mouseStartPoint.Y - pageBound.Y));
- textBorder.MaxWidth = (pageBound.Width - MousePoint.X - cropPoint.X);
- textBorder.MaxHeight = (pageBound.Height - MousePoint.Y - cropPoint.Y);
- textui.Foreground = new SolidColorBrush(textColor);
- textui.Background = new SolidColorBrush(backgroundColor);
- textui.MinHeight = 40;
- textui.MinWidth = 200;
- textBorder.Padding = new Thickness(0);
- textBorder.BorderBrush = new SolidColorBrush(borderColor);
- 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;
- string fontName = string.Empty;
- string fontFamily = string.Empty;
- CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
- textui.FontFamily = new FontFamily(fontFamily);
- textui.AcceptsReturn = true;
- textui.TextWrapping = TextWrapping.Wrap;
- textui.TextAlignment = TextAlignment.Left;
- switch (annotFreeText.Alignment)
- {
- case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
- textui.TextAlignment = TextAlignment.Left;
- break;
- case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
- textui.TextAlignment = TextAlignment.Right;
- break;
- case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
- textui.TextAlignment = TextAlignment.Center;
- break;
- default:
- break;
- }
- textBorder.SetValue(Canvas.LeftProperty, mouseStartPoint.X);
- textBorder.SetValue(Canvas.TopProperty, mouseStartPoint.Y);
- lastTextui = textui;
- lastTextBorder = textBorder;
- textui.Loaded += (object sender, RoutedEventArgs e) =>
- {
- textui.Focus();
- textui.CaretIndex = textui.Text.Length;
- textui.SetValue(PopupTextAttachDataProperty, cPDFAnnotation);
- UpdateAnnotHandler?.Invoke(this, false);
- };
- textui.LostFocus += (object sender, RoutedEventArgs e) =>
- {
- CPDFAnnotation currentAnnot = textui.GetValue(PopupTextAttachDataProperty) as CPDFAnnotation;
- AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
- if (currentAnnot != null && currentAnnot.IsValid())
- {
- CPDFFreeTextAnnotation updateFreeText = currentAnnot as CPDFFreeTextAnnotation;
- if (textui.Text != string.Empty || updateFreeText.GetBorderWidth() != 0)
- {
- updateFreeText.SetContent(textui.Text);
- Rect changeRect = new Rect(
- DpiHelper.StandardNumToPDFNum(freeTextPoint.X),
- DpiHelper.StandardNumToPDFNum(freeTextPoint.Y),
- DpiHelper.StandardNumToPDFNum(textBorder.ActualWidth / zoomFactor),
- DpiHelper.StandardNumToPDFNum(textBorder.ActualHeight / zoomFactor));
- updateFreeText.SetRect(new CRect(
- (float)changeRect.Left,
- (float)changeRect.Bottom,
- (float)changeRect.Right,
- (float)changeRect.Top
- ));
- updateFreeText.UpdateAp();
- FreeTextAnnotHistory freeTextAnnotHistory = new FreeTextAnnotHistory();
- annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
- annotParam.AnnotIndex = currentAnnot.Page.GetAnnotCount() - 1;
- freeTextAnnotHistory.CurrentParam = (FreeTextParam)annotParam;
- freeTextAnnotHistory.PDFDoc = cPDFViewer.GetDocument();
- cPDFViewer.UndoManager.AddHistory(freeTextAnnotHistory);
- UpdateAnnotHandler?.Invoke(this, true);
- cPDFViewer.UndoManager?.InvokeHistoryChanged(this, new KeyValuePair<ComPDFKitViewer.Helper.UndoAction, IHistory>(ComPDFKitViewer.Helper.UndoAction.Custom, freeTextAnnotHistory));
- freeTextPoint = new Point(0, 0);
- }
- else
- {
- updateFreeText.RemoveAnnot();
- CreateFreetextCanceled?.Invoke(this, annotParam);
- }
- }
- RemoveTextBox();
- };
- BaseLayer createAnnotTool = this;
- if (createAnnotTool != null)
- {
- createAnnotTool.Children.Add(textBorder);
- createAnnotTool.Arrange();
- }
- textui.LayoutUpdated += (object sender, EventArgs e) =>
- {
- createAnnotTool.Arrange();
- };
- }
- }
- catch
- {
- }
- }
- public void RemoveTextBox()
- {
- if (lastTextBorder == null)
- {
- return;
- }
- BaseLayer removeLayer = this;
- removeLayer.Children.Remove(lastTextBorder);
- }
- private void DrawText()
- {
- if (isProportionalScaling)
- {
- Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
- if (mouseOffset.X < 0)
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- }
- else
- {
- if (mouseOffset.Y > 0)
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
- }
- else
- {
- mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
- }
- }
- }
- Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
- double mLeft = rect.Left;
- double mRight = rect.Right;
- double mUp = rect.Top;
- double mDown = rect.Bottom;
- if (rect.Left < maxRect.Left)
- {
- mLeft = maxRect.Left;
- }
- if (rect.Right > maxRect.Right)
- {
- mRight = maxRect.Right;
- }
- if (rect.Top < maxRect.Top)
- {
- mUp = maxRect.Top;
- }
- if (rect.Bottom > maxRect.Bottom)
- {
- mDown = maxRect.Bottom;
- }
- Rect drawRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
- DPIRect = drawRect;
- try
- {
- if (lastTextui != null && lastTextBorder != null && cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
- {
- CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
- CTextAttribute textAttribute = annotFreeText.FreeTextDa;
- byte transparency = annotFreeText.GetTransparency();
- lastTextui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
- Color textColor = Color.FromArgb(
- transparency,
- textAttribute.FontColor[0],
- textAttribute.FontColor[1],
- textAttribute.FontColor[2]);
- Color borderColor = Colors.Transparent;
- Color backgroundColor = Colors.Transparent;
- byte[] colorArray = new byte[3];
- if (annotFreeText.Transparency > 0)
- {
- borderColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
- }
- if (annotFreeText.HasBgColor)
- {
- backgroundColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
- }
- Border parentUI = lastTextui.Parent as Border;
- if (parentUI != null)
- {
- parentUI.SetValue(Canvas.LeftProperty, drawRect.Left);
- parentUI.SetValue(Canvas.TopProperty, drawRect.Top);
- // The width is incorrect
- if (mouseEndPoint.X >= mouseStartPoint.X)
- {
- parentUI.MaxWidth = (pageBound.Right - drawRect.X - cropPoint.X);
- }
- else
- {
- parentUI.MaxWidth = (drawRect.Right - pageBound.X - cropPoint.X);
- }
- if (mouseEndPoint.Y >= mouseStartPoint.Y)
- {
- parentUI.MaxHeight = (pageBound.Bottom - drawRect.Y - cropPoint.Y);
- }
- else
- {
- parentUI.MaxHeight = (drawRect.Bottom - pageBound.Y - cropPoint.Y);
- }
- }
- lastTextui.MinWidth = drawRect.Width;
- lastTextui.MinHeight = drawRect.Height;
- lastTextui.Foreground = new SolidColorBrush(textColor);
- lastTextui.Background = new SolidColorBrush(backgroundColor);
- lastTextBorder.Padding = new Thickness(0);
- lastTextBorder.BorderBrush = new SolidColorBrush(borderColor);
- 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;
- CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
- lastTextui.FontFamily = new FontFamily(fontFamily);
- lastTextui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
- lastTextui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
- lastTextui.AcceptsReturn = true;
- lastTextui.TextWrapping = TextWrapping.Wrap;
- lastTextui.TextAlignment = TextAlignment.Left;
- switch (annotFreeText.Alignment)
- {
- case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
- lastTextui.TextAlignment = TextAlignment.Left;
- break;
- case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
- lastTextui.TextAlignment = TextAlignment.Right;
- break;
- case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
- lastTextui.TextAlignment = TextAlignment.Center;
- break;
- default:
- break;
- }
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// Use to calculate the point drawn at a fixed angle
- /// </summary>
- /// <param name="currentPoint">
- /// Current point
- /// </param>
- /// <param name="startPoint">
- /// Start point
- /// </param>
- /// <param name="pageBound">
- /// Maximum drawing area
- /// </param>
- /// <returns>
- /// Return the calculated point
- /// </returns>
- internal Point CalcAnglePoint(Point currentPoint, Point startPoint, Rect pageBound)
- {
- Vector angleVector = currentPoint - startPoint;
- Point originPoint = new Point(startPoint.X, startPoint.Y - angleVector.Length);
- Vector orignVector = originPoint - startPoint;
- Rect checkRect = pageBound;
- int angle = (int)Vector.AngleBetween(orignVector, angleVector);
- if (angle < 0)
- {
- angle += 360;
- }
- int mod = angle % 45;
- int quot = angle / 45;
- Point anglePoint = currentPoint;
- int rotateAngle = 0;
- if (mod < 22)
- {
- Matrix rotateMatrix = new Matrix();
- rotateAngle = quot * 45;
- rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
- anglePoint = rotateMatrix.Transform(originPoint);
- anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
- }
- else
- {
- Matrix rotateMatrix = new Matrix();
- rotateAngle = (quot + 1) * 45;
- rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
- anglePoint = rotateMatrix.Transform(originPoint);
- anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
- }
- if (checkRect.Contains(anglePoint) == false)
- {
- switch (rotateAngle)
- {
- case 0:
- {
- anglePoint.X = startPoint.X;
- anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
- }
- break;
- case 45:
- {
- double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
- addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
- anglePoint.X = startPoint.X + addValue;
- anglePoint.Y = startPoint.Y - addValue;
- }
- break;
- case 90:
- {
- anglePoint.X = startPoint.X + Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
- anglePoint.Y = startPoint.Y;
- }
- break;
- case 135:
- {
- double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
- addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
- anglePoint.X = startPoint.X + addValue;
- anglePoint.Y = startPoint.Y + addValue;
- }
- break;
- case 180:
- {
- anglePoint.X = startPoint.X;
- anglePoint.Y = Math.Min(anglePoint.Y, checkRect.Bottom);
- }
- break;
- case 225:
- {
- double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
- addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
- anglePoint.X = startPoint.X - addValue;
- anglePoint.Y = startPoint.Y + addValue;
- }
- break;
- case 270:
- {
- anglePoint.X = startPoint.X - Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
- anglePoint.Y = startPoint.Y;
- }
- break;
- case 315:
- {
- double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
- addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
- anglePoint.X = startPoint.X - addValue;
- anglePoint.Y = startPoint.Y - addValue;
- }
- break;
- case 360:
- {
- anglePoint.X = startPoint.X;
- anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
- }
- break;
- default:
- break;
- }
- }
- return anglePoint;
- }
- }
- }
|