using ComPDFKit.PDFDocument; using compdfkit_tools.Annotation.PDFAnnotationControl; using compdfkit_tools.Data; using compdfkit_tools.PDFControl; using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace annotation_ctrl_demo { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { private CPDFViewer pdfViewer = new CPDFViewer(); CPDFAnnotationControl pdfAnnotationControl = null; public MainWindow() { InitializeComponent(); TitleBarControl.Loaded += TitleBarControl_Loaded; AnnotationBarControl.Loaded += AnnotationBarControl_Loaded; ModeSelectorBarControl.ShowBOTAEvent += ModeSelectorBarControl_ShowBOTAEvent; } private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e) { 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 }; AnnotationBarControl.InitAnnotationBar(annotationProperties); AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged; AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel; } private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e) { pdfAnnotationControl.AnnotationCancel(); ExpandPropertyPanel((sender as ToggleButton).IsChecked == true); } /// /// 注释菜单选项变换时 /// 1、注释属性面板变换,注册属性变换的监听 /// 2、PDFViewerControl属性设置变换 /// /// /// private void AnnotationBarControl_AnnotationPropertyChanged(object sender, AnnotationType e) { UIElement propertyPanel = GetPropertyPanel(); pdfAnnotationControl = new CPDFAnnotationControl(); pdfAnnotationControl.InitWithPDFViewer(pdfViewer); pdfAnnotationControl.LoadAnnotationPanel(e); SetPropertyPanel(pdfAnnotationControl); ExpandPropertyPanel((sender as ToggleButton).IsChecked == true); } private void ModeSelectorBarControl_ShowBOTAEvent(object sender, EventArgs e) { UIElement botaTool = GetBotaTool(); if (botaTool == null) { CPDFBOTABarControl pdfBOTABarControl = new CPDFBOTABarControl(BOTATools.Outline | BOTATools.Thumbnail | BOTATools.Annotation | BOTATools.Search | BOTATools.Bookmark); if (pdfViewer != null && pdfViewer.Document != null) { pdfBOTABarControl.InitWithPDFViewer(pdfViewer); } SetBotaTool(pdfBOTABarControl); } ExpandTool(ModeSelectorBarControl.BOTABarIsShowing == true); } /// /// 加载文档 /// private void LoadDocument() { pdfViewer.Load(); PDFGrid.Child = pdfViewer; UIElement currentBotaTool = GetBotaTool(); if (currentBotaTool is CPDFBOTABarControl) { ((CPDFBOTABarControl)currentBotaTool).InitWithPDFViewer(pdfViewer); } } private void TitleBarControl_OpenFileEvent(object sender, string filePath) { pdfViewer?.CloseDocument(); pdfViewer?.InitDocument(filePath); LoadDocument(); } private void TitleBarControl_Loaded(object sender, RoutedEventArgs e) { TitleBarControl.OpenFileEvent += TitleBarControl_OpenFileEvent; } private UIElement GetPropertyPanel() { return PropertyPanelContainer.Child; } private UIElement GetBotaTool() { return BotaToolContainer.Child; } private void SetPropertyPanel(UIElement propertyPanel) { PropertyPanelContainer.Child = propertyPanel; } private void SetBotaTool(UIElement newChild) { BotaToolContainer.Child = newChild; } /// /// 展开Property工具 /// /// private void ExpandPropertyPanel(bool isExpand) { if (isExpand) { BodyGrid.ColumnDefinitions[4].Width = new GridLength(300); } else { BodyGrid.ColumnDefinitions[4].Width = new GridLength(0); } PropertyPanelContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed; PropertyPanelSplitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed; } /// /// 展开Bota工具 /// /// private void ExpandTool(bool isExpand) { if (isExpand) { BodyGrid.ColumnDefinitions[0].Width = new GridLength(300); } else { BodyGrid.ColumnDefinitions[0].Width = new GridLength(0); } BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed; BotaSplitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed; } } }