MainPage.xaml.cs 39 KB

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