MainWindow.xaml.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. using System.Reflection;
  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 = false;
  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.PDFView.Document.FilePath;
  126. if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
  127. {
  128. if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  129. {
  130. if (oldFilePath.ToLower() == filePath.ToLower())
  131. {
  132. return;
  133. }
  134. }
  135. passwordViewer = new PDFViewControl();
  136. passwordViewer.PDFView.InitDocument(filePath);
  137. if (passwordViewer.PDFView.Document == null)
  138. {
  139. MessageBox.Show("Open File Failed");
  140. return;
  141. }
  142. if (passwordViewer.PDFView.Document.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.PDFView.Document.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.PDFView.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.PDFView);
  173. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  174. regularViewerControl.SetBOTAContainer(null);
  175. regularViewerControl.SetBOTAContainer(botaBarControl);
  176. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  177. PDFGrid.Child = regularViewerControl;
  178. SignatureHelper.InitEffectiveSignatureList(pdfViewer.PDFView.Document);
  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.PDFView.Document);
  186. digitalSignatureControl.LoadUndoManagerEvent(pdfViewer.PDFView);
  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.PDFView.Document == null)
  196. {
  197. return;
  198. }
  199. pdfViewer.PDFView.Load();
  200. pdfViewer.PDFView.SetShowLink(true);
  201. pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  202. pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  203. pdfViewer.PDFView.SetFormFieldHighlight(true);
  204. pdfViewer.CustomSignHandle = true;
  205. PasswordUI.Closed -= PasswordUI_Closed;
  206. PasswordUI.Canceled -= PasswordUI_Canceled;
  207. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  208. PasswordUI.Closed += PasswordUI_Closed;
  209. PasswordUI.Canceled += PasswordUI_Canceled;
  210. PasswordUI.Confirmed += PasswordUI_Confirmed;
  211. ModeComboBox.SelectedIndex = 0;
  212. botaBarControl.InitWithPDFViewer(pdfViewer.PDFView, pdfViewer.Password);
  213. botaBarControl.AddBOTAContent(new []{BOTATools.Thumbnail , BOTATools.Outline , BOTATools.Bookmark , BOTATools.Search , BOTATools.Annotation , BOTATools.Signature});
  214. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  215. botaBarControl.DeleteSignatureEvent -= BotaControlOnDeleteSignatureEvent;
  216. botaBarControl.DeleteSignatureEvent += BotaControlOnDeleteSignatureEvent;
  217. botaBarControl.ViewCertificateEvent -= digitalSignatureControl.ViewCertificateEvent;
  218. botaBarControl.ViewCertificateEvent += digitalSignatureControl.ViewCertificateEvent;
  219. botaBarControl.ViewSignatureEvent -= digitalSignatureControl.ViewSignatureEvent;
  220. botaBarControl.ViewSignatureEvent += digitalSignatureControl.ViewSignatureEvent;
  221. displaySettingsControl.InitWithPDFViewer(pdfViewer.PDFView);
  222. LoadCustomControl();
  223. pdfViewer.PDFView.ChangeFitMode(FitMode.FitWidth);
  224. CPDFSaclingControl.InitWithPDFViewer(pdfViewer.PDFView);
  225. panelState.PropertyChanged -= PanelState_PropertyChanged;
  226. panelState.PropertyChanged += PanelState_PropertyChanged;
  227. }
  228. /// <summary>
  229. /// Event handler for confirming the password. Open the PDF file if the password is correct.
  230. /// </summary>
  231. /// <param name="sender"></param>
  232. /// <param name="e"></param>
  233. private void PasswordUI_Confirmed(object sender, string e)
  234. {
  235. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  236. {
  237. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  238. if (passwordViewer.PDFView.Document.IsLocked == false)
  239. {
  240. PasswordUI.SetShowError("", Visibility.Collapsed);
  241. PasswordUI.ClearPassword();
  242. PasswordUI.Visibility = Visibility.Collapsed;
  243. PopupBorder.Visibility = Visibility.Collapsed;
  244. passwordViewer.Password = e;
  245. pdfViewer = passwordViewer;
  246. LoadDocument();
  247. }
  248. else
  249. {
  250. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// Event handler for canceling password input.
  256. /// </summary>
  257. /// <param name="sender"></param>
  258. /// <param name="e"></param>
  259. private void PasswordUI_Canceled(object sender, EventArgs e)
  260. {
  261. PopupBorder.Visibility = Visibility.Collapsed;
  262. PasswordUI.Visibility = Visibility.Collapsed;
  263. }
  264. /// <summary>
  265. /// Event handler for closing the password input dialog.
  266. /// </summary>
  267. /// <param name="sender"></param>
  268. /// <param name="e"></param>
  269. private void PasswordUI_Closed(object sender, EventArgs e)
  270. {
  271. PopupBorder.Visibility = Visibility.Collapsed;
  272. PasswordUI.Visibility = Visibility.Collapsed;
  273. }
  274. #endregion
  275. #region Commands
  276. /// <summary>
  277. /// Command for the OpenFileBtn click event. Opens a PDF file.
  278. /// </summary>
  279. /// <param name="sender"></param>
  280. /// <param name="e"></param>
  281. private void OpenFile_Click(object sender, RoutedEventArgs e)
  282. {
  283. OpenFile();
  284. }
  285. /// <summary>
  286. /// Command for opening a PDF file saved with a signature.
  287. /// </summary>
  288. /// <param name="sender"></param>
  289. /// <param name="e"></param>
  290. private void DigitalSignatureControl_AfterFillSignature(object sender, string e)
  291. {
  292. OpenFile(e);
  293. }
  294. /// <summary>
  295. /// When the RightPanel state is changed, decide whether to display the ViewSettingPanel or RightToolPanel.
  296. /// </summary>
  297. /// <param name="sender"></param>
  298. /// <param name="e"></param>
  299. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  300. {
  301. if (e.PropertyName == "RightPanel")
  302. {
  303. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  304. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  305. }
  306. }
  307. /// <summary>
  308. /// Event handler for deleting a signature from the BOTA. Set the CanSave property to true and update the signature status.
  309. /// </summary>
  310. /// <param name="sender"></param>
  311. /// <param name="e"></param>
  312. private void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  313. {
  314. pdfViewer.PDFView.UndoManager.CanSave = true;
  315. DigitalSignatureControl_OnSignatureStatusChanged(sender, e);
  316. //this.CanSave = true;
  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.PDFView.Document);
  326. SignatureHelper.VerifySignatureList(pdfViewer.PDFView.Document);
  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.PDFView.UndoManager.CanSave = 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 && regularViewerControl.PdfViewControl.PDFView != null)
  398. {
  399. PDFGrid.Child = regularViewerControl;
  400. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  401. regularViewerControl.PdfViewControl = pdfViewer;
  402. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  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 && digitalSignatureControl.PDFViewControl.PDFView != null)
  411. {
  412. PDFGrid.Child = digitalSignatureControl;
  413. digitalSignatureControl.PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  414. digitalSignatureControl.PDFViewControl = pdfViewer;
  415. digitalSignatureControl.InitWithPDFViewer(pdfViewer.PDFView);
  416. digitalSignatureControl.SetBOTAContainer(botaBarControl);
  417. digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
  418. digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  419. }
  420. }
  421. currentMode = item.Content as string;
  422. }
  423. /// <summary>
  424. /// Event handler for ViewSignatureBtn click event. Expand the bota control and select the Signature tool.
  425. /// </summary>
  426. /// <param name="sender"></param>
  427. /// <param name="e"></param>
  428. private void ViewAllSignatures(object sender, EventArgs e)
  429. {
  430. LeftToolPanelButton.IsChecked = true;
  431. botaBarControl.SelectBotaTool(BOTATools.Signature);
  432. }
  433. /// <summary>
  434. /// Event handler for the SearchBtn click event. Expand the bota control and select the Search tool.
  435. /// </summary>
  436. /// <param name="sender"></param>
  437. /// <param name="e"></param>
  438. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  439. {
  440. LeftToolPanelButton.IsChecked = true;
  441. botaBarControl.SelectBotaTool(BOTATools.Search);
  442. }
  443. /// <summary>
  444. /// Event handler for the RightPanelButton click event. Expand or collapse the right panel.
  445. /// </summary>
  446. /// <param name="sender"></param>
  447. /// <param name="e"></param>
  448. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  449. {
  450. panelState.RightPanel =
  451. ((sender as ToggleButton).IsChecked == true) ?
  452. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  453. }
  454. /// <summary>
  455. /// Event handler for the ViewSettingBtn click event. Expand or collapse the view setting panel.
  456. /// </summary>
  457. /// <param name="sender"></param>
  458. /// <param name="e"></param>
  459. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  460. {
  461. panelState.RightPanel =
  462. ((sender as ToggleButton).IsChecked == true) ?
  463. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  464. }
  465. /// <summary>
  466. /// Close all the expanded panels.
  467. /// </summary>
  468. private void ClearPanelState()
  469. {
  470. LeftToolPanelButtonIsChecked = false;
  471. ViewSettingBtnIsChecked = false;
  472. RightToolPanelButtonIsChecked = false;
  473. }
  474. /// <summary>
  475. /// Event handler for the PageInfoBtn click event. Expand the file info window.
  476. /// </summary>
  477. /// <param name="sender"></param>
  478. /// <param name="e"></param>
  479. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  480. {
  481. PasswordUI.Visibility = Visibility.Collapsed;
  482. FileInfoUI.Visibility = Visibility.Visible;
  483. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  484. PopupBorder.Visibility = Visibility.Visible;
  485. }
  486. /// <summary>
  487. /// Event handler for the CloseFileInfoBtn click event. Collapse the file info window.
  488. /// </summary>
  489. /// <param name="sender"></param>
  490. /// <param name="e"></param>
  491. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  492. {
  493. PopupBorder.Visibility = Visibility.Collapsed;
  494. }
  495. /// <summary>
  496. /// Event handler for the control loaded event. Load the default PDF file.
  497. /// </summary>
  498. /// <param name="sender"></param>
  499. /// <param name="e"></param>
  500. private void Window_Loaded(object sender, RoutedEventArgs e)
  501. {
  502. pdfViewer = new PDFViewControl();
  503. LoadDefaultDocument();
  504. }
  505. /// <summary>
  506. /// Event handler for the control unloaded event.
  507. /// </summary>
  508. /// <param name="sender"></param>
  509. /// <param name="e"></param>
  510. private void Window_Unloaded(object sender, RoutedEventArgs e)
  511. {
  512. }
  513. /// <summary>
  514. /// Event handler for closing the window. Prompt to save the changes.
  515. /// </summary>
  516. /// <param name="sender"></param>
  517. /// <param name="e"></param>
  518. private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  519. {
  520. if(CanSave)
  521. {
  522. MessageBoxResult result = MessageBox.Show("Do you want to save the changes to the document?",
  523. "Save", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  524. if (result == MessageBoxResult.Yes)
  525. {
  526. SaveFile();
  527. }
  528. else if (result == MessageBoxResult.Cancel)
  529. {
  530. e.Cancel = true;
  531. }
  532. }
  533. }
  534. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  535. {
  536. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  537. }
  538. #endregion
  539. #region Save file
  540. /// <summary>
  541. /// Save the file to another PDF file.
  542. /// </summary>
  543. public void SaveAsFile()
  544. {
  545. {
  546. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  547. {
  548. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  549. SaveFileDialog saveDialog = new SaveFileDialog();
  550. saveDialog.Filter = "(*.pdf)|*.pdf";
  551. saveDialog.DefaultExt = ".pdf";
  552. saveDialog.OverwritePrompt = true;
  553. if (saveDialog.ShowDialog() == true)
  554. {
  555. pdfDoc.WriteToFilePath(saveDialog.FileName);
  556. }
  557. }
  558. }
  559. }
  560. /// <summary>
  561. /// Save the file in the current path.
  562. /// </summary>
  563. public void SaveFile()
  564. {
  565. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  566. {
  567. try
  568. {
  569. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  570. if (pdfDoc.WriteToLoadedPath())
  571. {
  572. return;
  573. }
  574. SaveFileDialog saveDialog = new SaveFileDialog();
  575. saveDialog.Filter = "(*.pdf)|*.pdf";
  576. saveDialog.DefaultExt = ".pdf";
  577. saveDialog.OverwritePrompt = true;
  578. if (saveDialog.ShowDialog() == true)
  579. {
  580. pdfDoc.WriteToFilePath(saveDialog.FileName);
  581. }
  582. }
  583. catch (Exception ex)
  584. {
  585. }
  586. }
  587. }
  588. #endregion
  589. }
  590. }