MainPage.xaml.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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.Runtime.CompilerServices;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Controls.Primitives;
  14. using System.Windows.Input;
  15. namespace PDFViewer
  16. {
  17. public partial class MainPage : UserControl, INotifyPropertyChanged
  18. {
  19. #region Properties
  20. private string currentMode = "Viewer";
  21. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  22. private PDFViewControl viewControl;
  23. private PDFViewControl passwordViewer;
  24. private RegularViewerControl regularViewerControl = new RegularViewerControl();
  25. private AnnotationControl annotationControl = new AnnotationControl();
  26. private FormControl formControl = new FormControl();
  27. private ContentEditControl contentEditControl = new ContentEditControl();
  28. private PageEditControl pageEditControl = new PageEditControl();
  29. private DigitalSignatureControl digitalSignatureControl = new DigitalSignatureControl();
  30. private SignatureStatusBarControl signatureStatusBarControl = new SignatureStatusBarControl();
  31. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl();
  32. private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
  33. private PanelState panelState = PanelState.GetInstance();
  34. public event EventHandler FileChangeEvent;
  35. public event Func<string[], bool> CheckExistBeforeOpenFileEvent;
  36. public event EventHandler<string> AfterSaveAsFileEvent;
  37. public event PropertyChangedEventHandler PropertyChanged;
  38. private bool _canSave = true;
  39. /// <summary>
  40. /// Whether the save operation can be performed.
  41. /// </summary>
  42. public bool CanSave
  43. {
  44. get => _canSave;
  45. set
  46. {
  47. _canSave = value;
  48. OnPropertyChanged();
  49. }
  50. }
  51. public PDFViewControl GetPDFViewControl()
  52. {
  53. return viewControl;
  54. }
  55. /// <summary>
  56. /// Whether the left panel is expanded.
  57. /// </summary>
  58. public bool LeftToolPanelButtonIsChecked
  59. {
  60. get => panelState.IsLeftPanelExpand;
  61. set
  62. {
  63. panelState.IsLeftPanelExpand = value;
  64. OnPropertyChanged();
  65. }
  66. }
  67. /// <summary>
  68. /// Whether the right panel is expanded.
  69. /// </summary>
  70. public bool RightToolPanelButtonIsChecked
  71. {
  72. get
  73. {
  74. return (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel);
  75. }
  76. set
  77. {
  78. panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  79. OnPropertyChanged();
  80. }
  81. }
  82. /// <summary>
  83. /// Whether the view setting panel is expanded.
  84. /// </summary>
  85. public bool ViewSettingBtnIsChecked
  86. {
  87. get
  88. {
  89. return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
  90. }
  91. set
  92. {
  93. panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  94. OnPropertyChanged();
  95. }
  96. }
  97. private Visibility _notDocsEditorVisible = Visibility.Visible;
  98. /// <summary>
  99. /// Whether the Document Editor panel is visible.
  100. /// </summary>
  101. public Visibility NotDocsEditorVisible
  102. {
  103. get => _notDocsEditorVisible;
  104. set
  105. {
  106. _notDocsEditorVisible = value;
  107. OnPropertyChanged();
  108. }
  109. }
  110. public MainPage()
  111. {
  112. InitializeComponent();
  113. this.DataContext = this;
  114. }
  115. #endregion
  116. #region Load document
  117. /// <summary>
  118. /// Initialize the PDFViewer and load the PDF file.
  119. /// </summary>
  120. /// <param name="filePath"></param>
  121. public void InitWithFilePath(string filePath)
  122. {
  123. viewControl = new PDFViewControl();
  124. viewControl.InitDocument(filePath);
  125. }
  126. public void InitWithDocument(CPDFDocument document)
  127. {
  128. viewControl = new PDFViewControl();
  129. viewControl.GetCPDFViewer().InitDoc(document);
  130. }
  131. /// <summary>
  132. /// Init controls and set events.
  133. /// </summary>
  134. private void LoadDocument()
  135. {
  136. if (viewControl != null && viewControl.PDFViewTool != null)
  137. {
  138. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  139. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  140. if (pdfDoc == null)
  141. {
  142. return;
  143. }
  144. SizeChanged -= MainPage_SizeChanged;
  145. SizeChanged += MainPage_SizeChanged;
  146. //viewControl.PDFView.Load();
  147. //viewControl.PDFView.SetShowLink(true);
  148. //viewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  149. //viewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  150. //viewControl.PDFView.SetFormFieldHighlight(true);
  151. PasswordUI.Closed -= PasswordUI_Closed;
  152. PasswordUI.Canceled -= PasswordUI_Canceled;
  153. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  154. PasswordUI.Closed += PasswordUI_Closed;
  155. PasswordUI.Canceled += PasswordUI_Canceled;
  156. PasswordUI.Confirmed += PasswordUI_Confirmed;
  157. //viewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  158. CPDFSaclingControl.InitWithPDFViewer(viewControl);
  159. ModeComboBox.SelectedIndex = 0;
  160. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfviewer.GetZoom() * 100)));
  161. botaBarControl.AddBOTAContent(new[] { BOTATools.Thumbnail, BOTATools.Outline, BOTATools.Bookmark, BOTATools.Annotation, BOTATools.Search });
  162. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  163. ViewSettingBtn.IsChecked = false;
  164. botaBarControl.InitWithPDFViewer(viewControl);
  165. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  166. displaySettingsControl.InitWithPDFViewer(viewControl);
  167. LoadCustomControl();
  168. panelState.PropertyChanged -= PanelState_PropertyChanged;
  169. panelState.PropertyChanged += PanelState_PropertyChanged;
  170. displaySettingsControl.SplitModeChanged -= DisplaySettingsControl_SplitModeChanged;
  171. displaySettingsControl.SplitModeChanged += DisplaySettingsControl_SplitModeChanged;
  172. }
  173. }
  174. private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
  175. {
  176. viewControl.WindowSizeChange();
  177. }
  178. private void DisplaySettingsControl_SplitModeChanged(object sender, Compdfkit_Tools.PDFControlUI.CPDFViewModeUI.SplitMode e)
  179. {
  180. viewControl.SetSplitViewMode(e);
  181. }
  182. /// <summary>
  183. /// When the RightPanel state is changed, decide whether to display the ViewSettingPanel or RightToolPanel.
  184. /// </summary>
  185. /// <param name="sender"></param>
  186. /// <param name="e"></param>
  187. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  188. {
  189. if (e.PropertyName == "RightPanel")
  190. {
  191. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  192. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  193. }
  194. }
  195. public void SetFeatureMode(string featureName)
  196. {
  197. if (!string.IsNullOrEmpty(featureName))
  198. {
  199. switch (featureName)
  200. {
  201. case "Viewer":
  202. ModeComboBox.SelectedIndex = 0;
  203. break;
  204. case "Annotations":
  205. ModeComboBox.SelectedIndex = 1;
  206. break;
  207. case "Forms":
  208. ModeComboBox.SelectedIndex = 2;
  209. break;
  210. case "Signatures":
  211. ModeComboBox.SelectedIndex = 5;
  212. break;
  213. case "Document Editor":
  214. ModeComboBox.SelectedIndex = 4;
  215. break;
  216. case "Content Editor":
  217. ModeComboBox.SelectedIndex = 3;
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. }
  224. internal void SetPDFViewer(PDFViewControl newPdfViewer)
  225. {
  226. if (newPdfViewer != null)
  227. {
  228. viewControl = newPdfViewer;
  229. }
  230. }
  231. #endregion
  232. #region Password
  233. /// <summary>
  234. /// Event handler for confirming the password. Open the PDF file if the password is correct.
  235. /// </summary>
  236. /// <param name="sender"></param>
  237. /// <param name="e"></param>
  238. private void PasswordUI_Confirmed(object sender, string e)
  239. {
  240. if (passwordViewer != null && passwordViewer.PDFViewTool != null)
  241. {
  242. CPDFViewer pdfviewer = passwordViewer.PDFViewTool.GetCPDFViewer();
  243. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  244. if (pdfDoc == null)
  245. {
  246. return;
  247. }
  248. pdfDoc.UnlockWithPassword(e);
  249. if (pdfDoc.IsLocked == false)
  250. {
  251. PasswordUI.SetShowError("", Visibility.Collapsed);
  252. PasswordUI.ClearPassword();
  253. PasswordUI.Visibility = Visibility.Collapsed;
  254. PopupBorder.Visibility = Visibility.Collapsed;
  255. viewControl = passwordViewer;
  256. LoadDocument();
  257. viewControl.PDFViewTool.GetCPDFViewer().UpdateVirtualNodes();
  258. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  259. }
  260. else
  261. {
  262. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  263. }
  264. }
  265. }
  266. /// <summary>
  267. /// Event handler for canceling password input.
  268. /// </summary>
  269. /// <param name="sender"></param>
  270. /// <param name="e"></param>
  271. private void PasswordUI_Canceled(object sender, EventArgs e)
  272. {
  273. PopupBorder.Visibility = Visibility.Collapsed;
  274. PasswordUI.Visibility = Visibility.Collapsed;
  275. }
  276. /// <summary>
  277. /// Event handler for closing the password input dialog.
  278. /// </summary>
  279. /// <param name="sender"></param>
  280. /// <param name="e"></param>
  281. private void PasswordUI_Closed(object sender, EventArgs e)
  282. {
  283. PopupBorder.Visibility = Visibility.Collapsed;
  284. PasswordUI.Visibility = Visibility.Collapsed;
  285. }
  286. #endregion
  287. #region Load custom control
  288. /// <summary>
  289. /// Load the custom controls for the PDF viewer.
  290. /// </summary>
  291. private void LoadCustomControl()
  292. {
  293. regularViewerControl.PdfViewControl = viewControl;
  294. regularViewerControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  295. regularViewerControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  296. regularViewerControl.InitWithPDFViewer(viewControl);
  297. //regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  298. regularViewerControl.SetBOTAContainer(null);
  299. regularViewerControl.SetBOTAContainer(botaBarControl);
  300. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  301. PDFGrid.Child = regularViewerControl;
  302. SignatureHelper.InitEffectiveSignatureList(viewControl.GetCPDFViewer().GetDocument());
  303. SignatureHelper.VerifySignatureList(viewControl.GetCPDFViewer().GetDocument());
  304. digitalSignatureControl.LoadUndoManagerEvent(viewControl.GetCPDFViewer());
  305. signatureStatusBarControl.SetStatus(SignatureHelper.SignatureList);
  306. }
  307. /// <summary>
  308. /// Event handler for the Loaded event of the MainPage.
  309. /// </summary>
  310. /// <param name="sender"></param>
  311. /// <param name="e"></param>
  312. private void MainPage_Loaded(object sender, RoutedEventArgs e)
  313. {
  314. LoadDocument();
  315. }
  316. #endregion
  317. #region Event handle
  318. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  319. {
  320. if (e.Key == "Zoom")
  321. {
  322. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  323. }
  324. }
  325. /// <summary>
  326. /// Command for opening a PDF file saved with a signature.
  327. /// </summary>
  328. /// <param name="sender"></param>
  329. /// <param name="e"></param>
  330. private void DigitalSignatureControl_AfterFillSignature(object sender, string e)
  331. {
  332. OpenFile(e);
  333. }
  334. /// <summary>
  335. /// Event handler for deleting a signature from the BOTA. Set the CanSave property to true and update the signature status.
  336. /// </summary>
  337. /// <param name="sender"></param>
  338. /// <param name="e"></param>
  339. private void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  340. {
  341. DigitalSignatureControl_OnSignatureStatusChanged(sender, e);
  342. }
  343. /// <summary>
  344. /// Event handler for updating a signature. Update the signature status.
  345. /// </summary>
  346. /// <param name="sender"></param>
  347. /// <param name="e"></param>
  348. private void DigitalSignatureControl_OnSignatureStatusChanged(object sender, EventArgs e)
  349. {
  350. SignatureHelper.InitEffectiveSignatureList(viewControl.GetCPDFViewer().GetDocument());
  351. SignatureHelper.VerifySignatureList(viewControl.GetCPDFViewer().GetDocument());
  352. signatureStatusBarControl.SetStatus(SignatureHelper.SignatureList);
  353. botaBarControl.LoadSignatureList();
  354. }
  355. /// <summary>
  356. /// Event handler for updating the CanSave property.
  357. /// </summary>
  358. /// <param name="sender"></param>
  359. /// <param name="e"></param>
  360. private void DigitalSignatureControl_OnCanSaveChanged(object sender, bool e)
  361. {
  362. this.CanSave = e;
  363. }
  364. #endregion
  365. #region Private Command Event
  366. /// <summary>
  367. /// Close all the expanded panels.
  368. /// </summary>
  369. private void ClearPanelState()
  370. {
  371. LeftToolPanelButtonIsChecked = false;
  372. ViewSettingBtnIsChecked = false;
  373. RightToolPanelButtonIsChecked = false;
  374. }
  375. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  376. {
  377. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  378. }
  379. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  380. {
  381. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  382. if ((string)item.Tag == currentMode)
  383. {
  384. return;
  385. }
  386. ClearPanelState();
  387. if (ViewSettingBtn != null)
  388. ViewSettingBtn.IsChecked = false;
  389. if (RightPanelButton != null)
  390. RightPanelButton.IsChecked = false;
  391. if (viewControl != null && viewControl.GetCPDFViewer() != null)
  392. {
  393. // pdfViewer.PDFView.ToolManager.EnableClickCreate = false;
  394. }
  395. if (currentMode == "Viewer")
  396. {
  397. regularViewerControl.ClearViewerControl();
  398. }
  399. else if (currentMode == "Annotation")
  400. {
  401. annotationControl.UnloadEvent();
  402. annotationControl.ClearViewerControl();
  403. viewControl.SetIsShowStampMouse(false);
  404. }
  405. else if (currentMode == "Form")
  406. {
  407. formControl.UnloadEvent();
  408. formControl.ClearViewerControl();
  409. viewControl.SetIsShowStampMouse(false);
  410. }
  411. else if (currentMode == "Content Editor")
  412. {
  413. displaySettingsControl.SetVisibilityWhenContentEdit(Visibility.Visible);
  414. contentEditControl.ClearViewerControl();
  415. contentEditControl.ClearPDFEditState();
  416. }
  417. else if (currentMode == "Document Editor")
  418. {
  419. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  420. NotDocsEditorVisible = Visibility.Visible;
  421. botaBarControl.LoadThumbnail();
  422. }
  423. else if (currentMode == "Digital Signature")
  424. {
  425. RightPanelButton.Visibility = Visibility.Visible;
  426. digitalSignatureControl.ClearViewerControl();
  427. botaBarControl.RemoveBOTAContent(BOTATools.Signature);
  428. }
  429. if (item.Tag as string == "Viewer")
  430. {
  431. regularViewerControl.PdfViewControl = viewControl;
  432. regularViewerControl.InitWithPDFViewer(viewControl);
  433. if (regularViewerControl.PdfViewControl != null)
  434. {
  435. PDFGrid.Child = regularViewerControl;
  436. viewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
  437. //viewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  438. regularViewerControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  439. regularViewerControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  440. regularViewerControl.SetBOTAContainer(botaBarControl);
  441. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  442. }
  443. }
  444. else if (item.Tag as string == "Annotation")
  445. {
  446. annotationControl.SetToolBarContainerVisibility(Visibility.Visible);
  447. PDFGrid.Child = annotationControl;
  448. viewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.CreateAnnot);
  449. //viewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  450. //annotationControl.PDFViewControl.PDFView.SetToolParam(new AnnotHandlerEventArgs());
  451. annotationControl.PDFViewControl = viewControl;
  452. annotationControl.InitWithPDFViewer(viewControl);
  453. if (annotationControl.PDFViewControl != null)
  454. {
  455. annotationControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  456. annotationControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  457. annotationControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  458. annotationControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  459. annotationControl.InitialPDFViewControl(annotationControl.PDFViewControl);
  460. annotationControl.SetBOTAContainer(botaBarControl);
  461. annotationControl.SetDisplaySettingsControl(displaySettingsControl);
  462. }
  463. }
  464. else if (item.Tag as string == "Form")
  465. {
  466. formControl.SetToolBarContainerVisibility(Visibility.Visible);
  467. formControl.PdfViewControl = viewControl;
  468. formControl.InitWithPDFViewer(viewControl);
  469. if (formControl.PdfViewControl != null )
  470. {
  471. PDFGrid.Child = formControl;
  472. viewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.FormEdit);
  473. formControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  474. formControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  475. formControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  476. formControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  477. formControl.SetBOTAContainer(botaBarControl);
  478. formControl.InitialPDFViewControl(formControl.PdfViewControl);
  479. formControl.SetDisplaySettingsControl(displaySettingsControl);
  480. }
  481. }
  482. else if (item.Tag as string == "Content Editor")
  483. {
  484. contentEditControl.PdfViewControl = viewControl;
  485. contentEditControl.InitWithPDFViewer(viewControl);
  486. displaySettingsControl.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  487. if (contentEditControl.pdfContentEditControl != null && contentEditControl.PdfViewControl!= null)
  488. {
  489. //viewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  490. //viewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  491. //viewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  492. //viewControl.PDFView?.ReloadDocument();
  493. PDFGrid.Child = contentEditControl;
  494. viewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.ContentEdit);
  495. contentEditControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  496. contentEditControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  497. contentEditControl.SetBOTAContainer(botaBarControl);
  498. contentEditControl.SetDisplaySettingsControl(displaySettingsControl);
  499. }
  500. }
  501. else if (item.Tag as string == "Document Editor")
  502. {
  503. pageEditControl.PDFViewControl = viewControl;
  504. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit;
  505. PDFGrid.Child = pageEditControl;
  506. NotDocsEditorVisible = Visibility.Collapsed;
  507. }
  508. else if (item.Tag as string == "Digital Signature")
  509. {
  510. if (contentEditControl.pdfContentEditControl != null && contentEditControl.PdfViewControl != null)
  511. {
  512. RightPanelButton.Visibility = Visibility.Collapsed;
  513. PDFGrid.Child = digitalSignatureControl;
  514. digitalSignatureControl.PDFViewControl.PDFToolManager.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
  515. digitalSignatureControl.PDFViewControl = viewControl;
  516. botaBarControl.AddBOTAContent(BOTATools.Signature);
  517. digitalSignatureControl.InitWithPDFViewer(viewControl);
  518. digitalSignatureControl.SetBOTAContainer(botaBarControl);
  519. digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
  520. digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  521. signatureStatusBarControl.OnViewSignatureButtonClicked -= ViewAllSignatures;
  522. signatureStatusBarControl.OnViewSignatureButtonClicked += ViewAllSignatures;
  523. digitalSignatureControl.OnCanSaveChanged -= DigitalSignatureControl_OnCanSaveChanged;
  524. digitalSignatureControl.OnCanSaveChanged += DigitalSignatureControl_OnCanSaveChanged;
  525. digitalSignatureControl.SignatureStatusChanged -= DigitalSignatureControl_OnSignatureStatusChanged;
  526. digitalSignatureControl.SignatureStatusChanged += DigitalSignatureControl_OnSignatureStatusChanged;
  527. digitalSignatureControl.AfterFillSignature -= DigitalSignatureControl_AfterFillSignature;
  528. digitalSignatureControl.AfterFillSignature += DigitalSignatureControl_AfterFillSignature;
  529. }
  530. }
  531. currentMode = item.Tag as string;
  532. RightToolPanelButtonIsChecked = false;
  533. }
  534. /// <summary>
  535. /// Event handler for ViewSignatureBtn click event. Expand the bota control and select the Signature tool.
  536. /// </summary>
  537. /// <param name="sender"></param>
  538. /// <param name="e"></param>
  539. private void ViewAllSignatures(object sender, EventArgs e)
  540. {
  541. LeftToolPanelButton.IsChecked = true;
  542. botaBarControl.SelectBotaTool(BOTATools.Signature);
  543. }
  544. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  545. {
  546. ModeComboBox.SelectedIndex = 0;
  547. }
  548. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  549. {
  550. PasswordUI.Visibility = Visibility.Collapsed;
  551. FileInfoUI.Visibility = Visibility.Visible;
  552. FileInfoControl.InitWithPDFViewer(viewControl);
  553. PopupBorder.Visibility = Visibility.Visible;
  554. }
  555. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  556. {
  557. panelState.RightPanel =
  558. ((sender as ToggleButton).IsChecked == true) ?
  559. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  560. }
  561. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  562. {
  563. panelState.RightPanel =
  564. ((sender as ToggleButton).IsChecked == true) ?
  565. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  566. }
  567. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  568. {
  569. LeftToolPanelButton.IsChecked = true;
  570. botaBarControl.SelectBotaTool(BOTATools.Search);
  571. }
  572. private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e)
  573. {
  574. CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent;
  575. CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent; ;
  576. CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent;
  577. CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent; ;
  578. CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent;
  579. CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent; ;
  580. }
  581. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  582. {
  583. SaveFile();
  584. }
  585. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  586. {
  587. SaveAsFile();
  588. }
  589. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  590. {
  591. OpenFile();
  592. }
  593. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  594. {
  595. PopupBorder.Visibility = Visibility.Collapsed;
  596. }
  597. /// <summary>
  598. /// Refresh the annotation list when a annotation is edited.
  599. /// </summary>
  600. /// <param name="sender"></param>
  601. /// <param name="e"></param>
  602. private void PdfFormControlRefreshAnnotList(object sender, EventArgs e)
  603. {
  604. botaBarControl.LoadAnnotationList();
  605. }
  606. /// <summary>
  607. /// When a CanSave property of a control is changed, the CanSave property of current page is changed.
  608. /// </summary>
  609. /// <param name="sender"></param>
  610. /// <param name="e"></param>
  611. private void ControlOnCanSaveChanged(object sender, bool e)
  612. {
  613. this.CanSave = e;
  614. }
  615. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  616. {
  617. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  618. }
  619. #endregion
  620. #region Open and Save file
  621. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  622. {
  623. SaveFile();
  624. }
  625. private void OpenFile(string filePath = "")
  626. {
  627. if (viewControl != null && viewControl.PDFViewTool != null)
  628. {
  629. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  630. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  631. if (pdfDoc == null)
  632. {
  633. return;
  634. }
  635. if (string.IsNullOrEmpty(filePath))
  636. {
  637. filePath = CommonHelper.GetExistedPathOrEmpty();
  638. }
  639. string oldFilePath = pdfDoc.FilePath;
  640. if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
  641. {
  642. if (oldFilePath.ToLower() == filePath.ToLower())
  643. {
  644. return;
  645. }
  646. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  647. {
  648. return;
  649. }
  650. passwordViewer = new PDFViewControl();
  651. passwordViewer.InitDocument(filePath);
  652. CPDFViewer tempViewer = passwordViewer.PDFViewTool?.GetCPDFViewer();
  653. CPDFDocument tempDoc = tempViewer?.GetDocument();
  654. if (tempDoc == null)
  655. {
  656. MessageBox.Show("Open File Failed");
  657. return;
  658. }
  659. if (passwordViewer.GetCPDFViewer().GetDocument().IsLocked)
  660. {
  661. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  662. PasswordUI.ClearPassword();
  663. PopupBorder.Visibility = Visibility.Visible;
  664. PasswordUI.Visibility = Visibility.Visible;
  665. }
  666. else
  667. {
  668. pdfDoc.Release();
  669. viewControl = passwordViewer;
  670. LoadDocument();
  671. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  672. }
  673. }
  674. }
  675. }
  676. private void OpenFile_Click(object sender, RoutedEventArgs e)
  677. {
  678. OpenFile();
  679. }
  680. /// <summary>
  681. /// Save the file to another PDF file.
  682. /// </summary>
  683. public void SaveAsFile()
  684. {
  685. if (viewControl != null && viewControl.PDFViewTool != null)
  686. {
  687. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  688. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  689. if (pdfDoc == null)
  690. {
  691. return;
  692. }
  693. SaveFileDialog saveDialog = new SaveFileDialog();
  694. saveDialog.Filter = "(*.pdf)|*.pdf";
  695. saveDialog.DefaultExt = ".pdf";
  696. saveDialog.OverwritePrompt = true;
  697. if (saveDialog.ShowDialog() == true)
  698. {
  699. pdfDoc.WriteToFilePath(saveDialog.FileName);
  700. }
  701. }
  702. }
  703. /// <summary>
  704. /// Save the file in the current path.
  705. /// </summary>
  706. public void SaveFile()
  707. {
  708. if (viewControl != null && viewControl.PDFViewTool != null)
  709. {
  710. CPDFViewer pdfviewer=viewControl.PDFViewTool.GetCPDFViewer();
  711. CPDFDocument pdfDoc=pdfviewer?.GetDocument();
  712. if(pdfDoc==null)
  713. {
  714. return;
  715. }
  716. try
  717. {
  718. if (!string.IsNullOrEmpty(pdfDoc.FilePath))
  719. {
  720. if (pdfDoc.WriteToLoadedPath())
  721. {
  722. return;
  723. }
  724. }
  725. SaveFileDialog saveDialog = new SaveFileDialog
  726. {
  727. Filter = "(*.pdf)|*.pdf",
  728. DefaultExt = ".pdf",
  729. OverwritePrompt = true
  730. };
  731. if (saveDialog.ShowDialog() == true)
  732. {
  733. if (pdfDoc.WriteToFilePath(saveDialog.FileName))
  734. {
  735. AfterSaveAsFileEvent?.Invoke(this, saveDialog.FileName);
  736. }
  737. }
  738. }
  739. catch (Exception ex)
  740. {
  741. return;
  742. }
  743. }
  744. }
  745. #endregion
  746. #region Command Binding
  747. private double CheckZoomLevel(double zoom, bool IsGrowth)
  748. {
  749. double standardZoom = 100;
  750. if (zoom <= 0.01)
  751. {
  752. return 0.01;
  753. }
  754. if (zoom >= 10)
  755. {
  756. return 10;
  757. }
  758. zoom *= 100;
  759. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  760. {
  761. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  762. {
  763. standardZoom = zoomLevelList[i + 1];
  764. break;
  765. }
  766. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  767. {
  768. standardZoom = zoomLevelList[i];
  769. break;
  770. }
  771. }
  772. return standardZoom / 100;
  773. }
  774. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  775. {
  776. OpenFile();
  777. }
  778. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  779. {
  780. if (CanSave)
  781. {
  782. SaveFile();
  783. }
  784. }
  785. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  786. {
  787. SaveAsFile();
  788. }
  789. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  790. {
  791. panelState.IsLeftPanelExpand = !panelState.IsLeftPanelExpand;
  792. }
  793. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  794. {
  795. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  796. {
  797. panelState.RightPanel = PanelState.RightPanelState.None;
  798. }
  799. else
  800. {
  801. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  802. }
  803. }
  804. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  805. {
  806. LeftToolPanelButton.IsChecked = true;
  807. botaBarControl.SelectBotaTool(BOTATools.Bookmark);
  808. }
  809. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  810. {
  811. LeftToolPanelButton.IsChecked = true;
  812. botaBarControl.SelectBotaTool(BOTATools.Outline);
  813. }
  814. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  815. {
  816. LeftToolPanelButton.IsChecked = true;
  817. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  818. }
  819. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  820. {
  821. LeftToolPanelButton.IsChecked = true;
  822. botaBarControl.SelectBotaTool(BOTATools.Annotation);
  823. }
  824. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  825. {
  826. LeftToolPanelButton.IsChecked = true;
  827. botaBarControl.SelectBotaTool(BOTATools.Search);
  828. }
  829. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  830. {
  831. if (viewControl != null && viewControl.PDFViewTool != null)
  832. {
  833. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  834. if (pdfViewer != null)
  835. {
  836. double newZoom = CheckZoomLevel(pdfViewer.GetZoom() + 0.01, true);
  837. pdfViewer.SetZoom(newZoom);
  838. }
  839. }
  840. }
  841. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  842. {
  843. if(viewControl!=null && viewControl.PDFViewTool!=null)
  844. {
  845. CPDFViewer pdfViewer= viewControl.PDFViewTool.GetCPDFViewer();
  846. if(pdfViewer!=null)
  847. {
  848. double newZoom = CheckZoomLevel(pdfViewer.GetZoom() - 0.01, false);
  849. pdfViewer.SetZoom(newZoom);
  850. }
  851. }
  852. }
  853. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  854. {
  855. panelState.RightPanel = PanelState.RightPanelState.ViewSettings;
  856. }
  857. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  858. {
  859. if (PopupBorder.Visibility != Visibility.Visible)
  860. {
  861. PasswordUI.Visibility = Visibility.Collapsed;
  862. FileInfoControl.Visibility = Visibility.Visible;
  863. FileInfoControl.InitWithPDFViewer(viewControl);
  864. FileInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  865. FileInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  866. PopupBorder.Visibility = Visibility.Visible;
  867. }
  868. else
  869. {
  870. FileInfoControl.Visibility = Visibility.Collapsed;
  871. PopupBorder.Visibility = Visibility.Collapsed;
  872. this.Focus();
  873. }
  874. }
  875. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  876. {
  877. PopupBorder.Visibility = Visibility.Collapsed;
  878. }
  879. #endregion
  880. }
  881. }