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(); private Dictionary getAnnotationFromTag; public MainWindow() { InitializeComponent(); TitleBarControl.Loaded += TitleBarControl_Loaded; AnnotationBarControl.Loaded += AnnotationBarControl_Loaded; ModeSelectorBarControl.ShowBOTAEvent += ModeSelectorBarControl_ShowBOTAEvent; InitDictionary(); } private void InitDictionary() { getAnnotationFromTag = new Dictionary(); getAnnotationFromTag.Clear(); getAnnotationFromTag.Add("Highlight", AnnotationType.Highlight); getAnnotationFromTag.Add("Underline", AnnotationType.Underline); getAnnotationFromTag.Add("Strikeout", AnnotationType.Strikeout); getAnnotationFromTag.Add("Squiggly", AnnotationType.Squiggly); getAnnotationFromTag.Add("Rect", AnnotationType.Rect); getAnnotationFromTag.Add("Round", AnnotationType.Round); getAnnotationFromTag.Add("StraightLine", AnnotationType.StraightLine); } private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e) { 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 }; AnnotationBarControl.InitAnnotationBar(annotationProperties); AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged; } /// /// 注释菜单选项变换时 /// 1、注释属性面板变换,注册属性变换的监听 /// 2、PDFViewerControl属性设置变换 /// /// /// private void AnnotationBarControl_AnnotationPropertyChanged(object sender, string e) { UIElement propertyPanel = GetPropertyPanel(); CPDFAnnotationControl pdfAnnotationControl = new CPDFAnnotationControl(); pdfAnnotationControl.PropertyChanged += PDFAnnotationControl_PropertyChanged; pdfAnnotationControl.LoadAnnotationPanel(getAnnotationFromTag[e]); SetPropertyPanel(pdfAnnotationControl); ExpandPropertyPanel((sender as ToggleButton).IsChecked == true); CPDFAnnotationData pdfAnnotationData = null; pdfAnnotationData = pdfAnnotationControl.GetAnnotationData(); CPDFViewerControl.ChangeAnnotationMode(pdfAnnotationData); } private void PDFAnnotationControl_PropertyChanged(object sender, CPDFAnnotationData e) { CPDFViewerControl.ChangeAnnotationMode(e); } 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(); CPDFViewerControl.InitWithPDFView(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; } } }