123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFDocument.Action;
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool.DrawTool;
- 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,
- }
- public class PopupControlInfo
- {
- public BaseAnnot Annot { get; set; }
- public UIElement PopUpControl { get; set; }
- }
- 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();
- }
- public void ReplaceCPDFViewer(CPDFViewer newViewer)
- {
- ScrollContainer.Content = newViewer;
- PDFViewer = newViewer;
- InsertSelectImageView();
- InsertAnnotView();
- InsertAnnotEditView();
- InsertWidgetView();
- InsertSelectedRectView();
- InsertMultiSelectedRectView();
- InsertCustomizeToolView();
- InsertSelectTextView();
- InsertTextEditView();
- InsertPageSelectedRectView();
- }
- 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;
- public event EventHandler<PopupControlInfo> PopUpControlHandler;
- 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 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();
- Mouse.Capture(this, CaptureMode.SubTree);
- MouseEventObject mouseEventObject = new MouseEventObject
- {
- mouseButtonEventArgs = e,
- hitTestType = MouseHitTestType.Unknown,
- annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE,
- IsCreate = false
- };
- 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 (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;
- }
- 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 (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;
- }
- }
- 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);
- }
- }
- }
|