MainPage.xaml.cs 40 KB

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