MainPage.xaml.cs 40 KB

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