MainPage.xaml.cs 44 KB

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