123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 |
- using System;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
- using Compdfkit_Tools.Helper;
- using ComPDFKit.DigitalSign;
- using ComPDFKit.PDFAnnotation.Form;
- using ComPDFKitViewer;
- using static ComPDFKit.Tool.CPDFToolManager;
- namespace Compdfkit_Tools.PDFControl
- {
- public partial class DigitalSignatureControl : UserControl, INotifyPropertyChanged
- {
- #region Properties
-
- private bool isFirstLoad = true;
- public PDFViewControl PDFViewControl = new PDFViewControl();
- private SignatureStatusBarControl signatureStatusBarControl;
- private PanelState panelState = PanelState.GetInstance();
- private CPDFDisplaySettingsControl displaySettingsControl = null;
- private CPDFSignatureWidget currentSignatureWidget;
- private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
- public event EventHandler<bool> OnCanSaveChanged;
- public event EventHandler<string> AfterFillSignature;
- public event EventHandler SignatureStatusChanged;
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged([CallerMemberName] string name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- /// <summary>
- /// Whether the undo operation can be performed.
- /// </summary>
- public bool CanUndo
- {
- get
- {
- if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
- {
- return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo;
- }
- return false;
- }
- }
- /// <summary>
- /// Whether the redo operation can be performed.
- /// </summary>
- public bool CanRedo
- {
- get
- {
- if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
- {
-
- return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo;
- }
- return false;
- }
- }
- /// <summary>
- /// Whether the save operation can be performed.
- /// </summary>
- public bool CanSave
- {
- get
- {
- if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
- {
- if (PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
- PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
- {
- return true;
- }
- }
- return false;
- }
- }
- #endregion
-
- /// <summary>
- /// A digital signature control should be used in a window, and it should be initialized with a PDFViewer.
- /// Certificates will be saved in the TrustedFolder.
- /// </summary>
- public DigitalSignatureControl()
- {
- InitializeComponent();
- DataContext = this;
- string trustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
- if (!Directory.Exists(trustedFolder))
- {
- Directory.CreateDirectory(trustedFolder);
- }
- CPDFSignature.SignCertTrustedFolder = trustedFolder;
- }
- #region Public Method
-
- /// <summary>
- /// Disconnect all the specified elements that are already the logical children of another element. And reset bar control status.
- /// </summary>
- public void ClearViewerControl()
- {
- PDFGrid.Child = null;
- BotaContainer.Child = null;
- PropertyContainer.Child = null;
- displaySettingsControl = null;
- SignatureStatusBorder.Child = null;
- DigitalSignatureBarControl.ClearAllToolState();
- }
- /// <summary>
- /// Init controls with pdfViewer, and load events.
- /// </summary>
- /// <param name="pdfViewer"></param>
- public void InitWithPDFViewer(PDFViewControl pdfViewer)
- {
- PDFViewControl = pdfViewer;
- PDFGrid.Child = PDFViewControl;
- FloatPageTool.InitWithPDFViewer(pdfViewer);
-
- PDFViewControl.PDFToolManager.SetToolType(ToolType.Viewer);
- PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.ClearHistory();
-
- DigitalSignatureBarControl.DigitalSignatureActionChanged -= DigitalSignatureBarControl_DigitalSignatureActionChanged;
- DigitalSignatureBarControl.DigitalSignatureActionChanged += DigitalSignatureBarControl_DigitalSignatureActionChanged;
-
- //PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
- //PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
- //PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
- //PDFViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
-
- panelState.PropertyChanged -= PanelState_PropertyChanged;
- panelState.PropertyChanged += PanelState_PropertyChanged;
- }
-
- /// <summary>
- /// Separately, init PDFView and load undo manager event. Only use for ensuring SaveBtn is enabled after deleting digital signature on Viewer mode.
- /// </summary>
- /// <param name="pdfViewer"></param>
- public void LoadUndoManagerEvent(CPDFViewer pdfViewer)
- {
- //PDFViewControl.PDFView = pdfViewer;
- //PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
- //PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
- }
- /// <summary>
- /// Set child for BOTAContainer with BOTABarControl.
- /// </summary>
- /// <param name="botaControl"></param>
- public void SetBOTAContainer(CPDFBOTABarControl botaControl)
- {
- BotaContainer.Child = botaControl;
- }
- /// <summary>
- /// Create a certificate info dialog with signature.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e">Signature to init certificate</param>
- public void ViewCertificateEvent(object sender, CPDFSignature e)
- {
- Window parentWindow = Window.GetWindow((DependencyObject)sender);
- ViewCertificateDialog dialog = new ViewCertificateDialog()
- {
- Owner = parentWindow
- };
- dialog.InitCertificateList(e);
- dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
- {
- SignatureStatusChanged?.Invoke(this, null);
- };
- if (parentWindow is VerifyDigitalSignatureControl verifyControl)
- {
- dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
- {
- verifyControl.InitWithSignature(e);
- };
- }
- dialog.ShowDialog();
- }
- /// <summary>
- /// Set display settings control.
- /// </summary>
- /// <param name="displaySettingsControl"></param>
- public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
- {
- this.displaySettingsControl = displaySettingsControl;
- }
- /// <summary>
- /// Set visibility of SignatureStatusBarControl according its status.
- /// </summary>
- /// <param name="signatureStatusBarControl"></param>
- public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
- {
- this.signatureStatusBarControl = signatureStatusBarControl;
- SignatureStatusBorder.Child = this.signatureStatusBarControl;
- if (signatureStatusBarControl.Status != SignatureStatus.None)
- {
- SignatureStatusBorder.Visibility = Visibility.Visible;
- }
- else
- {
- SignatureStatusBorder.Visibility = Visibility.Collapsed;
- }
- }
-
- /// <summary>
- /// Create a signature info dialog with signature.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e">Signature to be displayed</param>
- public void ViewSignatureEvent(object sender, CPDFSignature e)
- {
- Window parentWindow = Window.GetWindow((DependencyObject)sender);
- VerifyDigitalSignatureControl dialog = new VerifyDigitalSignatureControl()
- {
- Owner = parentWindow
- };
- dialog.ViewCertificateEvent -= ViewCertificateEvent;
- dialog.ViewCertificateEvent += ViewCertificateEvent;
- dialog.InitWithSignature(e);
- dialog.ShowDialog();
- }
-
- #endregion
- #region Private Method
- /// <summary>
- /// Get current time as a string.
- /// </summary>
- /// <returns></returns>
- private string GetTime()
- {
- DateTime dateTime = DateTime.Now;
- return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
- }
- /// <summary>
- /// Create a signature field.
- /// </summary>
- private void CreateSign()
- {
- //PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
- //WidgetSignArgs signArgs = new WidgetSignArgs
- //{
- // LineWidth = 1,
- // LineColor = Colors.Black,
- // FieldName = "Signature" + GetTime()
- //};
- //PDFViewControl.PDFView.SetToolParam(signArgs);
- }
-
- /// <summary>
- /// Expand or collapse left panel.
- /// </summary>
- /// <param name="isExpand"></param>
- private void ExpandLeftPanel(bool isExpand)
- {
- BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
- Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
- if (isExpand)
- {
- BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
- BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
- }
- else
- {
- BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
- BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
- }
- }
- /// <summary>
- /// Expand or collapse right panel.
- /// </summary>
- /// <param name="propertytPanel"></param>
- /// <param name="visible"></param>
- private void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
- {
- PropertyContainer.Width = 260;
- PropertyContainer.Child = propertytPanel;
- PropertyContainer.Visibility = visible;
- }
- #endregion
- #region Private Command Event
-
- /// <summary>
- /// Click event of signature field.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
- //{
- // var signatureWidget = (e as WidgetSignArgs).Sign;
- // CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.PDFView.Document);
- // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
- // {
- // ViewSignatureEvent(sender, sig);
- // }
- // else
- // {
- // Window parentWindow = Window.GetWindow((DependencyObject)sender);
- // AddCertificationDialog addCertificationControl = new AddCertificationDialog
- // {
- // Owner = parentWindow
- // };
- // currentSignatureWidget = signatureWidget;
- // addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
- // addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
- // addCertificationControl.ShowDialog();
- // }
- //}
- /// <summary>
- /// Event of filling a signature.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
- {
- FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
- {
- FilePath = e.filePath,
- Password = e.password,
- SignatureWidget = currentSignatureWidget,
- //Document = PDFViewControl.PDFView.Document,
- Owner = Window.GetWindow(this)
- };
- fillDigitalSignatureDialog.AfterFillSignature += FillDigitalSignatureDialog_AfterFillSignature; ;
- fillDigitalSignatureDialog.ShowDialog();
- }
- /// <summary>
- /// Event of after filling a signature.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void FillDigitalSignatureDialog_AfterFillSignature(object sender, string e)
- {
- AfterFillSignature?.Invoke(this, e);
- }
- /// <summary>
- /// Click event of buttons in digital SignatureBarControl.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
- {
- if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatureField)
- {
- CreateSign();
- }
- else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
- {
- PDFViewControl.PDFToolManager.SetToolType(ToolType.Viewer);
- }
- else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
- {
- ToggleButton button = sender as ToggleButton;
- button.IsChecked = false;
- SignatureStatusChanged?.Invoke(this, null);
- }
- }
- /// <summary>
- /// Property changed event of PanelState.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
- {
- ExpandLeftPanel(panelState.IsLeftPanelExpand);
- }
- else if (e.PropertyName == nameof(PanelState.RightPanel))
- {
- if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
- {
- ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
- }
- else
- {
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- }
- }
- }
- /// <summary>
- /// Event of UndoManager property changed.
- /// </summary>
- private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- OnPropertyChanged(e.PropertyName);
- if (e.PropertyName == "CanSave")
- {
- OnCanSaveChanged?.Invoke(this, CanSave);
- }
- }
- /// <summary>
- /// Command event of undo operation.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
- {
- PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
- }
- }
- /// <summary>
- /// Command event of redo operation.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
- {
- PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
- }
- }
- /// <summary>
- /// Click event of undo button.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void UndoButton_Click(object sender, RoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
- {
- PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
- }
- }
- /// <summary>
- /// Click event of redo button.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void RedoButton_Click(object sender, RoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanRedo)
- {
- PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
- }
- }
-
- #endregion
- #region ContextMenu
-
- /// <summary>
- /// Right click context menu
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- //private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
- //{
- // if (e != null && e.CommandType == CommandType.Context)
- // {
- // if (e.CommandTarget == TargetType.WidgetView)
- // {
- // e.Handle = true;
- // e.PopupMenu = new ContextMenu();
- // var sign = e.Sign.GetSignature(PDFViewControl.PDFView.Document);
- // if (e.Sign.IsSigned() && sign != null && sign.SignerList.Any())
- // {
- // MenuItem DeleteMenu = new MenuItem()
- // { Header = "Delete" };
- // DeleteMenu.Click += (o, args) =>
- // {
- // PDFViewControl.PDFView.Document.RemoveSignature(sign, true);
- // e.Sign.ResetForm();
- // e.Sign.SetIsLocked(false);
- // PDFViewControl.PDFView.ReloadVisibleAnnots();
-
- // PDFViewControl.PDFView.UndoManager.CanSave = true;
- // SignatureStatusChanged?.Invoke(this, null);
- // };
- // e.PopupMenu.Items.Add(DeleteMenu);
- // }
- // else
- // {
- // e.PopupMenu.Items.Add(new MenuItem()
- // { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
- // e.PopupMenu.Items.Add(new MenuItem()
- // { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
- // e.PopupMenu.Items.Add(new MenuItem()
- // { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- // }
- // }
- // else
- // {
- // e.Handle = true;
- // e.PopupMenu = new ContextMenu();
- // e.PopupMenu.Items.Add(new MenuItem()
- // { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
- // e.PopupMenu.Items.Add(new Separator());
- // MenuItem fitWidthMenu = new MenuItem();
- // fitWidthMenu.Header = "Automatically Resize";
- // fitWidthMenu.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
- // }
- // };
- // e.PopupMenu.Items.Add(fitWidthMenu);
- // MenuItem fitSizeMenu = new MenuItem();
- // fitSizeMenu.Header = "Actual Size";
- // fitSizeMenu.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
- // }
- // };
- // e.PopupMenu.Items.Add(fitSizeMenu);
- // MenuItem zoomInMenu = new MenuItem();
- // zoomInMenu.Header = "Zoom In";
- // zoomInMenu.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,
- // PDFViewControl.PDFView.ZoomFactor + 0.01, true);
- // PDFViewControl.PDFView?.Zoom(newZoom);
- // }
- // };
- // e.PopupMenu.Items.Add(zoomInMenu);
- // MenuItem zoomOutMenu = new MenuItem();
- // zoomOutMenu.Header = "Zoom Out";
- // zoomOutMenu.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,
- // PDFViewControl.PDFView.ZoomFactor - 0.01, false);
- // PDFViewControl.PDFView?.Zoom(newZoom);
- // }
- // };
- // e.PopupMenu.Items.Add(zoomOutMenu);
- // e.PopupMenu.Items.Add(new Separator());
- // MenuItem singleView = new MenuItem();
- // singleView.Header = "Single Page";
- // singleView.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
- // }
- // };
- // e.PopupMenu.Items.Add(singleView);
- // MenuItem singleContinuousView = new MenuItem();
- // singleContinuousView.Header = "Single Page Continuous";
- // singleContinuousView.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
- // }
- // };
- // e.PopupMenu.Items.Add(singleContinuousView);
- // MenuItem doubleView = new MenuItem();
- // doubleView.Header = "Two Pages";
- // doubleView.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
- // }
- // };
- // e.PopupMenu.Items.Add(doubleView);
- // MenuItem doubleContinuousView = new MenuItem();
- // doubleContinuousView.Header = "Two Pages Continuous";
- // doubleContinuousView.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
- // }
- // };
- // e.PopupMenu.Items.Add(doubleContinuousView);
- // MenuItem resetFormMenu = new MenuItem();
- // resetFormMenu.Header = "Reset Forms";
- // resetFormMenu.Click += (o, p) =>
- // {
- // if (PDFViewControl != null)
- // {
- // PDFViewControl.PDFView?.ResetForm(null);
- // }
- // };
- // e.PopupMenu.Items.Add(new Separator());
- // e.PopupMenu.Items.Add(resetFormMenu);
- // }
- // }
- // else
- // {
- // e.DoCommand();
- // }
- //}
- #endregion
- #region Load Unload Event
-
- /// <summary>
- /// Load event
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
-
- }
-
- /// <summary>
- /// Unload event
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void UserControl_Unloaded(object sender, RoutedEventArgs e)
- {
- //PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
- //PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
- }
-
- #endregion
- }
- }
|