MainWindow.xaml.cs 25 KB

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