MainWindow.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. CPDFAnnotationControl pdfAnnotationControl = null;
  31. public MainWindow()
  32. {
  33. InitializeComponent();
  34. TitleBarControl.Loaded += TitleBarControl_Loaded;
  35. AnnotationBarControl.Loaded += AnnotationBarControl_Loaded;
  36. ModeSelectorBarControl.ShowBOTAEvent += ModeSelectorBarControl_ShowBOTAEvent;
  37. }
  38. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  39. {
  40. AnnotationType[] annotationProperties = { AnnotationType.Highlight, AnnotationType.Underline, AnnotationType.Strikeout, AnnotationType.Squiggly, AnnotationType.Freetext, AnnotationType.Note, AnnotationType.Circle, AnnotationType.Square, AnnotationType.Arrow, AnnotationType.Line, AnnotationType.Stamp, AnnotationType.Signature, AnnotationType.Link, AnnotationType.Sound };
  41. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  42. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  43. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  44. }
  45. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  46. {
  47. pdfAnnotationControl.AnnotationCancel();
  48. ExpandPropertyPanel((sender as ToggleButton).IsChecked == true);
  49. }
  50. /// <summary>
  51. /// 注释菜单选项变换时
  52. /// 1、注释属性面板变换,注册属性变换的监听
  53. /// 2、PDFViewerControl属性设置变换
  54. /// </summary>
  55. /// <param name="sender"></param>
  56. /// <param name="e"></param>
  57. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, AnnotationType e)
  58. {
  59. UIElement propertyPanel = GetPropertyPanel();
  60. pdfAnnotationControl = new CPDFAnnotationControl();
  61. pdfAnnotationControl.InitWithPDFViewer(pdfViewer);
  62. pdfAnnotationControl.LoadAnnotationPanel(e);
  63. SetPropertyPanel(pdfAnnotationControl);
  64. ExpandPropertyPanel((sender as ToggleButton).IsChecked == true);
  65. }
  66. private void ModeSelectorBarControl_ShowBOTAEvent(object sender, EventArgs e)
  67. {
  68. UIElement botaTool = GetBotaTool();
  69. if (botaTool == null)
  70. {
  71. CPDFBOTABarControl pdfBOTABarControl = new CPDFBOTABarControl(BOTATools.Outline | BOTATools.Thumbnail | BOTATools.Annotation | BOTATools.Search | BOTATools.Bookmark);
  72. if (pdfViewer != null && pdfViewer.Document != null)
  73. {
  74. pdfBOTABarControl.InitWithPDFViewer(pdfViewer);
  75. }
  76. SetBotaTool(pdfBOTABarControl);
  77. }
  78. ExpandTool(ModeSelectorBarControl.BOTABarIsShowing == true);
  79. }
  80. /// <summary>
  81. /// 加载文档
  82. /// </summary>
  83. private void LoadDocument()
  84. {
  85. pdfViewer.Load();
  86. PDFGrid.Child = pdfViewer;
  87. UIElement currentBotaTool = GetBotaTool();
  88. if (currentBotaTool is CPDFBOTABarControl)
  89. {
  90. ((CPDFBOTABarControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  91. }
  92. }
  93. private void TitleBarControl_OpenFileEvent(object sender, string filePath)
  94. {
  95. pdfViewer?.CloseDocument();
  96. pdfViewer?.InitDocument(filePath);
  97. LoadDocument();
  98. }
  99. private void TitleBarControl_Loaded(object sender, RoutedEventArgs e)
  100. {
  101. TitleBarControl.OpenFileEvent += TitleBarControl_OpenFileEvent;
  102. }
  103. private UIElement GetPropertyPanel()
  104. {
  105. return PropertyPanelContainer.Child;
  106. }
  107. private UIElement GetBotaTool()
  108. {
  109. return BotaToolContainer.Child;
  110. }
  111. private void SetPropertyPanel(UIElement propertyPanel)
  112. {
  113. PropertyPanelContainer.Child = propertyPanel;
  114. }
  115. private void SetBotaTool(UIElement newChild)
  116. {
  117. BotaToolContainer.Child = newChild;
  118. }
  119. /// <summary>
  120. /// 展开Property工具
  121. /// </summary>
  122. /// <param name="isExpand"></param>
  123. private void ExpandPropertyPanel(bool isExpand)
  124. {
  125. if (isExpand)
  126. {
  127. BodyGrid.ColumnDefinitions[4].Width = new GridLength(300);
  128. }
  129. else
  130. {
  131. BodyGrid.ColumnDefinitions[4].Width = new GridLength(0);
  132. }
  133. PropertyPanelContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  134. PropertyPanelSplitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  135. }
  136. /// <summary>
  137. /// 展开Bota工具
  138. /// </summary>
  139. /// <param name="isExpand"></param>
  140. private void ExpandTool(bool isExpand)
  141. {
  142. if (isExpand)
  143. {
  144. BodyGrid.ColumnDefinitions[0].Width = new GridLength(300);
  145. }
  146. else
  147. {
  148. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  149. }
  150. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  151. BotaSplitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  152. }
  153. }
  154. }