MainPage.xaml.cs 45 KB

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