MainWindow.xaml.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Compdfkit_Tools.Helper;
  2. using Compdfkit_Tools.PDFControl;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace DocsEditControl
  21. {
  22. public partial class MainWindow : Window, INotifyPropertyChanged
  23. {
  24. private PDFViewControl pdfViewer;
  25. private PDFViewControl passwordViewer;
  26. private PageEditControl pageEditControl = new PageEditControl();
  27. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  28. public bool CanUndo
  29. {
  30. get
  31. {
  32. if (pdfViewer != null && pdfViewer.PDFView != null)
  33. {
  34. return pdfViewer.PDFView.UndoManager.CanUndo;
  35. }
  36. return false;
  37. }
  38. }
  39. public bool CanRedo
  40. {
  41. get
  42. {
  43. if (pdfViewer != null && pdfViewer.PDFView != null)
  44. {
  45. return pdfViewer.PDFView.UndoManager.CanRedo;
  46. }
  47. return false;
  48. }
  49. }
  50. private bool _canSave = false;
  51. public bool CanSave
  52. {
  53. get => _canSave;
  54. set
  55. {
  56. _canSave = value;
  57. OnPropertyChanged();
  58. }
  59. }
  60. public MainWindow()
  61. {
  62. InitializeComponent();
  63. LoadDefaultDocument();
  64. }
  65. #region Load document
  66. private void LoadDefaultDocument()
  67. {
  68. string defaultFilePath = "PDF32000_2008.pdf";
  69. pdfViewer = new PDFViewControl();
  70. pdfViewer.PDFView.InitDocument(defaultFilePath);
  71. LoadDocument();
  72. }
  73. private void LoadDocument()
  74. {
  75. pdfViewer.PDFView.Load();
  76. pdfViewer.PDFView.SetShowLink(true);
  77. pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  78. pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  79. PDFGrid.Child = pageEditControl;
  80. pageEditControl.PDFViewControl = pdfViewer;
  81. pageEditControl.OnAnnotEditHandler -= PageEditControlRefreshAnnotList;
  82. pageEditControl.OnAnnotEditHandler += PageEditControlRefreshAnnotList;
  83. pageEditControl.PageMoved -= PageEditControl_PageMoved;
  84. pageEditControl.PageMoved += PageEditControl_PageMoved;
  85. PasswordUI.Closed -= PasswordUI_Closed;
  86. PasswordUI.Canceled -= PasswordUI_Canceled;
  87. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  88. PasswordUI.Closed += PasswordUI_Closed;
  89. PasswordUI.Canceled += PasswordUI_Canceled;
  90. PasswordUI.Confirmed += PasswordUI_Confirmed;
  91. CPDFSaclingControl.InitWithPDFViewer(pdfViewer.PDFView);
  92. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.PDFView.ZoomFactor * 100)));
  93. ViewSettingBtn.IsChecked = false;
  94. botaBarControl.InitWithPDFViewer(pdfViewer.PDFView);
  95. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  96. }
  97. private void PageEditControlRefreshAnnotList(object sender, EventArgs e)
  98. {
  99. botaBarControl.LoadAnnotationList();
  100. }
  101. private void PasswordUI_Confirmed(object sender, string e)
  102. {
  103. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  104. {
  105. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  106. if (passwordViewer.PDFView.Document.IsLocked == false)
  107. {
  108. PasswordUI.SetShowError("", Visibility.Collapsed);
  109. PasswordUI.ClearPassword();
  110. PasswordUI.Visibility = Visibility.Collapsed;
  111. PopupBorder.Visibility = Visibility.Collapsed;
  112. pdfViewer = passwordViewer;
  113. LoadDocument();
  114. }
  115. else
  116. {
  117. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  118. }
  119. }
  120. }
  121. private void PasswordUI_Canceled(object sender, EventArgs e)
  122. {
  123. PopupBorder.Visibility = Visibility.Collapsed;
  124. PasswordUI.Visibility = Visibility.Collapsed;
  125. }
  126. private void PasswordUI_Closed(object sender, EventArgs e)
  127. {
  128. PopupBorder.Visibility = Visibility.Collapsed;
  129. PasswordUI.Visibility = Visibility.Collapsed;
  130. }
  131. #endregion
  132. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  133. {
  134. if (e.Key == "Zoom")
  135. {
  136. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  137. }
  138. }
  139. private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
  140. {
  141. if (pdfViewer == null || pdfViewer.PDFView == null)
  142. {
  143. return;
  144. }
  145. BotaSideTool.InitWithPDFViewer(pdfViewer.PDFView);
  146. }
  147. public event PropertyChangedEventHandler PropertyChanged;
  148. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  149. {
  150. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  151. }
  152. }
  153. }