123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920 |
- 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; }
- protected int pageIndex { get; set; } = -1;
- /// <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;
- this.pageIndex = cPDFPage.PageIndex;
- 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();
- pageIndex = -1;
- 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,int pageIndex)
- {
- if(pageIndex==this.pageIndex)
- {
- 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(annotFreeText.FreeTextDa.FontName + ",Microsoft YaHei");
- 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(annotFreeText.FreeTextDa.FontName + ",Microsoft YaHei");
- 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;
- }
- }
- }
|