MainWindow.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using ComPDFKit.PDFDocument;
  2. using compdfkit_tools.Annotation.PDFAnnotationControl;
  3. using compdfkit_tools.Data;
  4. using compdfkit_tools.PDFControl;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.PdfViewer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. namespace annotation_ctrl_demo
  23. {
  24. /// <summary>
  25. /// MainWindow.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class MainWindow : Window
  28. {
  29. private CPDFViewer pdfViewer = new CPDFViewer();
  30. private Dictionary<string, AnnotationType> getAnnotationFromTag;
  31. public MainWindow()
  32. {
  33. InitializeComponent();
  34. TitleBarControl.Loaded += TitleBarControl_Loaded;
  35. AnnotationBarControl.Loaded += AnnotationBarControl_Loaded;
  36. ModeSelectorBarControl.ShowBOTAEvent += ModeSelectorBarControl_ShowBOTAEvent;
  37. InitDictionary();
  38. }
  39. private void InitDictionary()
  40. {
  41. getAnnotationFromTag = new Dictionary<string, AnnotationType>();
  42. getAnnotationFromTag.Clear();
  43. getAnnotationFromTag.Add("Highlight", AnnotationType.Highlight);
  44. getAnnotationFromTag.Add("Underline", AnnotationType.Underline);
  45. getAnnotationFromTag.Add("Strikeout", AnnotationType.Strikeout);
  46. getAnnotationFromTag.Add("Squiggly", AnnotationType.Squiggly);
  47. getAnnotationFromTag.Add("Rect", AnnotationType.Rect);
  48. getAnnotationFromTag.Add("Round", AnnotationType.Round);
  49. getAnnotationFromTag.Add("StraightLine", AnnotationType.StraightLine);
  50. }
  51. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  52. {
  53. AnnotationType[] annotationProperties = { AnnotationType.Highlight, AnnotationType.Underline, AnnotationType.Strikeout, AnnotationType.Squiggly, AnnotationType.Freetext, AnnotationType.Note, AnnotationType.Round, AnnotationType.Rect, AnnotationType.Arrow, AnnotationType.StraightLine, AnnotationType.Line, AnnotationType.Stamp, AnnotationType.Signature, AnnotationType.Link, AnnotationType.Sound };
  54. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  55. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  56. }
  57. /// <summary>
  58. /// 注释菜单选项变换时
  59. /// 1、注释属性面板变换,注册属性变换的监听
  60. /// 2、PDFViewerControl属性设置变换
  61. /// </summary>
  62. /// <param name="sender"></param>
  63. /// <param name="e"></param>
  64. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, string e)
  65. {
  66. UIElement propertyPanel = GetPropertyPanel();
  67. CPDFAnnotationControl pdfAnnotationControl = new CPDFAnnotationControl();
  68. pdfAnnotationControl.PropertyChanged += PDFAnnotationControl_PropertyChanged;
  69. pdfAnnotationControl.LoadAnnotationPanel(getAnnotationFromTag[e]);
  70. SetPropertyPanel(pdfAnnotationControl);
  71. ExpandPropertyPanel((sender as ToggleButton).IsChecked == true);
  72. CPDFAnnotationData pdfAnnotationData = null;
  73. pdfAnnotationData = pdfAnnotationControl.GetAnnotationData();
  74. CPDFViewerControl.ChangeAnnotationMode(pdfAnnotationData);
  75. }
  76. private void PDFAnnotationControl_PropertyChanged(object sender, CPDFAnnotationData e)
  77. {
  78. CPDFViewerControl.ChangeAnnotationMode(e);
  79. }
  80. private void ModeSelectorBarControl_ShowBOTAEvent(object sender, EventArgs e)
  81. {
  82. UIElement botaTool = GetBotaTool();
  83. if (botaTool == null)
  84. {
  85. CPDFBOTABarControl pdfBOTABarControl = new CPDFBOTABarControl(BOTATools.Outline | BOTATools.Thumbnail | BOTATools.Annotation | BOTATools.Search | BOTATools.Bookmark);
  86. if (pdfViewer != null && pdfViewer.Document != null)
  87. {
  88. pdfBOTABarControl.InitWithPDFViewer(pdfViewer);
  89. }
  90. SetBotaTool(pdfBOTABarControl);
  91. }
  92. ExpandTool(ModeSelectorBarControl.BOTABarIsShowing == true);
  93. }
  94. /// <summary>
  95. /// 加载文档
  96. /// </summary>
  97. private void LoadDocument()
  98. {
  99. pdfViewer.Load();
  100. CPDFViewerControl.InitWithPDFView(pdfViewer);
  101. UIElement currentBotaTool = GetBotaTool();
  102. if (currentBotaTool is CPDFBOTABarControl)
  103. {
  104. ((CPDFBOTABarControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  105. }
  106. }
  107. private void TitleBarControl_OpenFileEvent(object sender, string filePath)
  108. {
  109. pdfViewer?.CloseDocument();
  110. pdfViewer?.InitDocument(filePath);
  111. LoadDocument();
  112. }
  113. private void TitleBarControl_Loaded(object sender, RoutedEventArgs e)
  114. {
  115. TitleBarControl.OpenFileEvent += TitleBarControl_OpenFileEvent;
  116. }
  117. private UIElement GetPropertyPanel()
  118. {
  119. return PropertyPanelContainer.Child;
  120. }
  121. private UIElement GetBotaTool()
  122. {
  123. return BotaToolContainer.Child;
  124. }
  125. private void SetPropertyPanel(UIElement propertyPanel)
  126. {
  127. PropertyPanelContainer.Child = propertyPanel;
  128. }
  129. private void SetBotaTool(UIElement newChild)
  130. {
  131. BotaToolContainer.Child = newChild;
  132. }
  133. /// <summary>
  134. /// 展开Property工具
  135. /// </summary>
  136. /// <param name="isExpand"></param>
  137. private void ExpandPropertyPanel(bool isExpand)
  138. {
  139. if (isExpand)
  140. {
  141. BodyGrid.ColumnDefinitions[4].Width = new GridLength(300);
  142. }
  143. else
  144. {
  145. BodyGrid.ColumnDefinitions[4].Width = new GridLength(0);
  146. }
  147. PropertyPanelContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  148. PropertyPanelSplitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  149. }
  150. /// <summary>
  151. /// 展开Bota工具
  152. /// </summary>
  153. /// <param name="isExpand"></param>
  154. private void ExpandTool(bool isExpand)
  155. {
  156. if (isExpand)
  157. {
  158. BodyGrid.ColumnDefinitions[0].Width = new GridLength(300);
  159. }
  160. else
  161. {
  162. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  163. }
  164. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  165. BotaSplitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  166. }
  167. }
  168. }