MainWindow.xaml.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Helper;
  3. using Compdfkit_Tools.PDFControl;
  4. using Compdfkit_Tools.PDFView;
  5. using ComPDFKitViewer;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Reflection;
  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. using ComPDFKit.Tool;
  27. namespace DigitalSignature
  28. {
  29. /// <summary>
  30. /// Interaction logic for MainWindow.xaml
  31. /// </summary>
  32. public partial class MainWindow : Window, INotifyPropertyChanged
  33. {
  34. private string currentMode = "Viewer";
  35. private bool isFirstLoad = true;
  36. private PDFViewControl pdfViewer;
  37. private PDFViewControl passwordViewer;
  38. private RegularViewerControl regularViewerControl = new RegularViewerControl();
  39. private DigitalSignatureControl digitalSignatureControl = new DigitalSignatureControl();
  40. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl();
  41. private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
  42. private SignatureStatusBarControl signatureStatusBarControl = new SignatureStatusBarControl();
  43. private PanelState panelState = PanelState.GetInstance();
  44. public event PropertyChangedEventHandler PropertyChanged;
  45. #region Properties
  46. /// <summary>
  47. /// Whether the save operation can be performed.
  48. /// </summary>
  49. private bool _canSave = true;
  50. public bool CanSave
  51. {
  52. get => _canSave;
  53. set
  54. {
  55. _canSave = value;
  56. OnPropertyChanged();
  57. }
  58. }
  59. /// <summary>
  60. /// Whether the left panel is expanded.
  61. /// </summary>
  62. public bool LeftToolPanelButtonIsChecked
  63. {
  64. get => panelState.IsLeftPanelExpand;
  65. set
  66. {
  67. panelState.IsLeftPanelExpand = value;
  68. OnPropertyChanged();
  69. }
  70. }
  71. /// <summary>
  72. /// Whether the right panel is expanded.
  73. /// </summary>
  74. public bool RightToolPanelButtonIsChecked
  75. {
  76. get
  77. {
  78. return (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel);
  79. }
  80. set
  81. {
  82. panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  83. OnPropertyChanged();
  84. }
  85. }
  86. /// <summary>
  87. /// Whether the view setting panel is expanded.
  88. /// </summary>
  89. public bool ViewSettingBtnIsChecked
  90. {
  91. get
  92. {
  93. return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
  94. }
  95. set
  96. {
  97. panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  98. OnPropertyChanged();
  99. }
  100. }
  101. public string AppInfo
  102. {
  103. get { return Assembly.GetExecutingAssembly().GetName().Name + " " + string.Join(".", Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(3)); }
  104. }
  105. #endregion
  106. public MainWindow()
  107. {
  108. InitializeComponent();
  109. DataContext = this;
  110. digitalSignatureControl.AfterFillSignature -= DigitalSignatureControl_AfterFillSignature;
  111. digitalSignatureControl.AfterFillSignature += DigitalSignatureControl_AfterFillSignature;
  112. Closing += MainWindow_Closing;
  113. }
  114. #region Load Document
  115. /// <summary>
  116. /// Open a PDF file.
  117. /// </summary>
  118. /// <param name="filePath">The path of the PDF file.</param>
  119. private void OpenFile(string filePath = "")
  120. {
  121. if (string.IsNullOrEmpty(filePath))
  122. {
  123. filePath = CommonHelper.GetExistedPathOrEmpty();
  124. }
  125. string oldFilePath = pdfViewer.GetCPDFViewer().GetDocument().FilePath;
  126. if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
  127. {
  128. if (pdfViewer.GetCPDFViewer() != null && pdfViewer.GetCPDFViewer().GetDocument() != null)
  129. {
  130. if (oldFilePath.ToLower() == filePath.ToLower())
  131. {
  132. return;
  133. }
  134. }
  135. passwordViewer = new PDFViewControl();
  136. passwordViewer.InitDocument(filePath);
  137. if (passwordViewer.GetCPDFViewer().GetDocument() == null)
  138. {
  139. MessageBox.Show("Open File Failed");
  140. return;
  141. }
  142. if (passwordViewer.GetCPDFViewer().GetDocument().IsLocked)
  143. {
  144. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  145. PasswordUI.ClearPassword();
  146. PopupBorder.Visibility = Visibility.Visible;
  147. PasswordUI.Visibility = Visibility.Visible;
  148. }
  149. else
  150. {
  151. pdfViewer.GetCPDFViewer().GetDocument().Release();
  152. pdfViewer = passwordViewer;
  153. LoadDocument();
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// Load the default PDF file when the application is started.
  159. /// </summary>
  160. private void LoadDefaultDocument()
  161. {
  162. string defaultFilePath = "ComPDFKit_Signatures_Sample_File.pdf";
  163. pdfViewer.InitDocument(defaultFilePath);
  164. LoadDocument();
  165. }
  166. /// <summary>
  167. /// Load the custom controls for the PDF viewer.
  168. /// </summary>
  169. private void LoadCustomControl()
  170. {
  171. regularViewerControl.PdfViewControl = pdfViewer;
  172. regularViewerControl.InitWithPDFViewer(pdfViewer);
  173. regularViewerControl.PdfViewControl.PDFViewTool.SetViewerModel(MouseModes.Viewer);
  174. regularViewerControl.SetBOTAContainer(null);
  175. regularViewerControl.SetBOTAContainer(botaBarControl);
  176. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  177. PDFGrid.Child = regularViewerControl;
  178. SignatureHelper.InitEffectiveSignatureList(pdfViewer.GetCPDFViewer().GetDocument());
  179. digitalSignatureControl.OnCanSaveChanged -= DigitalSignatureControl_OnCanSaveChanged;
  180. digitalSignatureControl.OnCanSaveChanged += DigitalSignatureControl_OnCanSaveChanged;
  181. digitalSignatureControl.SignatureStatusChanged -= DigitalSignatureControl_OnSignatureStatusChanged;
  182. digitalSignatureControl.SignatureStatusChanged += DigitalSignatureControl_OnSignatureStatusChanged;
  183. signatureStatusBarControl.OnViewSignatureButtonClicked -= ViewAllSignatures;
  184. signatureStatusBarControl.OnViewSignatureButtonClicked += ViewAllSignatures;
  185. SignatureHelper.VerifySignatureList(pdfViewer.GetCPDFViewer().GetDocument());
  186. digitalSignatureControl.LoadUndoManagerEvent(pdfViewer.GetCPDFViewer());
  187. signatureStatusBarControl.SetStatus(SignatureHelper.SignatureList);
  188. regularViewerControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  189. }
  190. /// <summary>
  191. /// Load current PDF file and initialize event handlers.
  192. /// </summary>
  193. private void LoadDocument()
  194. {
  195. if (pdfViewer.GetCPDFViewer().GetDocument() == null)
  196. {
  197. return;
  198. }
  199. // pdfViewer.PDFView.Load();
  200. // pdfViewer.PDFView.SetShowLink(true);
  201. //
  202. // pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  203. // pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  204. //
  205. // pdfViewer.PDFView.SetFormFieldHighlight(true);
  206. pdfViewer.CustomSignHandle = true;
  207. PasswordUI.Closed -= PasswordUI_Closed;
  208. PasswordUI.Canceled -= PasswordUI_Canceled;
  209. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  210. PasswordUI.Closed += PasswordUI_Closed;
  211. PasswordUI.Canceled += PasswordUI_Canceled;
  212. PasswordUI.Confirmed += PasswordUI_Confirmed;
  213. ModeComboBox.SelectedIndex = 0;
  214. botaBarControl.InitWithPDFViewer(pdfViewer);
  215. botaBarControl.AddBOTAContent(new []{BOTATools.Thumbnail , BOTATools.Outline , BOTATools.Bookmark , BOTATools.Search , BOTATools.Annotation , BOTATools.Signature});
  216. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  217. botaBarControl.DeleteSignatureEvent -= BotaControlOnDeleteSignatureEvent;
  218. botaBarControl.DeleteSignatureEvent += BotaControlOnDeleteSignatureEvent;
  219. botaBarControl.ViewCertificateEvent -= digitalSignatureControl.ViewCertificateEvent;
  220. botaBarControl.ViewCertificateEvent += digitalSignatureControl.ViewCertificateEvent;
  221. botaBarControl.ViewSignatureEvent -= digitalSignatureControl.ViewSignatureEvent;
  222. botaBarControl.ViewSignatureEvent += digitalSignatureControl.ViewSignatureEvent;
  223. displaySettingsControl.InitWithPDFViewer(pdfViewer);
  224. LoadCustomControl();
  225. pdfViewer.GetCPDFViewer().SetFitMode(FitMode.FitWidth);
  226. CPDFSaclingControl.InitWithPDFViewer(pdfViewer);
  227. panelState.PropertyChanged -= PanelState_PropertyChanged;
  228. panelState.PropertyChanged += PanelState_PropertyChanged;
  229. }
  230. /// <summary>
  231. /// Event handler for confirming the password. Open the PDF file if the password is correct.
  232. /// </summary>
  233. /// <param name="sender"></param>
  234. /// <param name="e"></param>
  235. private void PasswordUI_Confirmed(object sender, string e)
  236. {
  237. if (passwordViewer != null && passwordViewer.GetCPDFViewer() != null && passwordViewer.GetCPDFViewer().GetDocument() != null)
  238. {
  239. passwordViewer.GetCPDFViewer().GetDocument().UnlockWithPassword(e);
  240. if (passwordViewer.GetCPDFViewer().GetDocument().IsLocked == false)
  241. {
  242. PasswordUI.SetShowError("", Visibility.Collapsed);
  243. PasswordUI.ClearPassword();
  244. PasswordUI.Visibility = Visibility.Collapsed;
  245. PopupBorder.Visibility = Visibility.Collapsed;
  246. pdfViewer = passwordViewer;
  247. LoadDocument();
  248. }
  249. else
  250. {
  251. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// Event handler for canceling password input.
  257. /// </summary>
  258. /// <param name="sender"></param>
  259. /// <param name="e"></param>
  260. private void PasswordUI_Canceled(object sender, EventArgs e)
  261. {
  262. PopupBorder.Visibility = Visibility.Collapsed;
  263. PasswordUI.Visibility = Visibility.Collapsed;
  264. }
  265. /// <summary>
  266. /// Event handler for closing the password input dialog.
  267. /// </summary>
  268. /// <param name="sender"></param>
  269. /// <param name="e"></param>
  270. private void PasswordUI_Closed(object sender, EventArgs e)
  271. {
  272. PopupBorder.Visibility = Visibility.Collapsed;
  273. PasswordUI.Visibility = Visibility.Collapsed;
  274. }
  275. #endregion
  276. #region Commands
  277. /// <summary>
  278. /// Command for the OpenFileBtn click event. Opens a PDF file.
  279. /// </summary>
  280. /// <param name="sender"></param>
  281. /// <param name="e"></param>
  282. private void OpenFile_Click(object sender, RoutedEventArgs e)
  283. {
  284. OpenFile();
  285. }
  286. /// <summary>
  287. /// Command for opening a PDF file saved with a signature.
  288. /// </summary>
  289. /// <param name="sender"></param>
  290. /// <param name="e"></param>
  291. private void DigitalSignatureControl_AfterFillSignature(object sender, string e)
  292. {
  293. OpenFile(e);
  294. }
  295. /// <summary>
  296. /// When the RightPanel state is changed, decide whether to display the ViewSettingPanel or RightToolPanel.
  297. /// </summary>
  298. /// <param name="sender"></param>
  299. /// <param name="e"></param>
  300. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  301. {
  302. if (e.PropertyName == "RightPanel")
  303. {
  304. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  305. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  306. }
  307. }
  308. /// <summary>
  309. /// Event handler for deleting a signature from the BOTA. Set the CanSave property to true and update the signature status.
  310. /// </summary>
  311. /// <param name="sender"></param>
  312. /// <param name="e"></param>
  313. private void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  314. {
  315. pdfViewer.PDFViewTool.IsDocumentModified = true;
  316. DigitalSignatureControl_OnSignatureStatusChanged(sender, e);
  317. }
  318. /// <summary>
  319. /// Event handler for updating a signature. Update the signature status.
  320. /// </summary>
  321. /// <param name="sender"></param>
  322. /// <param name="e"></param>
  323. private void DigitalSignatureControl_OnSignatureStatusChanged(object sender, EventArgs e)
  324. {
  325. SignatureHelper.InitEffectiveSignatureList(pdfViewer.GetCPDFViewer().GetDocument());
  326. SignatureHelper.VerifySignatureList(pdfViewer.GetCPDFViewer().GetDocument());
  327. signatureStatusBarControl.SetStatus(SignatureHelper.SignatureList);
  328. botaBarControl.LoadSignatureList();
  329. }
  330. /// <summary>
  331. /// Event handler for updating the CanSave property.
  332. /// </summary>
  333. /// <param name="sender"></param>
  334. /// <param name="e"></param>
  335. private void DigitalSignatureControl_OnCanSaveChanged(object sender, bool e)
  336. {
  337. this.CanSave = e;
  338. }
  339. /// <summary>
  340. /// Event handler for the ZoomInBtn click event. Zoom in the PDF file.
  341. /// </summary>
  342. /// <param name="sender"></param>
  343. /// <param name="e"></param>
  344. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  345. {
  346. if (e.Key == "Zoom")
  347. {
  348. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  349. }
  350. }
  351. /// <summary>
  352. /// Event handler for the SaveAsFileBtn click event. Save the PDF file and set the CanSave property to false.
  353. /// </summary>
  354. /// <param name="sender"></param>
  355. /// <param name="e"></param>
  356. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  357. {
  358. SaveFile();
  359. pdfViewer.PDFViewTool.IsDocumentModified = false;
  360. }
  361. /// <summary>
  362. /// Event handler for the LeftToolPanelButton click event. Expand or collapse the left panel.
  363. /// </summary>
  364. /// <param name="sender"></param>
  365. /// <param name="e"></param>
  366. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  367. {
  368. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  369. }
  370. /// <summary>
  371. /// Event handler for the ComboBox selection changed event. Switch between the Viewer and Digital Signature modes.
  372. /// </summary>
  373. /// <param name="sender"></param>
  374. /// <param name="e"></param>
  375. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  376. {
  377. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  378. if (item.Content as string == currentMode)
  379. {
  380. return;
  381. }
  382. ClearPanelState();
  383. if(ViewSettingBtn != null)
  384. ViewSettingBtn.IsChecked = false;
  385. if(RightPanelButton != null)
  386. RightPanelButton.IsChecked = false;
  387. if (currentMode == "Viewer")
  388. {
  389. regularViewerControl.ClearViewerControl();
  390. }
  391. else if (currentMode == "Digital Signature")
  392. {
  393. digitalSignatureControl.ClearViewerControl();
  394. }
  395. if (item.Content as string == "Viewer")
  396. {
  397. if (regularViewerControl.PdfViewControl != null)
  398. {
  399. PDFGrid.Child = regularViewerControl;
  400. regularViewerControl.PdfViewControl.PDFViewTool.SetViewerModel(MouseModes.Viewer);
  401. regularViewerControl.PdfViewControl = pdfViewer;
  402. regularViewerControl.InitWithPDFViewer(pdfViewer);
  403. regularViewerControl.SetBOTAContainer(botaBarControl);
  404. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  405. regularViewerControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  406. }
  407. }
  408. else if (item.Content as string == "Digital Signature")
  409. {
  410. if (digitalSignatureControl.PDFViewControl != null)
  411. {
  412. PDFGrid.Child = digitalSignatureControl;
  413. digitalSignatureControl.PDFViewControl.PDFViewTool.SetViewerModel(MouseModes.WidgetEditTool);
  414. digitalSignatureControl.PDFViewControl = pdfViewer;
  415. digitalSignatureControl.InitWithPDFViewer(pdfViewer);
  416. digitalSignatureControl.SetBOTAContainer(botaBarControl);
  417. digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
  418. digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  419. //pdfViewer.PDFViewTool.SetViewerModel(MouseModes.PanTool);
  420. }
  421. }
  422. currentMode = item.Content as string;
  423. }
  424. /// <summary>
  425. /// Event handler for ViewSignatureBtn click event. Expand the bota control and select the Signature tool.
  426. /// </summary>
  427. /// <param name="sender"></param>
  428. /// <param name="e"></param>
  429. private void ViewAllSignatures(object sender, EventArgs e)
  430. {
  431. LeftToolPanelButton.IsChecked = true;
  432. botaBarControl.SelectBotaTool(BOTATools.Signature);
  433. }
  434. /// <summary>
  435. /// Event handler for the SearchBtn click event. Expand the bota control and select the Search tool.
  436. /// </summary>
  437. /// <param name="sender"></param>
  438. /// <param name="e"></param>
  439. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  440. {
  441. LeftToolPanelButton.IsChecked = true;
  442. botaBarControl.SelectBotaTool(BOTATools.Search);
  443. }
  444. /// <summary>
  445. /// Event handler for the RightPanelButton click event. Expand or collapse the right panel.
  446. /// </summary>
  447. /// <param name="sender"></param>
  448. /// <param name="e"></param>
  449. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  450. {
  451. panelState.RightPanel =
  452. ((sender as ToggleButton).IsChecked == true) ?
  453. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  454. }
  455. /// <summary>
  456. /// Event handler for the ViewSettingBtn click event. Expand or collapse the view setting panel.
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  461. {
  462. panelState.RightPanel =
  463. ((sender as ToggleButton).IsChecked == true) ?
  464. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  465. }
  466. /// <summary>
  467. /// Close all the expanded panels.
  468. /// </summary>
  469. private void ClearPanelState()
  470. {
  471. LeftToolPanelButtonIsChecked = false;
  472. ViewSettingBtnIsChecked = false;
  473. RightToolPanelButtonIsChecked = false;
  474. }
  475. /// <summary>
  476. /// Event handler for the PageInfoBtn click event. Expand the file info window.
  477. /// </summary>
  478. /// <param name="sender"></param>
  479. /// <param name="e"></param>
  480. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  481. {
  482. PasswordUI.Visibility = Visibility.Collapsed;
  483. FileInfoUI.Visibility = Visibility.Visible;
  484. FileInfoControl.InitWithPDFViewer(pdfViewer);
  485. PopupBorder.Visibility = Visibility.Visible;
  486. }
  487. /// <summary>
  488. /// Event handler for the CloseFileInfoBtn click event. Collapse the file info window.
  489. /// </summary>
  490. /// <param name="sender"></param>
  491. /// <param name="e"></param>
  492. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  493. {
  494. PopupBorder.Visibility = Visibility.Collapsed;
  495. }
  496. /// <summary>
  497. /// Event handler for the control loaded event. Load the default PDF file.
  498. /// </summary>
  499. /// <param name="sender"></param>
  500. /// <param name="e"></param>
  501. private void Window_Loaded(object sender, RoutedEventArgs e)
  502. {
  503. pdfViewer = new PDFViewControl();
  504. LoadDefaultDocument();
  505. }
  506. /// <summary>
  507. /// Event handler for the control unloaded event.
  508. /// </summary>
  509. /// <param name="sender"></param>
  510. /// <param name="e"></param>
  511. private void Window_Unloaded(object sender, RoutedEventArgs e)
  512. {
  513. }
  514. /// <summary>
  515. /// Event handler for closing the window. Prompt to save the changes.
  516. /// </summary>
  517. /// <param name="sender"></param>
  518. /// <param name="e"></param>
  519. private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  520. {
  521. if(CanSave)
  522. {
  523. MessageBoxResult result = MessageBox.Show("Do you want to save the changes to the document?",
  524. "Save", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  525. if (result == MessageBoxResult.Yes)
  526. {
  527. SaveFile();
  528. }
  529. else if (result == MessageBoxResult.Cancel)
  530. {
  531. e.Cancel = true;
  532. }
  533. }
  534. }
  535. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  536. {
  537. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  538. }
  539. #endregion
  540. #region Save file
  541. /// <summary>
  542. /// Save the file to another PDF file.
  543. /// </summary>
  544. public void SaveAsFile()
  545. {
  546. {
  547. if (pdfViewer != null && pdfViewer.GetCPDFViewer() != null && pdfViewer.GetCPDFViewer().GetDocument() != null)
  548. {
  549. CPDFDocument pdfDoc = pdfViewer.GetCPDFViewer().GetDocument();
  550. SaveFileDialog saveDialog = new SaveFileDialog();
  551. saveDialog.Filter = "(*.pdf)|*.pdf";
  552. saveDialog.DefaultExt = ".pdf";
  553. saveDialog.OverwritePrompt = true;
  554. if (saveDialog.ShowDialog() == true)
  555. {
  556. pdfDoc.WriteToFilePath(saveDialog.FileName);
  557. }
  558. }
  559. }
  560. }
  561. /// <summary>
  562. /// Save the file in the current path.
  563. /// </summary>
  564. public void SaveFile()
  565. {
  566. if (pdfViewer != null && pdfViewer.GetCPDFViewer() != null && pdfViewer.GetCPDFViewer().GetDocument() != null)
  567. {
  568. try
  569. {
  570. CPDFDocument pdfDoc = pdfViewer.GetCPDFViewer().GetDocument();
  571. if (pdfDoc.WriteToLoadedPath())
  572. {
  573. return;
  574. }
  575. SaveFileDialog saveDialog = new SaveFileDialog();
  576. saveDialog.Filter = "(*.pdf)|*.pdf";
  577. saveDialog.DefaultExt = ".pdf";
  578. saveDialog.OverwritePrompt = true;
  579. if (saveDialog.ShowDialog() == true)
  580. {
  581. pdfDoc.WriteToFilePath(saveDialog.FileName);
  582. }
  583. }
  584. catch (Exception ex)
  585. {
  586. }
  587. }
  588. }
  589. #endregion
  590. }
  591. }