MainPage.xaml.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  7. using Compdfkit_Tools.Helper;
  8. using Compdfkit_Tools.Measure;
  9. using Compdfkit_Tools.PDFControl;
  10. using Compdfkit_Tools.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_Tools.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_Tools.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.PDFViewTool.SetViewerModel(MouseModes.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(ComPDFKit.Tool.CPDFToolManager.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(ComPDFKit.Tool.CPDFToolManager.ToolType.CreateAnnot);
  513. viewControl.PDFViewTool.SetViewerModel(MouseModes.PanTool);
  514. annotationControl.PDFViewControl = viewControl;
  515. annotationControl.InitWithPDFViewer(viewControl);
  516. if (annotationControl.PDFViewControl != null)
  517. {
  518. annotationControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  519. annotationControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  520. annotationControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  521. annotationControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  522. annotationControl.InitialPDFViewControl(annotationControl.PDFViewControl);
  523. annotationControl.SetBOTAContainer(botaBarControl);
  524. annotationControl.SetDisplaySettingsControl(displaySettingsControl);
  525. }
  526. }
  527. else if (item.Tag as string == "Form")
  528. {
  529. formControl.SetToolBarContainerVisibility(Visibility.Visible);
  530. formControl.PdfViewControl = viewControl;
  531. formControl.InitWithPDFViewer(viewControl);
  532. if (formControl.PdfViewControl != null)
  533. {
  534. PDFGrid.Child = formControl;
  535. viewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  536. formControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  537. formControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  538. formControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  539. formControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  540. formControl.SetBOTAContainer(botaBarControl);
  541. formControl.InitialPDFViewControl(formControl.PdfViewControl);
  542. formControl.SetDisplaySettingsControl(displaySettingsControl);
  543. viewControl.GetCPDFViewer().SetFormFieldHighlight(true);
  544. }
  545. }
  546. else if (item.Tag as string == "Content Editor")
  547. {
  548. contentEditControl.PdfViewControl = viewControl;
  549. contentEditControl.InitWithPDFViewer(viewControl);
  550. displaySettingsControl.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  551. if (contentEditControl.pdfContentEditControl != null && contentEditControl.PdfViewControl != null)
  552. {
  553. PDFGrid.Child = contentEditControl;
  554. viewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.ContentEdit);
  555. viewControl.PDFViewTool.SetViewerModel(MouseModes.ContentEdit);
  556. contentEditControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  557. contentEditControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  558. contentEditControl.SetBOTAContainer(botaBarControl);
  559. contentEditControl.SetDisplaySettingsControl(displaySettingsControl);
  560. contentEditControl.PdfViewControl.SetSplitViewMode(CPDFViewModeUI.SplitMode.None);
  561. }
  562. }
  563. else if (item.Tag as string == "Document Editor")
  564. {
  565. pageEditControl.PDFViewControl = viewControl;
  566. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit;
  567. pageEditControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  568. pageEditControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  569. PDFGrid.Child = pageEditControl;
  570. NotDocsEditorVisible = Visibility.Collapsed;
  571. }
  572. else if (item.Tag as string == "Digital Signature")
  573. {
  574. if (digitalSignatureControl.PDFViewControl != null)
  575. {
  576. RightPanelButton.Visibility = Visibility.Collapsed;
  577. PDFGrid.Child = digitalSignatureControl;
  578. viewControl.PDFViewTool.SetViewerModel(MouseModes.Viewer);
  579. digitalSignatureControl.PDFViewControl = viewControl;
  580. botaBarControl.AddBOTAContent(BOTATools.Signature);
  581. digitalSignatureControl.InitWithPDFViewer(viewControl);
  582. digitalSignatureControl.SetBOTAContainer(botaBarControl);
  583. digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
  584. digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  585. signatureStatusBarControl.OnViewSignatureButtonClicked -= ViewAllSignatures;
  586. signatureStatusBarControl.OnViewSignatureButtonClicked += ViewAllSignatures;
  587. digitalSignatureControl.OnCanSaveChanged -= DigitalSignatureControl_OnCanSaveChanged;
  588. digitalSignatureControl.OnCanSaveChanged += DigitalSignatureControl_OnCanSaveChanged;
  589. digitalSignatureControl.SignatureStatusChanged -= DigitalSignatureControl_OnSignatureStatusChanged;
  590. digitalSignatureControl.SignatureStatusChanged += DigitalSignatureControl_OnSignatureStatusChanged;
  591. digitalSignatureControl.AfterFillSignature -= DigitalSignatureControl_AfterFillSignature;
  592. digitalSignatureControl.AfterFillSignature += DigitalSignatureControl_AfterFillSignature;
  593. }
  594. }
  595. else if (item.Tag as string == "Measurement")
  596. {
  597. if (contentEditControl.pdfContentEditControl != null && viewControl != null)
  598. {
  599. RightPanelButton.Visibility = Visibility.Visible;
  600. PDFGrid.Child = measureControl;
  601. viewControl.PDFViewTool.SetViewerModel(MouseModes.PanTool);
  602. viewControl.SetToolType(ToolType.Pan);
  603. measureControl.InitWithPDFViewer(viewControl);
  604. measureControl.SetBOTAContainer(botaBarControl);
  605. measureControl.ClearAllToolState();
  606. measureControl.SetSettingsControl(displaySettingsControl);
  607. GetPDFViewControl().PDFViewTool.GetDefaultSettingParam().IsOpenMeasure = true;
  608. measureControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  609. measureControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  610. }
  611. }
  612. currentMode = item.Tag as string;
  613. RightToolPanelButtonIsChecked = false;
  614. }
  615. /// <summary>
  616. /// Event handler for ViewSignatureBtn click event. Expand the bota control and select the Signature tool.
  617. /// </summary>
  618. /// <param name="sender"></param>
  619. /// <param name="e"></param>
  620. private void ViewAllSignatures(object sender, EventArgs e)
  621. {
  622. LeftToolPanelButton.IsChecked = true;
  623. botaBarControl.SelectBotaTool(BOTATools.Signature);
  624. }
  625. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  626. {
  627. ModeComboBox.SelectedIndex = 0;
  628. }
  629. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  630. {
  631. PasswordUI.Visibility = Visibility.Collapsed;
  632. FileInfoUI.Visibility = Visibility.Visible;
  633. FileInfoControl.InitWithPDFViewer(viewControl);
  634. PopupBorder.Visibility = Visibility.Visible;
  635. }
  636. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  637. {
  638. panelState.RightPanel =
  639. ((sender as ToggleButton).IsChecked == true) ?
  640. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  641. }
  642. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  643. {
  644. panelState.RightPanel =
  645. ((sender as ToggleButton).IsChecked == true) ?
  646. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  647. }
  648. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  649. {
  650. LeftToolPanelButton.IsChecked = true;
  651. botaBarControl.SelectBotaTool(BOTATools.Search);
  652. }
  653. private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e)
  654. {
  655. CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent;
  656. CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent;
  657. CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent;
  658. CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent;
  659. CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent;
  660. CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent;
  661. CPDFTitleBarControl.FlattenEvent -= CPDFTitleBarControl_FlattenEvent;
  662. CPDFTitleBarControl.FlattenEvent += CPDFTitleBarControl_FlattenEvent;
  663. }
  664. private void CPDFTitleBarControl_FlattenEvent(object sender, EventArgs e)
  665. {
  666. if (viewControl != null && viewControl.GetCPDFViewer() != null && viewControl.GetCPDFViewer().GetDocument() != null)
  667. {
  668. string savePath = Compdfkit_Tools.Helper.CommonHelper.GetGeneratePathOrEmpty("PDF files (*.pdf)|*.pdf", viewControl.GetCPDFViewer().GetDocument().FileName + "_Flattened.pdf");
  669. if (!string.IsNullOrEmpty(savePath))
  670. {
  671. if (CanSave)
  672. {
  673. SaveFile();
  674. viewControl.PDFViewTool.IsDocumentModified = false;
  675. }
  676. CPDFDocument document = CPDFDocument.InitWithFilePath(viewControl.GetCPDFViewer().GetDocument().FilePath);
  677. if (document?.WriteFlattenToFilePath(savePath) == true)
  678. {
  679. System.Diagnostics.Process.Start("Explorer.exe", "/select," + savePath);
  680. }
  681. document?.Release();
  682. }
  683. }
  684. }
  685. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  686. {
  687. SaveFile();
  688. }
  689. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  690. {
  691. SaveAsFile();
  692. }
  693. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  694. {
  695. OpenFile();
  696. }
  697. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  698. {
  699. PopupBorder.Visibility = Visibility.Collapsed;
  700. }
  701. /// <summary>
  702. /// Refresh the annotation list when a annotation is edited.
  703. /// </summary>
  704. /// <param name="sender"></param>
  705. /// <param name="e"></param>
  706. private void PdfFormControlRefreshAnnotList(object sender, EventArgs e)
  707. {
  708. botaBarControl.LoadAnnotationList();
  709. }
  710. /// <summary>
  711. /// When a CanSave property of a control is changed, the CanSave property of current page is changed.
  712. /// </summary>
  713. /// <param name="sender"></param>
  714. /// <param name="e"></param>
  715. private void ControlOnCanSaveChanged(object sender, bool e)
  716. {
  717. this.CanSave = e;
  718. }
  719. private void PDFViewTool_DocumentModifiedChanged(object sender, EventArgs e)
  720. {
  721. CanSave = viewControl.PDFViewTool.IsDocumentModified;
  722. }
  723. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  724. {
  725. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  726. }
  727. #endregion
  728. #region Open and Save file
  729. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  730. {
  731. SaveFile();
  732. CanSave = false;
  733. }
  734. private void OpenFile(string filePath = "")
  735. {
  736. if (viewControl != null && viewControl.PDFViewTool != null)
  737. {
  738. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  739. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  740. if (pdfDoc == null)
  741. {
  742. return;
  743. }
  744. if (string.IsNullOrEmpty(filePath))
  745. {
  746. filePath = Compdfkit_Tools.Helper.CommonHelper.GetExistedPathOrEmpty();
  747. }
  748. string oldFilePath = pdfDoc.FilePath;
  749. if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
  750. {
  751. if (oldFilePath.ToLower() == filePath.ToLower())
  752. {
  753. return;
  754. }
  755. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  756. {
  757. return;
  758. }
  759. passwordViewer = new PDFViewControl();
  760. passwordViewer.InitDocument(filePath);
  761. CPDFViewer tempViewer = passwordViewer.PDFViewTool?.GetCPDFViewer();
  762. CPDFDocument tempDoc = tempViewer?.GetDocument();
  763. if (tempDoc == null)
  764. {
  765. MessageBox.Show("Open File Failed");
  766. return;
  767. }
  768. if (passwordViewer.GetCPDFViewer().GetDocument().IsLocked)
  769. {
  770. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  771. PasswordUI.ClearPassword();
  772. PopupBorder.Visibility = Visibility.Visible;
  773. PasswordUI.Visibility = Visibility.Visible;
  774. }
  775. else
  776. {
  777. pdfDoc.Release();
  778. viewControl = passwordViewer;
  779. LoadDocument();
  780. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  781. }
  782. }
  783. }
  784. }
  785. private void OpenFile_Click(object sender, RoutedEventArgs e)
  786. {
  787. OpenFile();
  788. }
  789. /// <summary>
  790. /// Save the file to another PDF file.
  791. /// </summary>
  792. public void SaveAsFile()
  793. {
  794. if (viewControl != null && viewControl.PDFViewTool != null)
  795. {
  796. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  797. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  798. if (pdfDoc == null)
  799. {
  800. return;
  801. }
  802. SaveFileDialog saveDialog = new SaveFileDialog();
  803. saveDialog.Filter = "(*.pdf)|*.pdf";
  804. saveDialog.DefaultExt = ".pdf";
  805. saveDialog.OverwritePrompt = true;
  806. if (saveDialog.ShowDialog() == true)
  807. {
  808. pdfDoc.WriteToFilePath(saveDialog.FileName);
  809. }
  810. }
  811. }
  812. /// <summary>
  813. /// Save the file in the current path.
  814. /// </summary>
  815. public void SaveFile()
  816. {
  817. if (viewControl != null && viewControl.PDFViewTool != null)
  818. {
  819. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  820. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  821. if (pdfDoc == null)
  822. {
  823. return;
  824. }
  825. try
  826. {
  827. if (!string.IsNullOrEmpty(pdfDoc.FilePath))
  828. {
  829. if (pdfDoc.WriteToLoadedPath())
  830. {
  831. return;
  832. }
  833. }
  834. SaveFileDialog saveDialog = new SaveFileDialog
  835. {
  836. Filter = "(*.pdf)|*.pdf",
  837. DefaultExt = ".pdf",
  838. OverwritePrompt = true
  839. };
  840. if (saveDialog.ShowDialog() == true)
  841. {
  842. if (pdfDoc.WriteToFilePath(saveDialog.FileName))
  843. {
  844. AfterSaveAsFileEvent?.Invoke(this, saveDialog.FileName);
  845. }
  846. }
  847. }
  848. catch (Exception ex)
  849. {
  850. return;
  851. }
  852. }
  853. }
  854. #endregion
  855. #region Command Binding
  856. private double CheckZoomLevel(double zoom, bool IsGrowth)
  857. {
  858. double standardZoom = 100;
  859. if (zoom <= 0.01)
  860. {
  861. return 0.01;
  862. }
  863. if (zoom >= 10)
  864. {
  865. return 10;
  866. }
  867. zoom *= 100;
  868. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  869. {
  870. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  871. {
  872. standardZoom = zoomLevelList[i + 1];
  873. break;
  874. }
  875. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  876. {
  877. standardZoom = zoomLevelList[i];
  878. break;
  879. }
  880. }
  881. return standardZoom / 100;
  882. }
  883. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  884. {
  885. OpenFile();
  886. }
  887. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  888. {
  889. if (CanSave)
  890. {
  891. SaveFile();
  892. }
  893. }
  894. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  895. {
  896. SaveAsFile();
  897. }
  898. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  899. {
  900. panelState.IsLeftPanelExpand = !panelState.IsLeftPanelExpand;
  901. }
  902. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  903. {
  904. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  905. {
  906. panelState.RightPanel = PanelState.RightPanelState.None;
  907. }
  908. else
  909. {
  910. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  911. }
  912. }
  913. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  914. {
  915. LeftToolPanelButton.IsChecked = true;
  916. botaBarControl.SelectBotaTool(BOTATools.Bookmark);
  917. }
  918. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  919. {
  920. LeftToolPanelButton.IsChecked = true;
  921. botaBarControl.SelectBotaTool(BOTATools.Outline);
  922. }
  923. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  924. {
  925. LeftToolPanelButton.IsChecked = true;
  926. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  927. }
  928. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  929. {
  930. LeftToolPanelButton.IsChecked = true;
  931. botaBarControl.SelectBotaTool(BOTATools.Annotation);
  932. }
  933. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  934. {
  935. LeftToolPanelButton.IsChecked = true;
  936. botaBarControl.SelectBotaTool(BOTATools.Search);
  937. }
  938. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  939. {
  940. if (viewControl != null && viewControl.PDFViewTool != null)
  941. {
  942. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  943. if (pdfViewer != null)
  944. {
  945. double newZoom = CheckZoomLevel(pdfViewer.GetZoom() + 0.01, true);
  946. pdfViewer.SetZoom(newZoom);
  947. }
  948. }
  949. }
  950. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  951. {
  952. if (viewControl != null && viewControl.PDFViewTool != null)
  953. {
  954. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  955. if (pdfViewer != null)
  956. {
  957. double newZoom = CheckZoomLevel(pdfViewer.GetZoom() - 0.01, false);
  958. pdfViewer.SetZoom(newZoom);
  959. }
  960. }
  961. }
  962. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  963. {
  964. panelState.RightPanel = PanelState.RightPanelState.ViewSettings;
  965. }
  966. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  967. {
  968. if (PopupBorder.Visibility != Visibility.Visible)
  969. {
  970. PasswordUI.Visibility = Visibility.Collapsed;
  971. FileInfoControl.Visibility = Visibility.Visible;
  972. FileInfoControl.InitWithPDFViewer(viewControl);
  973. FileInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  974. FileInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  975. PopupBorder.Visibility = Visibility.Visible;
  976. }
  977. else
  978. {
  979. FileInfoControl.Visibility = Visibility.Collapsed;
  980. PopupBorder.Visibility = Visibility.Collapsed;
  981. this.Focus();
  982. }
  983. }
  984. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  985. {
  986. PopupBorder.Visibility = Visibility.Collapsed;
  987. }
  988. #endregion
  989. }
  990. }