MainPage.xaml.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. CPDFTitleBarControl.PrintEvent -= CPDFTitleBarControl_PrintEvent;
  662. CPDFTitleBarControl.PrintEvent += CPDFTitleBarControl_PrintEvent;
  663. }
  664. private void CPDFTitleBarControl_PrintEvent(object sender, EventArgs e)
  665. {
  666. PrinterDialog printerDialog = new PrinterDialog();
  667. printerDialog.Document = viewControl.GetCPDFViewer().GetDocument();
  668. printerDialog.ShowDialog();
  669. }
  670. private void CPDFTitleBarControl_FlattenEvent(object sender, EventArgs e)
  671. {
  672. if (viewControl != null && viewControl.GetCPDFViewer() != null && viewControl.GetCPDFViewer().GetDocument() != null)
  673. {
  674. string savePath = ComPDFKit.Controls.Helper.CommonHelper.GetGeneratePathOrEmpty("PDF files (*.pdf)|*.pdf", viewControl.GetCPDFViewer().GetDocument().FileName + "_Flattened.pdf");
  675. if (!string.IsNullOrEmpty(savePath))
  676. {
  677. if (CanSave)
  678. {
  679. SaveFile();
  680. viewControl.PDFViewTool.IsDocumentModified = false;
  681. }
  682. CPDFDocument document = CPDFDocument.InitWithFilePath(viewControl.GetCPDFViewer().GetDocument().FilePath);
  683. if (document?.WriteFlattenToFilePath(savePath) == true)
  684. {
  685. System.Diagnostics.Process.Start("Explorer.exe", "/select," + savePath);
  686. }
  687. document?.Release();
  688. }
  689. }
  690. }
  691. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  692. {
  693. SaveFile();
  694. }
  695. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  696. {
  697. SaveAsFile();
  698. }
  699. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  700. {
  701. OpenFile();
  702. }
  703. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  704. {
  705. PopupBorder.Visibility = Visibility.Collapsed;
  706. }
  707. /// <summary>
  708. /// Refresh the annotation list when a annotation is edited.
  709. /// </summary>
  710. /// <param name="sender"></param>
  711. /// <param name="e"></param>
  712. private void PdfFormControlRefreshAnnotList(object sender, EventArgs e)
  713. {
  714. botaBarControl.LoadAnnotationList();
  715. }
  716. /// <summary>
  717. /// When a CanSave property of a control is changed, the CanSave property of current page is changed.
  718. /// </summary>
  719. /// <param name="sender"></param>
  720. /// <param name="e"></param>
  721. private void ControlOnCanSaveChanged(object sender, bool e)
  722. {
  723. this.CanSave = e;
  724. }
  725. private void PDFViewTool_DocumentModifiedChanged(object sender, EventArgs e)
  726. {
  727. CanSave = viewControl.PDFViewTool.IsDocumentModified;
  728. }
  729. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  730. {
  731. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  732. }
  733. #endregion
  734. #region Open and Save file
  735. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  736. {
  737. SaveFile();
  738. CanSave = false;
  739. }
  740. private void OpenFile(string filePath = "")
  741. {
  742. if (viewControl != null && viewControl.PDFViewTool != null)
  743. {
  744. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  745. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  746. if (pdfDoc == null)
  747. {
  748. return;
  749. }
  750. if (string.IsNullOrEmpty(filePath))
  751. {
  752. filePath = ComPDFKit.Controls.Helper.CommonHelper.GetExistedPathOrEmpty();
  753. }
  754. string oldFilePath = pdfDoc.FilePath;
  755. if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
  756. {
  757. if (oldFilePath.ToLower() == filePath.ToLower())
  758. {
  759. return;
  760. }
  761. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  762. {
  763. return;
  764. }
  765. passwordViewer = new PDFViewControl();
  766. passwordViewer.InitDocument(filePath);
  767. CPDFViewer tempViewer = passwordViewer.PDFViewTool?.GetCPDFViewer();
  768. CPDFDocument tempDoc = tempViewer?.GetDocument();
  769. if (tempDoc == null)
  770. {
  771. MessageBox.Show("Open File Failed");
  772. return;
  773. }
  774. if (passwordViewer.GetCPDFViewer().GetDocument().IsLocked)
  775. {
  776. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  777. PasswordUI.ClearPassword();
  778. PopupBorder.Visibility = Visibility.Visible;
  779. PasswordUI.Visibility = Visibility.Visible;
  780. }
  781. else
  782. {
  783. pdfDoc.Release();
  784. viewControl = passwordViewer;
  785. LoadDocument();
  786. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  787. }
  788. }
  789. }
  790. }
  791. private void OpenFile_Click(object sender, RoutedEventArgs e)
  792. {
  793. OpenFile();
  794. }
  795. /// <summary>
  796. /// Save the file to another PDF file.
  797. /// </summary>
  798. public void SaveAsFile()
  799. {
  800. if (viewControl != null && viewControl.PDFViewTool != null)
  801. {
  802. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  803. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  804. if (pdfDoc == null)
  805. {
  806. return;
  807. }
  808. SaveFileDialog saveDialog = new SaveFileDialog();
  809. saveDialog.Filter = "(*.pdf)|*.pdf";
  810. saveDialog.DefaultExt = ".pdf";
  811. saveDialog.OverwritePrompt = true;
  812. if (saveDialog.ShowDialog() == true)
  813. {
  814. pdfDoc.WriteToFilePath(saveDialog.FileName);
  815. }
  816. }
  817. }
  818. /// <summary>
  819. /// Save the file in the current path.
  820. /// </summary>
  821. public void SaveFile()
  822. {
  823. if (viewControl != null && viewControl.PDFViewTool != null)
  824. {
  825. CPDFViewer pdfviewer = viewControl.PDFViewTool.GetCPDFViewer();
  826. CPDFDocument pdfDoc = pdfviewer?.GetDocument();
  827. if (pdfDoc == null)
  828. {
  829. return;
  830. }
  831. try
  832. {
  833. if (!string.IsNullOrEmpty(pdfDoc.FilePath))
  834. {
  835. if (pdfDoc.WriteToLoadedPath())
  836. {
  837. return;
  838. }
  839. }
  840. SaveFileDialog saveDialog = new SaveFileDialog
  841. {
  842. Filter = "(*.pdf)|*.pdf",
  843. DefaultExt = ".pdf",
  844. OverwritePrompt = true
  845. };
  846. if (saveDialog.ShowDialog() == true)
  847. {
  848. if (pdfDoc.WriteToFilePath(saveDialog.FileName))
  849. {
  850. AfterSaveAsFileEvent?.Invoke(this, saveDialog.FileName);
  851. }
  852. }
  853. }
  854. catch (Exception ex)
  855. {
  856. return;
  857. }
  858. }
  859. }
  860. #endregion
  861. #region Command Binding
  862. private double CheckZoomLevel(double zoom, bool IsGrowth)
  863. {
  864. double standardZoom = 100;
  865. if (zoom <= 0.01)
  866. {
  867. return 0.01;
  868. }
  869. if (zoom >= 10)
  870. {
  871. return 10;
  872. }
  873. zoom *= 100;
  874. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  875. {
  876. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  877. {
  878. standardZoom = zoomLevelList[i + 1];
  879. break;
  880. }
  881. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  882. {
  883. standardZoom = zoomLevelList[i];
  884. break;
  885. }
  886. }
  887. return standardZoom / 100;
  888. }
  889. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  890. {
  891. OpenFile();
  892. }
  893. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  894. {
  895. if (CanSave)
  896. {
  897. SaveFile();
  898. }
  899. }
  900. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  901. {
  902. SaveAsFile();
  903. }
  904. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  905. {
  906. panelState.IsLeftPanelExpand = !panelState.IsLeftPanelExpand;
  907. }
  908. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  909. {
  910. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  911. {
  912. panelState.RightPanel = PanelState.RightPanelState.None;
  913. }
  914. else
  915. {
  916. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  917. }
  918. }
  919. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  920. {
  921. LeftToolPanelButton.IsChecked = true;
  922. botaBarControl.SelectBotaTool(BOTATools.Bookmark);
  923. }
  924. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  925. {
  926. LeftToolPanelButton.IsChecked = true;
  927. botaBarControl.SelectBotaTool(BOTATools.Outline);
  928. }
  929. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  930. {
  931. LeftToolPanelButton.IsChecked = true;
  932. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  933. }
  934. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  935. {
  936. LeftToolPanelButton.IsChecked = true;
  937. botaBarControl.SelectBotaTool(BOTATools.Annotation);
  938. }
  939. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  940. {
  941. LeftToolPanelButton.IsChecked = true;
  942. botaBarControl.SelectBotaTool(BOTATools.Search);
  943. }
  944. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  945. {
  946. if (viewControl != null && viewControl.PDFViewTool != null)
  947. {
  948. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  949. if (pdfViewer != null)
  950. {
  951. double newZoom = CheckZoomLevel(pdfViewer.GetZoom() + 0.01, true);
  952. pdfViewer.SetZoom(newZoom);
  953. }
  954. }
  955. }
  956. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  957. {
  958. if (viewControl != null && viewControl.PDFViewTool != null)
  959. {
  960. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  961. if (pdfViewer != null)
  962. {
  963. double newZoom = CheckZoomLevel(pdfViewer.GetZoom() - 0.01, false);
  964. pdfViewer.SetZoom(newZoom);
  965. }
  966. }
  967. }
  968. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  969. {
  970. panelState.RightPanel = PanelState.RightPanelState.ViewSettings;
  971. }
  972. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  973. {
  974. if (PopupBorder.Visibility != Visibility.Visible)
  975. {
  976. PasswordUI.Visibility = Visibility.Collapsed;
  977. FileInfoControl.Visibility = Visibility.Visible;
  978. FileInfoControl.InitWithPDFViewer(viewControl);
  979. FileInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  980. FileInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  981. PopupBorder.Visibility = Visibility.Visible;
  982. }
  983. else
  984. {
  985. FileInfoControl.Visibility = Visibility.Collapsed;
  986. PopupBorder.Visibility = Visibility.Collapsed;
  987. this.Focus();
  988. }
  989. }
  990. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  991. {
  992. PopupBorder.Visibility = Visibility.Collapsed;
  993. }
  994. #endregion
  995. }
  996. }