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