123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- using Compdfkit_Tools.PDFControl;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using System;
- using System.ComponentModel;
- using System.IO;
- 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.DigitalSignature.VerifyDigitalSignatureControl;
- using Compdfkit_Tools.Helper;
- using ComPDFKit.DigitalSign;
- using ComPDFKit.PDFAnnotation.Form;
- namespace Compdfkit_Tools.PDFControl
- {
- public partial class DigitalSignatureControl : UserControl, INotifyPropertyChanged
- {
- private bool isFirstLoad = true;
- public PDFViewControl PDFViewControl = new PDFViewControl();
- private SignatureStatusBarControl signatureStatusBarControl;
- private PanelState panelState = PanelState.GetInstance();
- private CPDFDisplaySettingsControl displaySettingsControl = null;
- private bool _isActive = false;
- public event EventHandler<bool> OnCanSaveChanged;
- private CPDFSignatureWidget currentSignatureWidget;
- 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));
- }
- public bool IsActive
- {
- get => _isActive;
- set
- {
- _isActive = value;
- OnPropertyChanged();
- }
- }
- public bool CanUndo
- {
- get
- {
- if (PDFViewControl != null && PDFViewControl.PDFView != null)
- {
- return PDFViewControl.PDFView.UndoManager.CanUndo;
- }
- return false;
- }
- }
- public bool CanRedo
- {
- get
- {
- if (PDFViewControl != null && PDFViewControl.PDFView != null)
- {
- return PDFViewControl.PDFView.UndoManager.CanRedo;
- }
- return false;
- }
- }
- private bool CanSave
- {
- get
- {
- if (PDFViewControl != null && PDFViewControl.PDFView != null)
- {
- return PDFViewControl.PDFView.UndoManager.CanSave;
- }
- return false;
- }
- }
- public DigitalSignatureControl()
- {
- InitializeComponent();
- DataContext = this;
- string trustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
- if (!Directory.Exists(trustedFolder))
- {
- Directory.CreateDirectory(trustedFolder);
- }
- CPDFSignature.SignCertTrustedFolder = trustedFolder;
- }
- public void ClearViewerControl()
- {
- PDFGrid.Child = null;
- BotaContainer.Child = null;
- PropertyContainer.Child = null;
- SignatureStatusBorder.Child = null;
- }
- public void InitWithPDFViewer(CPDFViewer pdfViewer)
- {
- PDFViewControl.PDFView = pdfViewer;
- PDFGrid.Child = PDFViewControl;
- FloatPageTool.InitWithPDFViewer(pdfViewer);
- InitialControl();
- DigitalSignatureBarControl.DigitalSignatureActionChanged -= DigitalSignatureBarControl_DigitalSignatureActionChanged;
- DigitalSignatureBarControl.DigitalSignatureActionChanged += DigitalSignatureBarControl_DigitalSignatureActionChanged;
- PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
- PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
- PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
- PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
- panelState.PropertyChanged -= PanelState_PropertyChanged;
- panelState.PropertyChanged += PanelState_PropertyChanged;
- }
-
- private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
- {
- var signatureWidget = (e as WidgetSignArgs).Sign;
- if (signatureWidget.IsSigned())
- {
- CPDFSignature sig = signatureWidget.GetSignature();
- sig.VerifySignatureWithDocument(PDFViewControl.PDFView.Document);
- 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();
- }
- }
- private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
- {
- FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
- {
- FilePath = e.filePath,
- Password = e.password,
- SignatureWidget = currentSignatureWidget,
- Document = PDFViewControl.PDFView.Document
- };
- fillDigitalSignatureDialog.AfterFillSignature += FillDigitalSignatureDialog_AfterFillSignature; ;
- fillDigitalSignatureDialog.ShowDialog();
- }
- private void FillDigitalSignatureDialog_AfterFillSignature(object sender, string e)
- {
- AfterFillSignature?.Invoke(this, e);
- }
- 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.PDFView.SetMouseMode(MouseModes.Viewer);
- }
- else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
- {
- ToggleButton button = sender as ToggleButton;
- button.IsChecked = false;
- SignatureStatusChanged?.Invoke(this, null);
- }
- }
- private string GetTime()
- {
- DateTime dateTime = DateTime.Now;
- return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
- }
- private void CreateSign()
- {
- PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
- WidgetSignArgs signArgs = new WidgetSignArgs
- {
- LineWidth = 1,
- LineColor = Colors.Black,
- FieldName = "Signature" + GetTime()
- };
- PDFViewControl.PDFView.SetToolParam(signArgs);
- }
- private void InitialControl()
- {
- PDFViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
- //PDFViewControl.PDFView?.Load();
- PDFViewControl.PDFView?.SetShowLink(true);
- PDFGrid.Child = PDFViewControl;
- PDFViewControl.PDFView.SetFormFieldHighlight(true);
- }
- 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((IsActive) ? displaySettingsControl : null, Visibility.Visible);
- }
- else
- {
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- }
- }
- }
- public 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);
- }
- }
- public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
- {
- PropertyContainer.Width = 260;
- PropertyContainer.Child = propertytPanel;
- PropertyContainer.Visibility = visible;
- }
- public void SetBOTAContainer(CPDFBOTABarControl botaControl)
- {
- BotaContainer.Child = botaControl;
- }
- 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) =>
- {
- e.VerifySignatureWithDocument(PDFViewControl.PDFView.Document);
- SignatureStatusChanged?.Invoke(this, null);
- };
- dialog.ShowDialog();
- }
- public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
- {
- this.displaySettingsControl = displaySettingsControl;
- }
- 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>
- /// Undo Redo Event Noitfy
- /// </summary>
- private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- OnPropertyChanged(e.PropertyName);
- if (e.PropertyName == "CanSave")
- {
- OnCanSaveChanged?.Invoke(this, CanSave);
- }
- }
- private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
- {
- PDFViewControl.PDFView.UndoManager?.Undo();
- }
- }
- private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
- {
- PDFViewControl.PDFView.UndoManager?.Redo();
- }
- }
- private void UndoButton_Click(object sender, RoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
- {
- PDFViewControl.PDFView.UndoManager?.Undo();
- }
- }
- private void RedoButton_Click(object sender, RoutedEventArgs e)
- {
- if (PDFViewControl != null && PDFViewControl.PDFView != null && CanRedo)
- {
- PDFViewControl.PDFView.UndoManager?.Redo();
- }
- }
- 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();
- }
- }
- }
|