using ComPDFKit.PDFDocument;
using Compdfkit_Tools.Helper;
using Compdfkit_Tools.PDFControl;
using Compdfkit_Tools.PDFView;
using ComPDFKitViewer;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ComPDFKit.DigitalSign;
namespace DigitalSignature
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string currentMode = "Viewer";
private PDFViewControl pdfViewer;
private PDFViewControl passwordViewer;
private RegularViewerControl regularViewerControl = new RegularViewerControl();
private DigitalSignatureControl digitalSignatureControl = new DigitalSignatureControl();
private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation | BOTATools.Signature);
private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
private SignatureStatusBarControl signatureStatusBarControl = new SignatureStatusBarControl();
private List signatures = new List();
private PanelState panelState = PanelState.GetInstance();
public event EventHandler OnCanSaveChanged;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool _canSave = false;
public bool CanSave
{
get => _canSave;
set
{
_canSave = value;
OnPropertyChanged();
}
}
public bool LeftToolPanelButtonIsChecked
{
get => panelState.IsLeftPanelExpand;
set
{
panelState.IsLeftPanelExpand = value;
OnPropertyChanged();
}
}
public bool RightToolPanelButtonIsChecked
{
get
{
return (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel);
}
set
{
panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
OnPropertyChanged();
}
}
public bool ViewSettingBtnIsChecked
{
get
{
return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
}
set
{
panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
digitalSignatureControl.AfterFillSignature -= DigitalSignatureControl_AfterFillSignature;
digitalSignatureControl.AfterFillSignature += DigitalSignatureControl_AfterFillSignature;
}
private void DigitalSignatureControl_AfterFillSignature(object sender, string e)
{
OpenFile(e);
}
#region Load Document
private void OpenFile(string filePath = "")
{
if (string.IsNullOrEmpty(filePath))
{
filePath = CommonHelper.GetExistedPathOrEmpty();
}
string oldFilePath = pdfViewer.PDFView.Document.FilePath;
if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
{
if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
{
if (oldFilePath.ToLower() == filePath.ToLower())
{
return;
}
}
passwordViewer = new PDFViewControl();
passwordViewer.PDFView.InitDocument(filePath);
if (passwordViewer.PDFView.Document == null)
{
MessageBox.Show("Open File Failed");
return;
}
if (passwordViewer.PDFView.Document.IsLocked)
{
PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
PasswordUI.ClearPassword();
PopupBorder.Visibility = Visibility.Visible;
PasswordUI.Visibility = Visibility.Visible;
}
else
{
pdfViewer = passwordViewer;
LoadDocument();
}
}
}
private void LoadDefaultDocument()
{
string defaultFilePath = "PDF32000_2008.pdf";
pdfViewer.PDFView.InitDocument(defaultFilePath);
LoadDocument();
}
private void LoadCustomControl()
{
signatures = SignatureHelper.GetEffectiveSignatureList(pdfViewer.PDFView.Document.GetSignatureList());
VerifySignatures();
regularViewerControl.PdfViewControl = pdfViewer;
regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
regularViewerControl.SetBOTAContainer(null);
regularViewerControl.SetBOTAContainer(botaBarControl);
regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
PDFGrid.Child = regularViewerControl;
signatureStatusBarControl.SetStatus(signatures);
regularViewerControl.SetSignatureStatusBarControl(signatureStatusBarControl);
signatureStatusBarControl.OnViewSignatureButtonClicked -= ViewAllSignatures;
signatureStatusBarControl.OnViewSignatureButtonClicked += ViewAllSignatures;
digitalSignatureControl.OnCanSaveChanged -= DigitalSignatureControl_OnCanSaveChanged;
digitalSignatureControl.OnCanSaveChanged += DigitalSignatureControl_OnCanSaveChanged;
digitalSignatureControl.SignatureStatusChanged -= DigitalSignatureControl_OnSignatureStatusChanged;
digitalSignatureControl.SignatureStatusChanged += DigitalSignatureControl_OnSignatureStatusChanged;
}
private void LoadDocument()
{
if (pdfViewer.PDFView.Document == null)
{
return;
}
pdfViewer.PDFView.Load();
pdfViewer.PDFView.SetShowLink(true);
pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
pdfViewer.PDFView.SetFormFieldHighlight(true);
pdfViewer.CustomSignHandle = true;
PasswordUI.Closed -= PasswordUI_Closed;
PasswordUI.Canceled -= PasswordUI_Canceled;
PasswordUI.Confirmed -= PasswordUI_Confirmed;
PasswordUI.Closed += PasswordUI_Closed;
PasswordUI.Canceled += PasswordUI_Canceled;
PasswordUI.Confirmed += PasswordUI_Confirmed;
ModeComboBox.SelectedIndex = 0;
botaBarControl.InitWithPDFViewer(pdfViewer.PDFView);
botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
botaBarControl.DeleteSignatureEvent -= BotaControlOnDeleteSignatureEvent;
botaBarControl.DeleteSignatureEvent += BotaControlOnDeleteSignatureEvent;
botaBarControl.ViewCertificateEvent -= digitalSignatureControl.ViewCertificateEvent;
botaBarControl.ViewCertificateEvent += digitalSignatureControl.ViewCertificateEvent;
botaBarControl.ViewSignatureEvent -= digitalSignatureControl.ViewSignatureEvent;
botaBarControl.ViewSignatureEvent += digitalSignatureControl.ViewSignatureEvent;
displaySettingsControl.InitWithPDFViewer(pdfViewer.PDFView);
LoadCustomControl();
pdfViewer.PDFView.ChangeFitMode(FitMode.FitWidth);
CPDFSaclingControl.InitWithPDFViewer(pdfViewer.PDFView);
regularViewerControl.IsActive = true;
digitalSignatureControl.InitWithPDFViewer(pdfViewer.PDFView);
}
public void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
{
DigitalSignatureControl_OnSignatureStatusChanged(sender, e);
}
private void DigitalSignatureControl_OnSignatureStatusChanged(object sender, EventArgs e)
{
signatures = SignatureHelper.GetEffectiveSignatureList(pdfViewer.PDFView.Document.GetSignatureList());
VerifySignatures();
signatureStatusBarControl.SetStatus(signatures);
botaBarControl.LoadSignatureList();
}
private void DigitalSignatureControl_OnCanSaveChanged(object sender, bool e)
{
this.CanSave = e;
}
private void PdfViewer_InfoChanged(object sender, KeyValuePair e)
{
if (e.Key == "Zoom")
{
CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
}
}
private void PasswordUI_Confirmed(object sender, string e)
{
if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
{
passwordViewer.PDFView.Document.UnlockWithPassword(e);
if (passwordViewer.PDFView.Document.IsLocked == false)
{
PasswordUI.SetShowError("", Visibility.Collapsed);
PasswordUI.ClearPassword();
PasswordUI.Visibility = Visibility.Collapsed;
PopupBorder.Visibility = Visibility.Collapsed;
pdfViewer = passwordViewer;
LoadDocument();
}
else
{
PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
}
}
}
private void PasswordUI_Canceled(object sender, EventArgs e)
{
PopupBorder.Visibility = Visibility.Collapsed;
PasswordUI.Visibility = Visibility.Collapsed;
}
private void PasswordUI_Closed(object sender, EventArgs e)
{
PopupBorder.Visibility = Visibility.Collapsed;
PasswordUI.Visibility = Visibility.Collapsed;
}
#endregion
private void OpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFile();
}
private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
{
SaveFile();
pdfViewer.PDFView.UndoManager.CanSave = false;
}
private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
{
panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
if (item.Content as string == currentMode)
{
return;
}
LeftToolPanelButtonIsChecked = false;
LeftToolPanelButton.IsChecked = false;
ViewSettingBtnIsChecked = false;
if(ViewSettingBtn != null)
ViewSettingBtn.IsChecked = false;
if (currentMode == "Viewer")
{
regularViewerControl.ClearViewerControl();
regularViewerControl.IsActive = false;
}
else if (currentMode == "Digital Signature")
{
digitalSignatureControl.ClearViewerControl();
digitalSignatureControl.IsActive = false;
}
if (item.Content as string == "Viewer")
{
regularViewerControl.IsActive = true;
if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFView != null)
{
PDFGrid.Child = regularViewerControl;
regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
regularViewerControl.PdfViewControl = pdfViewer;
regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
regularViewerControl.SetBOTAContainer(botaBarControl);
regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
regularViewerControl.SetSignatureStatusBarControl(signatureStatusBarControl);
}
}
else if (item.Content as string == "Digital Signature")
{
digitalSignatureControl.IsActive = true;
if (digitalSignatureControl.PDFViewControl != null && digitalSignatureControl.PDFViewControl.PDFView != null)
{
PDFGrid.Child = digitalSignatureControl;
digitalSignatureControl.PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
digitalSignatureControl.PDFViewControl = pdfViewer;
digitalSignatureControl.InitWithPDFViewer(pdfViewer.PDFView);
digitalSignatureControl.SetBOTAContainer(botaBarControl);
digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
}
}
currentMode = item.Content as string;
}
private void ViewAllSignatures(object sender, EventArgs e)
{
LeftToolPanelButton.IsChecked = true;
panelState.IsLeftPanelExpand = true;
botaBarControl.SelectBotaTool(BOTATools.Signature);
}
private void VerifySignatures()
{
foreach (var sig in signatures)
{
sig.VerifySignatureWithDocument(pdfViewer.PDFView.Document);
}
}
private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
{
LeftToolPanelButton.IsChecked = true;
panelState.IsLeftPanelExpand = true;
botaBarControl.SelectBotaTool(BOTATools.Search);
}
private void RightPanelButton_Click(object sender, RoutedEventArgs e)
{
panelState.RightPanel =
((sender as ToggleButton).IsChecked == true) ?
PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
}
private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
{
panelState.RightPanel =
((sender as ToggleButton).IsChecked == true) ?
PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
}
private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
{
PasswordUI.Visibility = Visibility.Collapsed;
FileInfoUI.Visibility = Visibility.Visible;
FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
PopupBorder.Visibility = Visibility.Visible;
}
private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
{
PopupBorder.Visibility = Visibility.Collapsed;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
pdfViewer = new PDFViewControl();
LoadDefaultDocument();
}
private void Window_Unloaded(object sender, RoutedEventArgs e)
{
}
#region Save file
///
/// Save the file to another PDF file.
///
public void SaveAsFile()
{
{
if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
{
CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "(*.pdf)|*.pdf";
saveDialog.DefaultExt = ".pdf";
saveDialog.OverwritePrompt = true;
if (saveDialog.ShowDialog() == true)
{
pdfDoc.WriteToFilePath(saveDialog.FileName);
}
}
}
}
///
/// Save the file in the current path.
///
public void SaveFile()
{
if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
{
try
{
CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
if (pdfDoc.WriteToLoadedPath())
{
return;
}
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "(*.pdf)|*.pdf";
saveDialog.DefaultExt = ".pdf";
saveDialog.OverwritePrompt = true;
if (saveDialog.ShowDialog() == true)
{
pdfDoc.WriteToFilePath(saveDialog.FileName);
}
}
catch (Exception ex)
{
}
}
}
#endregion
}
}