MainPage.xaml.cs 40 KB

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