MainWindow.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Helper;
  3. using Compdfkit_Tools.PDFControl;
  4. using Compdfkit_Tools.PDFView;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.PdfViewer;
  7. using Microsoft.Win32;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Runtime.CompilerServices;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Controls.Primitives;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. using System.Windows.Shapes;
  25. using ComPDFKit.DigitalSign;
  26. namespace DigitalSignature
  27. {
  28. /// <summary>
  29. /// MainWindow.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class MainWindow : Window, INotifyPropertyChanged
  32. {
  33. private string currentMode = "Viewer";
  34. private PDFViewControl pdfViewer;
  35. private PDFViewControl passwordViewer;
  36. private RegularViewerControl regularViewerControl = new RegularViewerControl();
  37. private DigitalSignatureControl digitalSignatureControl = new DigitalSignatureControl();
  38. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation | BOTATools.Signature);
  39. private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
  40. private SignatureStatusBarControl signatureStatusBarControl = new SignatureStatusBarControl();
  41. private List<CPDFSignature> signatures = new List<CPDFSignature>();
  42. private PanelState panelState = PanelState.GetInstance();
  43. public event EventHandler<bool> OnCanSaveChanged;
  44. public event PropertyChangedEventHandler PropertyChanged;
  45. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  46. {
  47. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  48. }
  49. private bool _canSave = false;
  50. public bool CanSave
  51. {
  52. get => _canSave;
  53. set
  54. {
  55. _canSave = value;
  56. OnPropertyChanged();
  57. }
  58. }
  59. public bool LeftToolPanelButtonIsChecked
  60. {
  61. get => panelState.IsLeftPanelExpand;
  62. set
  63. {
  64. panelState.IsLeftPanelExpand = value;
  65. OnPropertyChanged();
  66. }
  67. }
  68. public bool RightToolPanelButtonIsChecked
  69. {
  70. get
  71. {
  72. return (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel);
  73. }
  74. set
  75. {
  76. panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  77. OnPropertyChanged();
  78. }
  79. }
  80. public bool ViewSettingBtnIsChecked
  81. {
  82. get
  83. {
  84. return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
  85. }
  86. set
  87. {
  88. panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  89. OnPropertyChanged();
  90. }
  91. }
  92. public MainWindow()
  93. {
  94. InitializeComponent();
  95. DataContext = this;
  96. digitalSignatureControl.AfterFillSignature -= DigitalSignatureControl_AfterFillSignature;
  97. digitalSignatureControl.AfterFillSignature += DigitalSignatureControl_AfterFillSignature;
  98. }
  99. private void DigitalSignatureControl_AfterFillSignature(object sender, string e)
  100. {
  101. OpenFile(e);
  102. }
  103. #region Load Document
  104. private void OpenFile(string filePath = "")
  105. {
  106. if (string.IsNullOrEmpty(filePath))
  107. {
  108. filePath = CommonHelper.GetExistedPathOrEmpty();
  109. }
  110. string oldFilePath = pdfViewer.PDFView.Document.FilePath;
  111. if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
  112. {
  113. if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  114. {
  115. if (oldFilePath.ToLower() == filePath.ToLower())
  116. {
  117. return;
  118. }
  119. }
  120. passwordViewer = new PDFViewControl();
  121. passwordViewer.PDFView.InitDocument(filePath);
  122. if (passwordViewer.PDFView.Document == null)
  123. {
  124. MessageBox.Show("Open File Failed");
  125. return;
  126. }
  127. if (passwordViewer.PDFView.Document.IsLocked)
  128. {
  129. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  130. PasswordUI.ClearPassword();
  131. PopupBorder.Visibility = Visibility.Visible;
  132. PasswordUI.Visibility = Visibility.Visible;
  133. }
  134. else
  135. {
  136. pdfViewer = passwordViewer;
  137. LoadDocument();
  138. }
  139. }
  140. }
  141. private void LoadDefaultDocument()
  142. {
  143. string defaultFilePath = "PDF32000_2008.pdf";
  144. pdfViewer.PDFView.InitDocument(defaultFilePath);
  145. LoadDocument();
  146. }
  147. private void LoadCustomControl()
  148. {
  149. signatures = SignatureHelper.GetEffectiveSignatureList(pdfViewer.PDFView.Document.GetSignatureList());
  150. VerifySignatures();
  151. regularViewerControl.PdfViewControl = pdfViewer;
  152. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  153. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  154. regularViewerControl.SetBOTAContainer(null);
  155. regularViewerControl.SetBOTAContainer(botaBarControl);
  156. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  157. PDFGrid.Child = regularViewerControl;
  158. signatureStatusBarControl.SetStatus(signatures);
  159. regularViewerControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  160. signatureStatusBarControl.OnViewSignatureButtonClicked -= ViewAllSignatures;
  161. signatureStatusBarControl.OnViewSignatureButtonClicked += ViewAllSignatures;
  162. digitalSignatureControl.OnCanSaveChanged -= DigitalSignatureControl_OnCanSaveChanged;
  163. digitalSignatureControl.OnCanSaveChanged += DigitalSignatureControl_OnCanSaveChanged;
  164. digitalSignatureControl.SignatureStatusChanged -= DigitalSignatureControl_OnSignatureStatusChanged;
  165. digitalSignatureControl.SignatureStatusChanged += DigitalSignatureControl_OnSignatureStatusChanged;
  166. }
  167. private void LoadDocument()
  168. {
  169. if (pdfViewer.PDFView.Document == null)
  170. {
  171. return;
  172. }
  173. pdfViewer.PDFView.Load();
  174. pdfViewer.PDFView.SetShowLink(true);
  175. pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  176. pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  177. pdfViewer.PDFView.SetFormFieldHighlight(true);
  178. pdfViewer.CustomSignHandle = true;
  179. PasswordUI.Closed -= PasswordUI_Closed;
  180. PasswordUI.Canceled -= PasswordUI_Canceled;
  181. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  182. PasswordUI.Closed += PasswordUI_Closed;
  183. PasswordUI.Canceled += PasswordUI_Canceled;
  184. PasswordUI.Confirmed += PasswordUI_Confirmed;
  185. ModeComboBox.SelectedIndex = 0;
  186. botaBarControl.InitWithPDFViewer(pdfViewer.PDFView);
  187. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  188. botaBarControl.DeleteSignatureEvent -= BotaControlOnDeleteSignatureEvent;
  189. botaBarControl.DeleteSignatureEvent += BotaControlOnDeleteSignatureEvent;
  190. botaBarControl.ViewCertificateEvent -= digitalSignatureControl.ViewCertificateEvent;
  191. botaBarControl.ViewCertificateEvent += digitalSignatureControl.ViewCertificateEvent;
  192. botaBarControl.ViewSignatureEvent -= digitalSignatureControl.ViewSignatureEvent;
  193. botaBarControl.ViewSignatureEvent += digitalSignatureControl.ViewSignatureEvent;
  194. displaySettingsControl.InitWithPDFViewer(pdfViewer.PDFView);
  195. LoadCustomControl();
  196. pdfViewer.PDFView.ChangeFitMode(FitMode.FitWidth);
  197. CPDFSaclingControl.InitWithPDFViewer(pdfViewer.PDFView);
  198. regularViewerControl.IsActive = true;
  199. digitalSignatureControl.InitWithPDFViewer(pdfViewer.PDFView);
  200. }
  201. public void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  202. {
  203. DigitalSignatureControl_OnSignatureStatusChanged(sender, e);
  204. }
  205. private void DigitalSignatureControl_OnSignatureStatusChanged(object sender, EventArgs e)
  206. {
  207. signatures = SignatureHelper.GetEffectiveSignatureList(pdfViewer.PDFView.Document.GetSignatureList());
  208. VerifySignatures();
  209. signatureStatusBarControl.SetStatus(signatures);
  210. botaBarControl.LoadSignatureList();
  211. }
  212. private void DigitalSignatureControl_OnCanSaveChanged(object sender, bool e)
  213. {
  214. this.CanSave = e;
  215. }
  216. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  217. {
  218. if (e.Key == "Zoom")
  219. {
  220. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  221. }
  222. }
  223. private void PasswordUI_Confirmed(object sender, string e)
  224. {
  225. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  226. {
  227. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  228. if (passwordViewer.PDFView.Document.IsLocked == false)
  229. {
  230. PasswordUI.SetShowError("", Visibility.Collapsed);
  231. PasswordUI.ClearPassword();
  232. PasswordUI.Visibility = Visibility.Collapsed;
  233. PopupBorder.Visibility = Visibility.Collapsed;
  234. pdfViewer = passwordViewer;
  235. LoadDocument();
  236. }
  237. else
  238. {
  239. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  240. }
  241. }
  242. }
  243. private void PasswordUI_Canceled(object sender, EventArgs e)
  244. {
  245. PopupBorder.Visibility = Visibility.Collapsed;
  246. PasswordUI.Visibility = Visibility.Collapsed;
  247. }
  248. private void PasswordUI_Closed(object sender, EventArgs e)
  249. {
  250. PopupBorder.Visibility = Visibility.Collapsed;
  251. PasswordUI.Visibility = Visibility.Collapsed;
  252. }
  253. #endregion
  254. private void OpenFile_Click(object sender, RoutedEventArgs e)
  255. {
  256. OpenFile();
  257. }
  258. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  259. {
  260. SaveFile();
  261. pdfViewer.PDFView.UndoManager.CanSave = false;
  262. }
  263. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  264. {
  265. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  266. }
  267. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  268. {
  269. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  270. if (item.Content as string == currentMode)
  271. {
  272. return;
  273. }
  274. LeftToolPanelButtonIsChecked = false;
  275. LeftToolPanelButton.IsChecked = false;
  276. ViewSettingBtnIsChecked = false;
  277. if(ViewSettingBtn != null)
  278. ViewSettingBtn.IsChecked = false;
  279. if (currentMode == "Viewer")
  280. {
  281. regularViewerControl.ClearViewerControl();
  282. regularViewerControl.IsActive = false;
  283. }
  284. else if (currentMode == "Digital Signature")
  285. {
  286. digitalSignatureControl.ClearViewerControl();
  287. digitalSignatureControl.IsActive = false;
  288. }
  289. if (item.Content as string == "Viewer")
  290. {
  291. regularViewerControl.IsActive = true;
  292. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFView != null)
  293. {
  294. PDFGrid.Child = regularViewerControl;
  295. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  296. regularViewerControl.PdfViewControl = pdfViewer;
  297. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  298. regularViewerControl.SetBOTAContainer(botaBarControl);
  299. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  300. regularViewerControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  301. }
  302. }
  303. else if (item.Content as string == "Digital Signature")
  304. {
  305. digitalSignatureControl.IsActive = true;
  306. if (digitalSignatureControl.PDFViewControl != null && digitalSignatureControl.PDFViewControl.PDFView != null)
  307. {
  308. PDFGrid.Child = digitalSignatureControl;
  309. digitalSignatureControl.PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  310. digitalSignatureControl.PDFViewControl = pdfViewer;
  311. digitalSignatureControl.InitWithPDFViewer(pdfViewer.PDFView);
  312. digitalSignatureControl.SetBOTAContainer(botaBarControl);
  313. digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
  314. digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  315. }
  316. }
  317. currentMode = item.Content as string;
  318. }
  319. private void ViewAllSignatures(object sender, EventArgs e)
  320. {
  321. LeftToolPanelButton.IsChecked = true;
  322. panelState.IsLeftPanelExpand = true;
  323. botaBarControl.SelectBotaTool(BOTATools.Signature);
  324. }
  325. private void VerifySignatures()
  326. {
  327. foreach (var sig in signatures)
  328. {
  329. sig.VerifySignatureWithDocument(pdfViewer.PDFView.Document);
  330. }
  331. }
  332. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  333. {
  334. LeftToolPanelButton.IsChecked = true;
  335. panelState.IsLeftPanelExpand = true;
  336. botaBarControl.SelectBotaTool(BOTATools.Search);
  337. }
  338. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  339. {
  340. panelState.RightPanel =
  341. ((sender as ToggleButton).IsChecked == true) ?
  342. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  343. }
  344. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  345. {
  346. panelState.RightPanel =
  347. ((sender as ToggleButton).IsChecked == true) ?
  348. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  349. }
  350. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  351. {
  352. PasswordUI.Visibility = Visibility.Collapsed;
  353. FileInfoUI.Visibility = Visibility.Visible;
  354. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  355. PopupBorder.Visibility = Visibility.Visible;
  356. }
  357. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  358. {
  359. PopupBorder.Visibility = Visibility.Collapsed;
  360. }
  361. private void Window_Loaded(object sender, RoutedEventArgs e)
  362. {
  363. pdfViewer = new PDFViewControl();
  364. LoadDefaultDocument();
  365. }
  366. private void Window_Unloaded(object sender, RoutedEventArgs e)
  367. {
  368. }
  369. #region Save file
  370. /// <summary>
  371. /// Save the file to another PDF file.
  372. /// </summary>
  373. public void SaveAsFile()
  374. {
  375. {
  376. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  377. {
  378. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  379. SaveFileDialog saveDialog = new SaveFileDialog();
  380. saveDialog.Filter = "(*.pdf)|*.pdf";
  381. saveDialog.DefaultExt = ".pdf";
  382. saveDialog.OverwritePrompt = true;
  383. if (saveDialog.ShowDialog() == true)
  384. {
  385. pdfDoc.WriteToFilePath(saveDialog.FileName);
  386. }
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// Save the file in the current path.
  392. /// </summary>
  393. public void SaveFile()
  394. {
  395. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  396. {
  397. try
  398. {
  399. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  400. if (pdfDoc.WriteToLoadedPath())
  401. {
  402. return;
  403. }
  404. SaveFileDialog saveDialog = new SaveFileDialog();
  405. saveDialog.Filter = "(*.pdf)|*.pdf";
  406. saveDialog.DefaultExt = ".pdf";
  407. saveDialog.OverwritePrompt = true;
  408. if (saveDialog.ShowDialog() == true)
  409. {
  410. pdfDoc.WriteToFilePath(saveDialog.FileName);
  411. }
  412. }
  413. catch (Exception ex)
  414. {
  415. }
  416. }
  417. }
  418. #endregion
  419. }
  420. }