12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFDocument.Action;
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool.DrawTool;
- using ComPDFKit.Tool.FillTool;
- using ComPDFKit.Tool.Help;
- using ComPDFKit.Tool.SettingParam;
- using ComPDFKit.Viewer.Helper;
- using ComPDFKit.Viewer.Layer;
- using ComPDFKitViewer;
- using ComPDFKitViewer.Annot;
- using ComPDFKitViewer.BaseObject;
- using ComPDFKitViewer.Helper;
- using ComPDFKitViewer.Layer;
- using ComPDFKitViewer.Widget;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using UndoAction = ComPDFKitViewer.Helper.UndoAction;
- namespace ComPDFKit.Tool
- {
- public struct MouseEventObject
- {
- public MouseEventArgs mouseButtonEventArgs;
- public MouseHitTestType hitTestType;
- public C_ANNOTATION_TYPE annotType;
- /// <summary>
- /// Identifies whether the object is created
- /// </summary>
- public bool IsCreate;
- public bool IsDrawing;
- public bool IsMersured;
- public object Data;
- }
- public enum MouseHitTestType
- {
- Unknown,
- Text,
- Annot,
- SelectRect,
- Widget,
- TextEdit,
- ImageEdit,
- ImageSelect,
- MultiTextEdit,
- SelectedPageRect,
- MultiSelector
- }
- public enum ToolType
- {
- None = -1,
- Viewer,
- Pan,
- CreateAnnot,
- WidgetEdit,
- ContentEdit,
- Customize,
- SelectedPage,
- /// <summary>
- /// Scroller Tool
- /// </summary>
- ViewerScroller
- }
- public partial class CPDFViewerTool : UserControl
- {
- public bool IsDocumentModified
- {
- get => isDocumentModified;
- set
- {
- isDocumentModified = value;
- DocumentModifiedChanged?.Invoke(this, EventArgs.Empty);
- }
- }
- public CPDFViewerTool()
- {
- InitializeComponent();
- BindCommand();
- Application.Current.Exit += Current_Exit;
- InsertSelectImageView();
- InsertAnnotView();
- InsertAnnotEditView();
- InsertWidgetView();
- InsertSelectedRectView();
- InsertMultiSelectedRectView();
- InsertCustomizeToolView();
- InsertSelectTextView();
- //Frame Select
- InsertFrameSelectToolView();
- InsertTextEditView();
- InsertPageSelectedRectView();
- AnnotSelectInsert();
- FillManager = new FillManager(this);
- }
- internal void InvokeMouseLeftDown(MouseEventObject e)
- {
- MouseLeftButtonDownHandler?.Invoke(this, e);
- }
- private void Current_Exit(object sender, ExitEventArgs e)
- {
- GetCPDFViewer().Dispose();
- }
- protected override Visual GetVisualChild(int index)
- {
- return base.GetVisualChild(index);
- }
- protected override int VisualChildrenCount => base.VisualChildrenCount;
- public event EventHandler<MouseEventObject> MouseLeftButtonDownHandler;
- public event EventHandler<MouseEventObject> MouseLeftButtonUpHandler;
- public event EventHandler<MouseEventObject> MouseMoveHandler;
- public event EventHandler<MouseEventObject> MouseRightButtonDownHandler;
- public event EventHandler<ScrollChangedEventArgs> ScrollChangedHandler;
- public event EventHandler DrawChanged;
- public event EventHandler DocumentModifiedChanged;
- private ToolType currentModel = ToolType.Viewer;
- DefaultSettingParam defaultSettingParam = new DefaultSettingParam();
- DefaultDrawParam defaultDrawParam = new DefaultDrawParam();
- MeasureSetting measureSetting = new MeasureSetting();
- Point Point = new Point();
- Point CachePoint = new Point();
- private bool isMultiSelected;
- private bool isDocumentModified = false;
- public FillManager FillManager { get; private set; }
- public bool CanAddTextEdit = true;
- protected bool isContinueCreateTextEdit = false;
- public bool GetIsMultiSelected()
- {
- return isMultiSelected;
- }
- /// <summary>
- /// Set whether continuous text editing is required
- /// </summary>
- /// <param name="isContinueCreateTextEdit"></param>
- public void SetContinueCreateTextEdit(bool isContinueCreateTextEdit)
- {
- this.isContinueCreateTextEdit = isContinueCreateTextEdit;
- CanAddTextEdit = true;
- }
- /// <summary>
- /// Does it support multiple selection
- /// </summary>
- /// <param name="isMulti">true Can MultiSelected</param>
- public void SetIsMultiSelected(bool isMulti)
- {
- isMultiSelected = isMulti;
- }
- public DefaultSettingParam GetDefaultSettingParam()
- {
- return defaultSettingParam;
- }
- public DefaultDrawParam GetDefaultDrawParam()
- {
- return defaultDrawParam;
- }
- /// <summary>
- /// Set default painting parameters
- /// </summary>
- /// <param name="defaultDrawParam"></param>
- public void SetDefaultDrawParam(DefaultDrawParam defaultDrawParam = null)
- {
- if (defaultDrawParam == null)
- {
- this.defaultDrawParam = new DefaultDrawParam();
- }
- else
- {
- this.defaultDrawParam = defaultDrawParam;
- }
- }
- public MeasureSetting GetMeasureSetting()
- {
- return measureSetting;
- }
- public bool IsSelectRectMousePoint()
- {
- if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
- {
- return true;
- }
- return false;
- }
- private void LinkAnnotAction(BaseAnnot annot)
- {
- AnnotData data = annot.GetAnnotData();
- CPDFLinkAnnotation linkAnnot = data.Annot as CPDFLinkAnnotation;
- CPDFAction action = linkAnnot.GetLinkAction();
- if (action != null)
- {
- ActionProcess(action);
- }
- else
- {
- CPDFDestination dest = linkAnnot.GetDestination(PDFViewer.GetDocument());
- if (dest != null)
- {
- CPDFGoToAction gotoAction = new CPDFGoToAction();
- gotoAction.SetDestination(PDFViewer.GetDocument(), dest);
- ActionProcess(gotoAction);
- }
- }
- }
- public void ActionProcess(CPDFAction action)
- {
- if (action == null)
- {
- return;
- }
- switch (action.ActionType)
- {
- case C_ACTION_TYPE.ACTION_TYPE_NAMED:
- {
- CPDFNamedAction namedAction = action as CPDFNamedAction;
- string namedStr = namedAction.GetName();
- switch (namedStr)
- {
- case "FirstPage":
- {
- PDFViewer?.GoToPage(0, new Point(0, 0));
- break;
- }
- case "LastPage":
- {
- PDFViewer?.GoToPage(PDFViewer.GetDocument().PageCount - 1, new Point(0, 0));
- break;
- }
- case "NextPage":
- if (PDFViewer != null)
- {
- int nextIndex = PDFViewer.CurrentRenderFrame.PageIndex + 1;
- if (nextIndex < PDFViewer.GetDocument().PageCount)
- {
- PDFViewer.GoToPage(nextIndex, new Point(0, 0));
- }
- }
- break;
- case "PrevPage":
- if (PDFViewer != null)
- {
- int prevIndex = PDFViewer.CurrentRenderFrame.PageIndex - 1;
- if (prevIndex >= 0)
- {
- PDFViewer.GoToPage(prevIndex, new Point(0, 0));
- }
- }
- break;
- default:
- break;
- }
- break;
- }
- case C_ACTION_TYPE.ACTION_TYPE_GOTO:
- if (PDFViewer != null)
- {
- CPDFGoToAction gotoAction = action as CPDFGoToAction;
- CPDFDestination dest = gotoAction.GetDestination(PDFViewer.GetDocument());
- if (dest != null)
- {
- Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
- PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
- }
- }
- break;
- case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
- if (PDFViewer != null)
- {
- CPDFGoToRAction gotorAction = action as CPDFGoToRAction;
- CPDFDestination dest = gotorAction.GetDestination(PDFViewer.GetDocument());
- if (dest != null)
- {
- Size pageSize = DataConversionForWPF.CSizeConversionForSize(PDFViewer.GetDocument().GetPageSize(dest.PageIndex));
- PDFViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, pageSize.Height - dest.Position_Y));
- }
- }
- break;
- case C_ACTION_TYPE.ACTION_TYPE_URI:
- {
- CPDFUriAction uriAction = action as CPDFUriAction;
- string uri = uriAction.GetUri();
- try
- {
- if (!string.IsNullOrEmpty(uri))
- {
- Process.Start(uri);
- }
- }
- catch (Exception ex)
- {
- }
- }
- break;
- default:
- break;
- }
- }
- protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
- {
- if (isContinueCreateTextEdit)
- {
- if (lastSelectedRect != null)
- {
- CanAddTextEdit = false;
- }
- else
- {
- CanAddTextEdit = true;
- }
- }
- if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
- {
- return;
- }
-
- if (!HitTestBorder())
- {
- RemovePopTextUI();
- }
- Focus();
- if (FillManager?.MouseDownHandle(e) == true)
- {
- return;
- }
- Mouse.Capture(this, CaptureMode.SubTree);
- MouseEventObject mouseEventObject = new MouseEventObject
- {
- mouseButtonEventArgs = e,
- hitTestType = MouseHitTestType.Unknown,
- annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
- IsCreate = false
- };
- if (currentModel == ToolType.ViewerScroller)
- {
- // Scroller Tool Only Scrolle
- MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
- return;
- }
- if (isDrawSelectRect)
- {
- if (e.ClickCount == 2)
- {
- // Refresh the currently selected annotation object to ensure it is the latest
- AnnotHitTest();
- if (cacheHitTestAnnot is FreeTextAnnot)
- {
- BuildPopTextUI(cacheHitTestAnnot);
- isDrawSelectRect = false;
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
- MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
- return;
- }
- if (cacheHitTestAnnot is StickyNoteAnnot)
- {
- (cacheHitTestAnnot as StickyNoteAnnot).PopStickyNote();
- }
- }
- // Click inside the selected rectangle area
- if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
- {
- mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
- MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
- return;
- }
- else
- {
- CleanSelectedRect();
- }
- }
- if (IsDrawEditAnnot)
- {
- // Click inside the selected rectangle area
- if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
- {
- mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
- MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
- return;
- }
- }
- Point = e.GetPosition(this);
- // Annotation selection effect
- if ((currentModel == ToolType.Pan
- || currentModel == ToolType.CreateAnnot)
- && AnnotHitTest()
- && IsCanSave()
- && !PDFViewer.GetIsShowStampMouse()
- )
- {
- //if (!IsCacheRedaction)
- {
- if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
- {
- LinkAnnotAction(cacheHitTestAnnot);
- }
- else
- {
- List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
- {
- C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
- };
- if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
- {
- mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
- SetEditAnnotObject();
- }
- else
- {
- SelectedAnnot();
- CleanDrawSelectImage();
- }
- isDrawSelectRect = true;
- }
- }
- mouseEventObject.hitTestType = MouseHitTestType.Annot;
- if (cacheHitTestAnnot != null)
- {
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- }
- else
- {
- mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
- }
- }
- // Form selected effect
- else if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
- {
- mouseEventObject.hitTestType = MouseHitTestType.Widget;
- mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
- FormClickProcess();
- }
- else if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
- {
- if (!IsText() && DrawDownSelectImage(true))
- {
- mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
- }
- else
- {
- ReDrawSelectImage();
- }
- }
- // Form creation mode
- else if (currentModel == ToolType.WidgetEdit)
- {
- if (!AnnotSelectAreaHitTest())
- {
- if (AnnotWidgetHitTest())
- {
- BaseWidget hitWidget = PDFViewer?.AnnotHitTest() as BaseWidget;
- cacheHitTestAnnot = null;
- if (AnnotSelectGetCount() == 0 && hitWidget != null)
- {
- AnnotSelectAddItem(hitWidget.GetAnnotData());
- cacheHitTestAnnot = hitWidget;
- SelectedAnnot();
- }
- mouseEventObject.hitTestType = MouseHitTestType.Annot;
- mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
- }
- else
- {
- AnnotSelectAreaHit();
- }
- }
- AnnotSelectMoveHit();
- }
- // Content editing mode
- else if (currentModel == ToolType.ContentEdit)
- {
- OpenSelectedMulti(isMultiSelected);
- if (!PDFViewer.GetIsShowStampMouse())
- {
- DrawTextEditDownEvent(true);
- }
- if (lastSelectedRect != null)
- {
- //Multi selection processing optimization, other click effects
- DrawEndFrameSelect();
- if (!Keyboard.IsKeyDown(multiKey) || !isMultiSelected)
- {
- CleanSelectedMultiRect();
- OpenSelectedMulti(false);
- if (PDFViewer.CurrentRenderFrame != null)
- {
- currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
- if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
- {
- SetEditTextRect(PDFViewer.CurrentRenderFrame);
- if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
- {
- DrawSelectedEditAreaForIndex();
- }
- }
- }
- ReDrawSelectedMultiRect();
- }
- if (lastSelectedRect == null)
- {
- return;
- }
- SelectedMultiRect(lastSelectedRect.GetRect(), lastSelectedRect.GetMaxRect(), SelectedType.PDFEdit);
- HideDrawSelectedMultiRect();
- lastSelectedRect.DataChanged -= SelectedRect_DataChanged;
- lastSelectedRect.DataChanged += SelectedRect_DataChanged;
- }
- else
- {
- if (Keyboard.IsKeyDown(multiKey) && isMultiSelected)
- {
- DelMultiSelectRect();
- }
- if (HitTestMultiSelectedRect())
- {
- mouseEventObject.hitTestType = MouseHitTestType.MultiTextEdit;
- }
- else
- {
- //Clear the currently selected object
- startSelectedRect = null;
- startSelectedIndex = -1;
- startSelectedPageIndex = -1;
- startSelectedEditAreaObject = null;
- CleanSelectedMultiRect();
- OpenSelectedMulti(false);
- if (PDFViewer.CurrentRenderFrame != null)
- {
- currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
- if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
- {
- SetEditTextRect(PDFViewer.CurrentRenderFrame);
- if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
- {
- DrawSelectedEditAreaForIndex();
- }
- }
- }
- ReDrawSelectedMultiRect();
- }
- }
- }
- else if (currentModel == ToolType.SelectedPage)
- {
- if (HitTestPageSelectedRect())
- {
- }
- else
- {
- CleanPageSelectedRect();
- CreatePageSelectdRect();
- }
- mouseEventObject.hitTestType = MouseHitTestType.SelectedPageRect;
- }
- MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
- }
- protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
- {
- if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
- {
- return;
- }
- if (FillManager?.MouseUpHandle(e) == true)
- {
- return;
- }
- if (currentModel == ToolType.WidgetEdit)
- {
- bool areaSelectAdd = AnnotSelectAreaSelect(true);
- if (AnnotWidgetHitTest())
- {
- BaseWidget checkItem = PDFViewer?.AnnotHitTest() as BaseWidget;
- AnnotSelectAddItem(checkItem?.GetAnnotData(), !AnnotSelectAreaHitTest());
- }
- if (AnnotSelectGetCount() > 1)
- {
- CleanSelectedRect();
- }
- if (IsMoved == false && AnnotSelectAreaHitTest() == false && AnnotWidgetHitTest() == false)
- {
- AnnotSelectClean();
- }
- if (AnnotSelectGetCount() == 1)
- {
- cacheHitTestAnnot = AnnotSelectGetAnnot();
- if (cacheHitTestAnnot != null)
- {
- BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
- SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
- if (selectedRect != null)
- {
- if (IsMoved && areaSelectAdd == false)
- {
- selectedRect.UpdateAnnotData(cacheHitTestAnnot.GetAnnotData());
- }
- else
- {
- selectedRect.SetAnnotData(cacheHitTestAnnot.GetAnnotData());
- }
- selectedRect.Draw();
- }
- isDrawSelectRect = true;
- }
- }
- }
- MouseEventObject mouseEventObject = new MouseEventObject
- {
- mouseButtonEventArgs = e,
- hitTestType = MouseHitTestType.Unknown,
- annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
- IsCreate = false
- };
- if (isDrawSelectRect || IsDrawEditAnnot)
- {
- mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
- if (cacheHitTestAnnot != null)
- {
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- }
- else
- {
- mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
- }
- }
- MouseLeftButtonUpHandler?.Invoke(this, mouseEventObject);
- ReleaseMouseCapture();
- if (currentModel == ToolType.WidgetEdit)
- {
- AnnotSelectUpdate();
- AnnotSelectDraw();
- AnnotSelectSave();
- AreaMoveData = new AnnotSelectAreaData();
- }
- IsMoved = false;
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- IsMoved = e.LeftButton == MouseButtonState.Pressed;
- if (PDFViewer == null || PDFViewer.CurrentRenderFrame == null)
- {
- return;
- }
- if(FillManager?.MouseMoveHandle(e)==true)
- {
- return;
- }
- Cursor oldCursor = this.Cursor;
- Cursor newCursor = this.Cursor;
- MouseEventObject mouseEventObject = new MouseEventObject
- {
- mouseButtonEventArgs = e,
- hitTestType = MouseHitTestType.Unknown,
- annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
- IsCreate = false
- };
- if (currentModel == ToolType.ViewerScroller)
- {
- // Scroller Tool Only Scrolle
- MouseLeftButtonDownHandler?.Invoke(this, mouseEventObject);
- return;
- }
- if (Mouse.LeftButton != MouseButtonState.Pressed)
- {
- List<ToolType> allowModeList = new List<ToolType>()
- {
- ToolType.Pan,
- ToolType.Viewer
- };
- if (allowModeList.Contains(currentModel))
- {
- newCursor = Cursors.Arrow;
- if (caheMoveAnnot is BaseWidget)
- {
- BaseWidget widget = (BaseWidget)caheMoveAnnot;
- if (widget.GetFormType() == PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON && PDFViewer != null)
- {
- newCursor = Cursors.Hand;
- }
- }
- if (caheMoveAnnot is LinkAnnot)
- {
- newCursor = Cursors.Hand;
- }
- }
- }
- if (!isDrawSelectRect && !IsDrawEditAnnot)
- {
- if (AnnotMoveHitTest())
- {
- if (isCacheRedaction)
- {
- (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(true);
- (caheMoveAnnot as RedactionAnnot).Draw();
- }
- mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
- mouseEventObject.IsMersured = caheMoveAnnot.GetAnnotData().Annot.IsMeasured();
- }
- else
- {
- if (isCacheRedaction)
- {
- isCacheRedaction = false;
- (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(false);
- (caheMoveAnnot as RedactionAnnot).Draw();
- }
- caheMoveAnnot = null;
- if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer))
- {
- if (GetTextSelectInfo() == null)
- {
- DrawMoveSelectImage();
- }
- }
- }
- }
- else
- {
- if (AnnotMoveHitTest())
- {
- if (DrawSelectRectDownEvent() == false && Mouse.LeftButton != MouseButtonState.Pressed)
- {
- mouseEventObject.annotType = caheMoveAnnot.GetAnnotData().AnnotType;
- }
- if (isCacheRedaction)
- {
- (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(true);
- (caheMoveAnnot as RedactionAnnot)?.Draw();
- }
- }
- else
- {
- if (isCacheRedaction)
- {
- (caheMoveAnnot as RedactionAnnot)?.SetIsMouseHover(false);
- (caheMoveAnnot as RedactionAnnot)?.Draw();
- }
- }
- if (mouseEventObject.annotType == C_ANNOTATION_TYPE.C_ANNOTATION_NONE)
- {
- mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
- if (cacheHitTestAnnot != null)
- {
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- mouseEventObject.IsMersured = cacheHitTestAnnot.GetAnnotData().Annot.IsMeasured();
- }
- else
- {
- mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
- }
- }
- }
- if (currentModel == ToolType.WidgetEdit)
- {
- AnnotSelectAreaDraw();
- if (!AnnotSelectMoveDraw())
- {
- AnnotSelectAreaHover();
- }
- }
- MouseMoveHandler?.Invoke(this, mouseEventObject);
- PDFViewer.SetCustomMousePoint(Mouse.GetPosition(this).Y, Mouse.GetPosition(this).X);
- if (oldCursor != newCursor)
- {
- this.Cursor = newCursor;
- }
- }
- protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
- {
- MouseEventObject mouseEventObject = new MouseEventObject
- {
- mouseButtonEventArgs = e,
- hitTestType = MouseHitTestType.Unknown,
- annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
- IsCreate = false
- };
- if (currentModel == ToolType.Pan || currentModel == ToolType.Viewer)
- {
- if (GetMousePointToTextSelectInfo())
- {
- mouseEventObject.hitTestType = MouseHitTestType.Text;
- }
- }
- if (isDrawSelectRect)
- {
- // Click inside the selected rectangle area
- if (DrawSelectRectDownEvent() && cacheHitTestAnnot != null)
- {
- mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
- return;
- }
- }
- if (IsDrawEditAnnot)
- {
- // Click inside the selected rectangle area
- if (DrawEditAnnotDownEvent() && cacheHitTestAnnot != null)
- {
- mouseEventObject.hitTestType = MouseHitTestType.SelectRect;
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
- return;
- }
- }
- Point = e.GetPosition(this);
- // Annotation selection effect
- if ((currentModel == ToolType.Pan || currentModel == ToolType.CreateAnnot))
- {
- if (AnnotHitTest())
- {
- if (!isCacheRedaction)
- {
- if (cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK && currentModel != ToolType.CreateAnnot)
- {
- LinkAnnotAction(cacheHitTestAnnot);
- }
- else
- {
- List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
- {
- C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
- };
- if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
- {
- SetEditAnnotObject();
- DrawEditAnnotLayer();
- }
- else
- {
- SelectedAnnot();
- DrawSelectedLayer();
- }
- isDrawSelectRect = true;
- }
- }
- mouseEventObject.hitTestType = MouseHitTestType.Annot;
- if (cacheHitTestAnnot != null)
- {
- mouseEventObject.annotType = cacheHitTestAnnot.GetAnnotData().AnnotType;
- }
- else
- {
- mouseEventObject.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
- }
- }
- else
- {
- CleanSelectedRect();
- }
- }
- // Form selection effect
- if ((currentModel == ToolType.Pan || currentModel == ToolType.Viewer) && AnnotWidgetHitTest())
- {
- mouseEventObject.hitTestType = MouseHitTestType.Widget;
- mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
- FormClickProcess();
- }
- // Form creation mode
- if (currentModel == ToolType.WidgetEdit)
- {
- if (!AnnotSelectAreaHitTest())
- {
- if (AnnotWidgetHitTest())
- {
- cacheHitTestAnnot = PDFViewer?.AnnotHitTest() as BaseWidget;
- SelectedAnnot();
- DrawSelectedLayer();
- mouseEventObject.hitTestType = MouseHitTestType.Annot;
- mouseEventObject.annotType = cacheMoveWidget.GetAnnotData().AnnotType;
- AnnotSelectClean();
- AnnotSelectAddItem(cacheHitTestAnnot?.GetAnnotData());
- }
- else
- {
- CleanSelectedRect();
- }
- }
- else
- {
- mouseEventObject.hitTestType = MouseHitTestType.MultiSelector;
- }
- }
- // Content editing mode
- if (currentModel == ToolType.ContentEdit)
- {
- if (e.ClickCount == 1)
- {
- DrawTextEditDownEvent(false);
- if (GetLastSelectedRect() != null)
- {
- EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
- switch (editAreaObject.cPDFEditArea.Type)
- {
- case CPDFEditType.EditText:
- mouseEventObject.hitTestType = MouseHitTestType.TextEdit;
- break;
- case CPDFEditType.EditImage:
- mouseEventObject.hitTestType = MouseHitTestType.ImageEdit;
- break;
- default:
- break;
- }
- }
- }
- }
- else
- {
- if ((currentModel == ToolType.Viewer || currentModel == ToolType.Pan) && mouseEventObject.hitTestType == MouseHitTestType.Unknown && DrawDownSelectImage(false))
- {
- mouseEventObject.hitTestType = MouseHitTestType.ImageSelect;
- }
- }
- if (currentModel == ToolType.SelectedPage)
- {
- if (HitTestPageSelectedRect())
- {
- mouseEventObject.hitTestType = MouseHitTestType.SelectedPageRect;
- }
- }
- MouseRightButtonDownHandler?.Invoke(this, mouseEventObject);
- }
- public bool GetIsCropMode()
- {
- if (lastSelectedRect != null)
- {
- return lastSelectedRect.GetCurrentDrawPointType() == DrawPointType.Crop;
- }
- return false;
- }
- public void SetCropMode(bool crop)
- {
- if (lastSelectedRect != null)
- {
- List<PointControlType> ignoreList = new List<PointControlType>();
- if (crop)
- {
- lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Crop);
- ignoreList.Add(PointControlType.Body);
- // Initialize ClipRect
- ClipThickness = new Thickness(0, 0, 0, 0);
- if (editArea.TryGetValue(lastSelectedRect, out EditAreaObject editAreaObject))
- {
- cropIndex = editAreaObject.EditAreaIndex;
- }
- lastSelectedRect.DataChanged -= LastSelectedRect_DataChanged;
- }
- else
- {
- lastSelectedRect.SetCurrentDrawPointType(DrawPointType.Square);
- cropIndex = -1;
- lastSelectedRect.DataChanged += LastSelectedRect_DataChanged;
- }
- lastSelectedRect.SetIgnorePoints(ignoreList);
- lastSelectedRect.Draw();
- }
- }
- internal void SetToolType(ToolType model)
- {
- currentModel = model;
- CPDFViewer pdfViewer = GetCPDFViewer();
- if (pdfViewer != null)
- {
- if (currentModel == ToolType.WidgetEdit)
- {
- pdfViewer.IsHideFormShow = true;
- }
- else
- {
- pdfViewer.IsHideFormShow = false;
- }
- }
- }
- public ToolType GetToolType()
- {
- return currentModel;
- }
- public void SavePoint()
- {
- CachePoint = Point;
- }
- public void CleanPoint()
- {
- CachePoint = new Point();
- }
- private void CPDFViewerTool_Loaded(object sender, RoutedEventArgs e)
- {
- PDFViewer.DrawChanged += PDFViewer_DrawChanged;
- PDFViewer.UndoManager.HistoryChanged += UndoManager_HistoryChanged;
- PDFViewer.MouseEnter += PDFViewer_MouseEnter;
- PDFViewer.MouseLeave += PDFViewer_MouseLeave;
- }
- private void PDFViewer_MouseLeave(object sender, MouseEventArgs e)
- {
- PDFViewer.IsVisibilityMouse(false);
- }
- private void PDFViewer_MouseEnter(object sender, MouseEventArgs e)
- {
- PDFViewer.IsVisibilityMouse(true);
- }
- private void UndoManager_HistoryChanged(object sender, KeyValuePair<UndoAction, IHistory> data)
- {
- IsDocumentModified = true;
- }
- private void CPDFViewerTool_Unloaded(object sender, RoutedEventArgs e)
- {
- PDFViewer.DrawChanged -= PDFViewer_DrawChanged;
- }
- private void PDFViewer_DrawChanged(object sender, EventArgs e)
- {
- SizeChangeds();
- AnnotSelectUpdate();
- AnnotSelectDraw();
- DrawChanged?.Invoke(this, e);
- }
- public void SizeChangeds()
- {
- if (IsLoaded)
- {
- if (cacheHitTestAnnot != null)
- {
- BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
- bool Update = (baseLayer1 as AnnotLayer).GetUpdate(ref cacheHitTestAnnot);
- if (Update)
- {
- if (IsDrawEditAnnot)
- {
- SetEditAnnotObject();
- DrawEditAnnotLayer();
- }
- if (isDrawSelectRect)
- {
- List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
- {
- C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
- };
- if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
- {
- SetEditAnnotObject();
- DrawEditAnnotLayer();
- }
- else
- {
- SelectedAnnot();
- }
- DrawSelectedLayer();
- }
- }
- else
- {
- SelectedAnnot(null);
- }
- }
- else if (selectedPageIndex != -1 && selectedAnnotIndex != -1)
- {
- BaseLayer baseLayer1 = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag());
- cacheHitTestAnnot = (baseLayer1 as AnnotLayer).GetSelectedAnnot(ref selectedPageIndex, ref selectedAnnotIndex);
- if (cacheHitTestAnnot != null)
- {
- List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
- {
- C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
- };
- if (cacheHitTestAnnot != null && list.Contains(cacheHitTestAnnot.CurrentType))
- {
- SetEditAnnotObject();
- DrawEditAnnotLayer();
- }
- else
- {
- SelectedAnnot();
- }
- DrawSelectedLayer();
- isDrawSelectRect = true;
- }
- }
- if (PDFViewer.CurrentRenderFrame != null)
- {
- currentZoom = PDFViewer.CurrentRenderFrame.ZoomFactor;
- if (PDFViewer.CurrentRenderFrame.IsCacheEditPage == true && currentModel == ToolType.ContentEdit)
- {
- SetEditTextRect(PDFViewer.CurrentRenderFrame);
- if (selectedEditPageIndex != -1 && selectedEditAreaIndex != -1)
- {
- DrawSelectedEditAreaForIndex();
- }
- }
- }
- ReDrawSelectedMultiRect();
- ReDrawWidget();
- ReDrawSelectText();
- ReDrawSelectImage();
- UpdateFormHitPop();
- UpdateTextPop();
- }
- }
- private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
- {
- }
- private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
- {
- }
- private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
- {
- ScrollChangedHandler?.Invoke(this, e);
- }
- }
- }
|