MainPage.xaml.cs 45 KB

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