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 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);
  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. pdfViewer = passwordViewer;
  245. LoadDocument();
  246. }
  247. else
  248. {
  249. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  250. }
  251. }
  252. }
  253. /// <summary>
  254. /// Event handler for canceling password input.
  255. /// </summary>
  256. /// <param name="sender"></param>
  257. /// <param name="e"></param>
  258. private void PasswordUI_Canceled(object sender, EventArgs e)
  259. {
  260. PopupBorder.Visibility = Visibility.Collapsed;
  261. PasswordUI.Visibility = Visibility.Collapsed;
  262. }
  263. /// <summary>
  264. /// Event handler for closing the password input dialog.
  265. /// </summary>
  266. /// <param name="sender"></param>
  267. /// <param name="e"></param>
  268. private void PasswordUI_Closed(object sender, EventArgs e)
  269. {
  270. PopupBorder.Visibility = Visibility.Collapsed;
  271. PasswordUI.Visibility = Visibility.Collapsed;
  272. }
  273. #endregion
  274. #region Commands
  275. /// <summary>
  276. /// Command for the OpenFileBtn click event. Opens a PDF file.
  277. /// </summary>
  278. /// <param name="sender"></param>
  279. /// <param name="e"></param>
  280. private void OpenFile_Click(object sender, RoutedEventArgs e)
  281. {
  282. OpenFile();
  283. }
  284. /// <summary>
  285. /// Command for opening a PDF file saved with a signature.
  286. /// </summary>
  287. /// <param name="sender"></param>
  288. /// <param name="e"></param>
  289. private void DigitalSignatureControl_AfterFillSignature(object sender, string e)
  290. {
  291. OpenFile(e);
  292. }
  293. /// <summary>
  294. /// When the RightPanel state is changed, decide whether to display the ViewSettingPanel or RightToolPanel.
  295. /// </summary>
  296. /// <param name="sender"></param>
  297. /// <param name="e"></param>
  298. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  299. {
  300. if (e.PropertyName == "RightPanel")
  301. {
  302. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  303. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  304. }
  305. }
  306. /// <summary>
  307. /// Event handler for deleting a signature from the BOTA. Set the CanSave property to true and update the signature status.
  308. /// </summary>
  309. /// <param name="sender"></param>
  310. /// <param name="e"></param>
  311. private void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  312. {
  313. pdfViewer.PDFView.UndoManager.CanSave = true;
  314. DigitalSignatureControl_OnSignatureStatusChanged(sender, e);
  315. //this.CanSave = true;
  316. }
  317. /// <summary>
  318. /// Event handler for updating a signature. Update the signature status.
  319. /// </summary>
  320. /// <param name="sender"></param>
  321. /// <param name="e"></param>
  322. private void DigitalSignatureControl_OnSignatureStatusChanged(object sender, EventArgs e)
  323. {
  324. SignatureHelper.InitEffectiveSignatureList(pdfViewer.PDFView.Document);
  325. SignatureHelper.VerifySignatureList(pdfViewer.PDFView.Document);
  326. signatureStatusBarControl.SetStatus(SignatureHelper.SignatureList);
  327. botaBarControl.LoadSignatureList();
  328. }
  329. /// <summary>
  330. /// Event handler for updating the CanSave property.
  331. /// </summary>
  332. /// <param name="sender"></param>
  333. /// <param name="e"></param>
  334. private void DigitalSignatureControl_OnCanSaveChanged(object sender, bool e)
  335. {
  336. this.CanSave = e;
  337. }
  338. /// <summary>
  339. /// Event handler for the ZoomInBtn click event. Zoom in the PDF file.
  340. /// </summary>
  341. /// <param name="sender"></param>
  342. /// <param name="e"></param>
  343. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  344. {
  345. if (e.Key == "Zoom")
  346. {
  347. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  348. }
  349. }
  350. /// <summary>
  351. /// Event handler for the SaveAsFileBtn click event. Save the PDF file and set the CanSave property to false.
  352. /// </summary>
  353. /// <param name="sender"></param>
  354. /// <param name="e"></param>
  355. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  356. {
  357. SaveFile();
  358. pdfViewer.PDFView.UndoManager.CanSave = false;
  359. }
  360. /// <summary>
  361. /// Event handler for the LeftToolPanelButton click event. Expand or collapse the left panel.
  362. /// </summary>
  363. /// <param name="sender"></param>
  364. /// <param name="e"></param>
  365. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  366. {
  367. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  368. }
  369. /// <summary>
  370. /// Event handler for the ComboBox selection changed event. Switch between the Viewer and Digital Signature modes.
  371. /// </summary>
  372. /// <param name="sender"></param>
  373. /// <param name="e"></param>
  374. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  375. {
  376. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  377. if (item.Content as string == currentMode)
  378. {
  379. return;
  380. }
  381. ClearPanelState();
  382. if(ViewSettingBtn != null)
  383. ViewSettingBtn.IsChecked = false;
  384. if(RightPanelButton != null)
  385. RightPanelButton.IsChecked = false;
  386. if (currentMode == "Viewer")
  387. {
  388. regularViewerControl.ClearViewerControl();
  389. }
  390. else if (currentMode == "Digital Signature")
  391. {
  392. digitalSignatureControl.ClearViewerControl();
  393. }
  394. if (item.Content as string == "Viewer")
  395. {
  396. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFView != null)
  397. {
  398. PDFGrid.Child = regularViewerControl;
  399. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  400. regularViewerControl.PdfViewControl = pdfViewer;
  401. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  402. regularViewerControl.SetBOTAContainer(botaBarControl);
  403. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  404. regularViewerControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  405. }
  406. }
  407. else if (item.Content as string == "Digital Signature")
  408. {
  409. if (digitalSignatureControl.PDFViewControl != null && digitalSignatureControl.PDFViewControl.PDFView != null)
  410. {
  411. PDFGrid.Child = digitalSignatureControl;
  412. digitalSignatureControl.PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  413. digitalSignatureControl.PDFViewControl = pdfViewer;
  414. digitalSignatureControl.InitWithPDFViewer(pdfViewer.PDFView);
  415. digitalSignatureControl.SetBOTAContainer(botaBarControl);
  416. digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
  417. digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  418. }
  419. }
  420. currentMode = item.Content as string;
  421. }
  422. /// <summary>
  423. /// Event handler for ViewSignatureBtn click event. Expand the bota control and select the Signature tool.
  424. /// </summary>
  425. /// <param name="sender"></param>
  426. /// <param name="e"></param>
  427. private void ViewAllSignatures(object sender, EventArgs e)
  428. {
  429. LeftToolPanelButton.IsChecked = true;
  430. botaBarControl.SelectBotaTool(BOTATools.Signature);
  431. }
  432. /// <summary>
  433. /// Event handler for the SearchBtn click event. Expand the bota control and select the Search tool.
  434. /// </summary>
  435. /// <param name="sender"></param>
  436. /// <param name="e"></param>
  437. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  438. {
  439. LeftToolPanelButton.IsChecked = true;
  440. botaBarControl.SelectBotaTool(BOTATools.Search);
  441. }
  442. /// <summary>
  443. /// Event handler for the RightPanelButton click event. Expand or collapse the right panel.
  444. /// </summary>
  445. /// <param name="sender"></param>
  446. /// <param name="e"></param>
  447. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  448. {
  449. panelState.RightPanel =
  450. ((sender as ToggleButton).IsChecked == true) ?
  451. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  452. }
  453. /// <summary>
  454. /// Event handler for the ViewSettingBtn click event. Expand or collapse the view setting panel.
  455. /// </summary>
  456. /// <param name="sender"></param>
  457. /// <param name="e"></param>
  458. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  459. {
  460. panelState.RightPanel =
  461. ((sender as ToggleButton).IsChecked == true) ?
  462. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  463. }
  464. /// <summary>
  465. /// Close all the expanded panels.
  466. /// </summary>
  467. private void ClearPanelState()
  468. {
  469. LeftToolPanelButtonIsChecked = false;
  470. ViewSettingBtnIsChecked = false;
  471. RightToolPanelButtonIsChecked = false;
  472. }
  473. /// <summary>
  474. /// Event handler for the PageInfoBtn click event. Expand the file info window.
  475. /// </summary>
  476. /// <param name="sender"></param>
  477. /// <param name="e"></param>
  478. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  479. {
  480. PasswordUI.Visibility = Visibility.Collapsed;
  481. FileInfoUI.Visibility = Visibility.Visible;
  482. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  483. PopupBorder.Visibility = Visibility.Visible;
  484. }
  485. /// <summary>
  486. /// Event handler for the CloseFileInfoBtn click event. Collapse the file info window.
  487. /// </summary>
  488. /// <param name="sender"></param>
  489. /// <param name="e"></param>
  490. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  491. {
  492. PopupBorder.Visibility = Visibility.Collapsed;
  493. }
  494. /// <summary>
  495. /// Event handler for the control loaded event. Load the default PDF file.
  496. /// </summary>
  497. /// <param name="sender"></param>
  498. /// <param name="e"></param>
  499. private void Window_Loaded(object sender, RoutedEventArgs e)
  500. {
  501. pdfViewer = new PDFViewControl();
  502. LoadDefaultDocument();
  503. }
  504. /// <summary>
  505. /// Event handler for the control unloaded event.
  506. /// </summary>
  507. /// <param name="sender"></param>
  508. /// <param name="e"></param>
  509. private void Window_Unloaded(object sender, RoutedEventArgs e)
  510. {
  511. }
  512. /// <summary>
  513. /// Event handler for closing the window. Prompt to save the changes.
  514. /// </summary>
  515. /// <param name="sender"></param>
  516. /// <param name="e"></param>
  517. private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  518. {
  519. if(CanSave)
  520. {
  521. MessageBoxResult result = MessageBox.Show("Do you want to save the changes to the document?",
  522. "Save", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  523. if (result == MessageBoxResult.Yes)
  524. {
  525. SaveFile();
  526. }
  527. else if (result == MessageBoxResult.Cancel)
  528. {
  529. e.Cancel = true;
  530. }
  531. }
  532. }
  533. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  534. {
  535. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  536. }
  537. #endregion
  538. #region Save file
  539. /// <summary>
  540. /// Save the file to another PDF file.
  541. /// </summary>
  542. public void SaveAsFile()
  543. {
  544. {
  545. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  546. {
  547. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  548. SaveFileDialog saveDialog = new SaveFileDialog();
  549. saveDialog.Filter = "(*.pdf)|*.pdf";
  550. saveDialog.DefaultExt = ".pdf";
  551. saveDialog.OverwritePrompt = true;
  552. if (saveDialog.ShowDialog() == true)
  553. {
  554. pdfDoc.WriteToFilePath(saveDialog.FileName);
  555. }
  556. }
  557. }
  558. }
  559. /// <summary>
  560. /// Save the file in the current path.
  561. /// </summary>
  562. public void SaveFile()
  563. {
  564. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  565. {
  566. try
  567. {
  568. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  569. if (pdfDoc.WriteToLoadedPath())
  570. {
  571. return;
  572. }
  573. SaveFileDialog saveDialog = new SaveFileDialog();
  574. saveDialog.Filter = "(*.pdf)|*.pdf";
  575. saveDialog.DefaultExt = ".pdf";
  576. saveDialog.OverwritePrompt = true;
  577. if (saveDialog.ShowDialog() == true)
  578. {
  579. pdfDoc.WriteToFilePath(saveDialog.FileName);
  580. }
  581. }
  582. catch (Exception ex)
  583. {
  584. }
  585. }
  586. }
  587. #endregion
  588. }
  589. }