using ComPDFKit.PDFDocument; using compdfkit_tools.PDFControlUI; 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 compdfkit_tools.PDFControl { public enum BOTATools { Bookmark = 1 << 0, Outline = 1 << 1, Thumbnail = 1 << 2, Annotation = 1 << 3, Search = 1 << 4, } /// /// BOTABarControl.xaml 的交互逻辑 /// public partial class CPDFBOTABarControl : UserControl { private CPDFViewer pdfViewer; private ToggleButton bookmarkButton; private ToggleButton outlineButton; private ToggleButton thumbnailButton; private ToggleButton annotButton; private ToggleButton searchButton; public CPDFBOTABarControl(BOTATools botaTools) { InitializeComponent(); BOTABarTitleGrid.ColumnDefinitions.Clear(); InitBOTABar(botaTools); AddBOTAContent(botaTools); } public void InitWithPDFViewer(CPDFViewer pdfViewer) { this.pdfViewer = pdfViewer; } public void InitBOTABar(BOTATools botaTools) { foreach (BOTATools item in Enum.GetValues(typeof(BOTATools))) { if ((item & botaTools) > 0) { BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition()); } else if ((item & botaTools) > 0) { BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition()); } else if ((item & botaTools) > 0) { BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition()); } else if ((item & botaTools) > 0) { BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition()); } else if ((item & botaTools) > 0) { BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition()); } } } public void AddBOTAContent(BOTATools botaTools) { int botaCounter = 0; var brush = (Brush)FindResource("btn.bg.bota"); while (botaTools > 0) { if ((botaTools & BOTATools.Bookmark) > 0) { bookmarkButton = new ToggleButton(); bookmarkButton.Background = brush; Geometry bookmarkGeometry = Geometry.Parse("M17.5,1.25 L2.5,1.25 L2.5,19.1421541 L10,15.3425 L17.5,19.1421541 L17.5,1.25 Z M16.25,2.5 L16.25,17.1075 L10,13.9422615 L3.75,17.10625 L3.75,2.5 L16.25,2.5 Z"); Path path = new Path { Width = 20, Height = 20, Data = bookmarkGeometry, Fill = (Brush)FindResource("btn.logo.bota") }; bookmarkButton.Content = path; Grid.SetColumn(bookmarkButton, botaCounter++); BOTABarTitleGrid.Children.Add(bookmarkButton); bookmarkButton.Click += BookmarkButton_Click; botaTools -= BOTATools.Bookmark; } else if ((botaTools & BOTATools.Outline) > 0) { outlineButton = new ToggleButton(); outlineButton.Background = brush; Geometry outlineGeometry = Geometry.Parse(" M5,14.375 L5,18.125 L1.25,18.125 L1.25,14.375 L5,14.375 Z M18.75,15.625 L18.75,16.875 L7.5,16.875 L7.5,15.625 L18.75,15.625 Z M5,8.125 L5,11.875 L1.25,11.875 L1.25,8.125 L5,8.125 Z M18.75,9.375 L18.75,10.625 L7.5,10.625 L7.5,9.375 L18.75,9.375 Z M5,1.875 L5,5.625 L1.25,5.625 L1.25,1.875 L5,1.875 Z M18.75,3.125 L18.75,4.375 L7.5,4.375 L7.5,3.125 L18.75,3.125 Z"); Path path = new Path { Width = 20, Height = 20, Data = outlineGeometry, Fill = (Brush)FindResource("btn.logo.bota"), }; outlineButton.Content = path; Grid.SetColumn(outlineButton, botaCounter++); BOTABarTitleGrid.Children.Add(outlineButton); outlineButton.Click += OutlineButton_Click; ; botaTools -= BOTATools.Outline; } else if ((botaTools & BOTATools.Thumbnail) > 0) { thumbnailButton = new ToggleButton(); thumbnailButton.Background = brush; Geometry thumbnailGeometry = Geometry.Parse("M15 9V17H5L5 3L9 3V7C9 8.10457 9.89543 9 11 9H15ZM14.1716 7H11V3.82843L12.5858 5.41421L14.1716 7ZM17 8V17C17 18.1046 16.1046 19 15 19H5C3.89543 19 3 18.1046 3 17V3C3 1.89543 3.89543 1 5 1H10H10.1716C10.702 1 11.2107 1.21071 11.5858 1.58579L14 4L16.4142 6.41421C16.7893 6.78929 17 7.29799 17 7.82843V8Z"); Path path = new Path { Width = 20, Height = 20, Data = thumbnailGeometry, Fill = (Brush)FindResource("btn.logo.bota"), }; thumbnailButton.Content = path; Grid.SetColumn(thumbnailButton, botaCounter++); BOTABarTitleGrid.Children.Add(thumbnailButton); thumbnailButton.Click += ThumbnailButton_Click; botaTools -= BOTATools.Thumbnail; } else if ((botaTools & BOTATools.Annotation) > 0) { annotButton = new ToggleButton(); annotButton.Background = brush; Geometry thumbnailGeometry = Geometry.Parse("M20,1.25 L18,18.75 L0,18.75 L2,1.25 L20,1.25 Z M10.4360672,3.51788083 L9.2754713,3.51788083 L4.624,15.1438808 L3.125,15.1442308 L3.125,16.3942308 L6.97115385,16.3942308 L6.97115385,15.1442308 L5.971,15.1438808 L6.913,12.7878808 L12.798,12.7878808 L13.74,15.1438808 L12.7403846,15.1442308 L12.7403846,16.3942308 L16.5865385,16.3942308 L16.5865385,15.1442308 L15.086,15.1438808 L10.4360672,3.51788083 Z M9.855,5.43125 L12.298,11.5378808 L7.413,11.5378808 L9.855,5.43125 Z"); Path path = new Path { Width = 20, Height = 20, Data = thumbnailGeometry, Fill = (Brush)FindResource("btn.logo.bota"), }; annotButton.Content = path; Grid.SetColumn(annotButton, botaCounter++); BOTABarTitleGrid.Children.Add(annotButton); annotButton.Click += AnnotButton_Click; botaTools -= BOTATools.Annotation; } else if ((botaTools & BOTATools.Search) > 0) { searchButton = new ToggleButton(); searchButton.Background = brush; Geometry thumbnailGeometry = Geometry.Parse(" M14 8.5C14 11.5376 11.5376 14 8.5 14C5.46243 14 3 11.5376 3 8.5C3 5.46243 5.46243 3 8.5 3C11.5376 3 14 5.46243 14 8.5ZM13.0492 14.4633C11.7874 15.4274 10.2106 16 8.5 16C4.35786 16 1 12.6421 1 8.5C1 4.35786 4.35786 1 8.5 1C12.6421 1 16 4.35786 16 8.5C16 10.2105 15.4274 11.7873 14.4634 13.0491L18.4572 17.0429L17.043 18.4571L13.0492 14.4633Z"); Path path = new Path { Width = 20, Height = 20, Data = thumbnailGeometry, Fill = (Brush)FindResource("btn.logo.bota"), }; searchButton.Content = path; Grid.SetColumn(searchButton, botaCounter++); BOTABarTitleGrid.Children.Add(searchButton); searchButton.Click += SearchButton_Click; botaTools -= BOTATools.Search; } } } /// /// 获取Bota工具 /// /// private UIElement GetBotaTool() { return BotaToolContainer.Child; } private void SetBotaTool(UIElement newChild) { BotaToolContainer.Child = newChild; } /// /// 展开Bota工具 /// /// private void ExpandTool(bool isExpand) { BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed; } /// /// 清除工具栏状态 /// private void ClearToolState(UIElement ignoreTool) { foreach (UIElement child in BOTABarTitleGrid.Children) { if (child != ignoreTool && child is ToggleButton buttonTool) { buttonTool.IsChecked = false; } } } private void SearchButton_Click(object sender, RoutedEventArgs e) { UIElement botaTool = GetBotaTool(); if (botaTool == null || !(botaTool is CPDFSearchControl)) { CPDFSearchControl searchControl = new CPDFSearchControl(); if (pdfViewer != null && pdfViewer.Document != null) { searchControl.InitWithPDFViewer(pdfViewer); } SetBotaTool(searchControl); } ExpandTool(searchButton.IsChecked == true); ClearToolState(searchButton); } private void AnnotButton_Click(object sender, RoutedEventArgs e) { } private void ThumbnailButton_Click(object sender, RoutedEventArgs e) { UIElement botaTool = GetBotaTool(); if (botaTool == null || !(botaTool is CPDFThumbnailControl)) { CPDFThumbnailControl thumbnailControl = new CPDFThumbnailControl(); if (pdfViewer != null && pdfViewer.Document != null) { thumbnailControl.InitWithPDFViewer(pdfViewer); thumbnailControl.LoadThumb(); } SetBotaTool(thumbnailControl); } ExpandTool(thumbnailButton.IsChecked == true); ClearToolState(thumbnailButton); } private void OutlineButton_Click(object sender, RoutedEventArgs e) { UIElement botaTool = GetBotaTool(); if (botaTool == null || !(botaTool is CPDFOutlineControl)) { CPDFOutlineControl outlineControl = new CPDFOutlineControl(); if (pdfViewer != null && pdfViewer.Document != null) { outlineControl.InitWithPDFViewer(pdfViewer); } SetBotaTool(outlineControl); } ExpandTool(outlineButton.IsChecked == true); ClearToolState(outlineButton); } private void BookmarkButton_Click(object sender, RoutedEventArgs e) { UIElement botaTool = GetBotaTool(); if (botaTool == null || !(botaTool is CPDFBookmarkControl)) { CPDFBookmarkControl pdfBookmarkControl = new CPDFBookmarkControl(); if (pdfViewer != null && pdfViewer.Document != null) { pdfBookmarkControl.InitWithPDFViewer(pdfViewer); } SetBotaTool(pdfBookmarkControl); } ExpandTool(bookmarkButton.IsChecked == true); ClearToolState(bookmarkButton); } } }