MainWindow.xaml.cs 25 KB

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