123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- 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,
- }
- /// <summary>
- /// BOTABarControl.xaml 的交互逻辑
- /// </summary>
- public partial class CPDFBOTABarControl : UserControl
- {
- private CPDFViewer pdfViewer;
- private ToggleButton bookmarkButton;
- private ToggleButton outlineButton;
- private ToggleButton thumbnailButton;
- private ToggleButton annotButton;
- private ToggleButton searchButton;
- private CPDFSearchControl searchControl;
- public CPDFBOTABarControl(BOTATools botaTools)
- {
- InitializeComponent();
- AddBOTAContent(botaTools);
- }
- public CPDFBOTABarControl()
- {
- InitializeComponent();
- }
- public void InitWithPDFViewer(CPDFViewer pdfViewer)
- {
- this.pdfViewer = pdfViewer;
- UIElement currentBotaTool = GetBotaTool();
- if (currentBotaTool is CPDFSearchControl)
- {
- ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
- }
- if (currentBotaTool is CPDFThumbnailControl)
- {
- ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
- ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
- ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
- }
- if (currentBotaTool is CPDFBookmarkControl)
- {
- ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
- ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
- }
- if (currentBotaTool is CPDFOutlineControl)
- {
- ((CPDFOutlineControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
- }
- if (currentBotaTool is CPDFAnnotationListControl)
- {
- CPDFAnnotationListControl annotListControl= currentBotaTool as CPDFAnnotationListControl;
- annotListControl.InitWithPDFViewer(pdfViewer);
- annotListControl.LoadAnnotationList();
- }
- }
- public void AddBOTAContent(BOTATools botaTools)
- {
- var brush = (Brush)FindResource("btn.bg.bota");
- while (botaTools > 0)
- {
- if ((botaTools & BOTATools.Thumbnail) > 0)
- {
- thumbnailButton = new ToggleButton();
- thumbnailButton.Background = brush;
- Geometry thumbnailGeometry = Geometry.Parse("M1 0.25H0.25V1V15V15.75H1H4V16.5V18H5.5H16.5H18V16.5V5.5V4H16.5H15.75V1V0.25H15H1ZM14.25 4V1.75H1.75V14.25H4V5.5V4H5.5H14.25ZM5.5 15.75V16.5H16.5V5.5H5.5V14.25V15.75Z");
- Path path = new Path
- {
- Width = 20,
- Height = 20,
- Data = thumbnailGeometry,
- Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
- };
- thumbnailButton.Height = 40;
- thumbnailButton.Width = 65;
- thumbnailButton.BorderThickness = new Thickness(0);
- thumbnailButton.Style = FindResource("ToggleButtonStyle") as Style;
- thumbnailButton.Content = path;
-
- BOTABarTitleGrid.Children.Add(thumbnailButton);
- thumbnailButton.Click += ThumbnailButton_Click;
- botaTools -= BOTATools.Thumbnail;
- }
- else if ((botaTools & BOTATools.Outline) > 0)
- {
- outlineButton = new ToggleButton();
- outlineButton.Background = brush;
- Geometry outlineGeometry = Geometry.Parse(" M4.5 1.5H1.5V4.5H4.5V1.5ZM4.5 6.5H1.5V9.5H4.5V6.5ZM1.5 11.5H4.5V14.5H1.5V11.5ZM15.5 3.75V2.25H6.5V3.75H15.5ZM15.5 7.25V8.75H6.5V7.25H15.5ZM15.5 13.75V12.25H6.5V13.75H15.5Z");
- Path path = new Path
- {
- Width = 20,
- Height = 20,
- Data = outlineGeometry,
- Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
- };
- outlineButton.Height = 40;
- outlineButton.Width = 65;
- outlineButton.BorderThickness= new Thickness(0);
- outlineButton.Style = FindResource("ToggleButtonStyle") as Style;
- outlineButton.Content = path;
-
- BOTABarTitleGrid.Children.Add(outlineButton);
- outlineButton.Click += OutlineButton_Click; ;
- botaTools -= BOTATools.Outline;
- }
- else if ((botaTools & BOTATools.Bookmark) > 0)
- {
- bookmarkButton = new ToggleButton();
- bookmarkButton.Background = brush;
- Geometry bookmarkGeometry = Geometry.Parse("M6.1221 9.85217L1.25 12.6942V0.75H11.75V12.6942L6.8779 9.85217L6.5 9.63172L6.1221 9.85217Z");
- Path path = new Path
- {
- Width = 20,
- Height = 20,
- Data = bookmarkGeometry,
- Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
- };
- bookmarkButton.Height = 40;
- bookmarkButton.Width = 65;
- bookmarkButton.BorderThickness = new Thickness(0);
- bookmarkButton.Style = FindResource("ToggleButtonStyle") as Style;
- bookmarkButton.Content = path;
-
- BOTABarTitleGrid.Children.Add(bookmarkButton);
- bookmarkButton.Click += BookmarkButton_Click;
- botaTools -= BOTATools.Bookmark;
- }
- else if ((botaTools & BOTATools.Annotation) > 0)
- {
- annotButton = new ToggleButton();
- annotButton.Background = brush;
- Geometry thumbnailGeometry = Geometry.Parse("M16 0H1.6L0 14H14.4L16 0ZM8.34885 1.8143L12.0693 11.1154H13.2692V12.1154H10.1923V11.1154H10.9922L10.2384 9.23077H5.53048L4.77689 "+
- "11.1154H5.57692V12.1154H2.5V11.1154H3.69995L7.42038 1.8143H8.34885ZM7.884 3.345L9.83837 8.23077H5.93035L7.884 3.345Z");
- Path path = new Path
- {
- Width = 20,
- Height = 20,
- Data = thumbnailGeometry,
- Fill = (Brush)FindResource("btn.logo.bota")
- };
- annotButton.Height = 40;
- annotButton.Width = 65;
- annotButton.BorderThickness = new Thickness(0);
- annotButton.Style = FindResource("ToggleButtonStyle") as Style;
- annotButton.Content = path;
-
- 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(
- "M4.9284 11.1635C3.06851 9.30364 3.06851 6.28816 4.9284 4.42827C6.78829 2.56838 9.80377 2.56838 11.6637 4.42827C13.5235 6.28816 13.5235 9.30364 11.6637 11.1635C9.80377 "+
- "13.0234 6.78829 13.0234 4.9284 11.1635ZM3.86774 3.36761C1.42207 5.81329 1.42207 9.77851 3.86774 12.2242C6.13397 14.4904 9.70494 14.6567 12.1626 12.723L14.3875 "+
- "14.9479C14.6804 15.2408 15.1552 15.2408 15.4481 14.9479C15.741 14.655 15.741 14.1801 15.4481 13.8872L13.2233 11.6624C15.1568 9.20466 14.9905 5.63379 12.7243 "+
- "3.36761C10.2786 0.921935 6.31342 0.921935 3.86774 3.36761Z");
- Path path = new Path
- {
- Width = 20,
- Height = 20,
- Data = thumbnailGeometry,
- Fill = (Brush)FindResource("btn.logo.bota"),
- };
- searchButton.Height = 40;
- searchButton.Width = 65;
- searchButton.BorderThickness = new Thickness(0);
- searchButton.Style = FindResource("ToggleButtonStyle") as Style;
- searchButton.Content = path;
-
- BOTABarTitleGrid.Children.Add(searchButton);
- searchButton.Click += SearchButton_Click;
- botaTools -= BOTATools.Search;
- }
- }
- }
- /// <summary>
- /// 获取Bota工具
- /// </summary>
- /// <returns></returns>
- private UIElement GetBotaTool()
- {
- return BotaToolContainer.Child;
- }
- private void SetBotaTool(UIElement newChild)
- {
- BotaToolContainer.Child = newChild;
- }
- /// <summary>
- /// 展开Bota工具
- /// </summary>
- /// <param name="isExpand"></param>
- private void ExpandTool(bool isExpand)
- {
- BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
- }
- /// <summary>
- /// 清除工具栏状态
- /// </summary>
- 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)
- {
- SelectBotaTool(BOTATools.Search);
- }
- private void AnnotButton_Click(object sender, RoutedEventArgs e)
- {
- SelectBotaTool(BOTATools.Annotation);
- }
- private void ThumbnailButton_Click(object sender, RoutedEventArgs e)
- {
- SelectBotaTool(BOTATools.Thumbnail);
- }
- private void OutlineButton_Click(object sender, RoutedEventArgs e)
- {
- SelectBotaTool(BOTATools.Outline);
- }
- private void BookmarkButton_Click(object sender, RoutedEventArgs e)
- {
- SelectBotaTool(BOTATools.Bookmark);
- }
- public void SelectBotaTool(BOTATools tool)
- {
- UIElement botaTool = GetBotaTool();
- ToggleButton checkBtn = null;
- switch(tool)
- {
- case BOTATools.Thumbnail:
- {
- if(thumbnailButton != null)
- {
- thumbnailButton.IsChecked = true;
- if (botaTool == null || !(botaTool is CPDFThumbnailControl))
- {
- CPDFThumbnailControl thumbnailControl = new CPDFThumbnailControl();
- if (pdfViewer != null && pdfViewer.Document != null)
- {
- thumbnailControl.InitWithPDFViewer(pdfViewer);
- thumbnailControl.LoadThumb();
- }
- SetBotaTool(thumbnailControl);
- checkBtn = thumbnailButton;
- }
- }
- }
- break;
- case BOTATools.Outline:
- {
- if(outlineButton != null)
- {
- outlineButton.IsChecked = true;
- if (botaTool == null || !(botaTool is CPDFOutlineControl))
- {
- CPDFOutlineControl outlineControl = new CPDFOutlineControl();
- if (pdfViewer != null && pdfViewer.Document != null)
- {
- outlineControl.InitWithPDFViewer(pdfViewer);
- }
- SetBotaTool(outlineControl);
- checkBtn = outlineButton;
- }
- }
- }
- break;
- case BOTATools.Bookmark:
- {
- if(bookmarkButton!=null)
- {
- bookmarkButton.IsChecked = true;
- if (botaTool == null || !(botaTool is CPDFBookmarkControl))
- {
- CPDFBookmarkControl pdfBookmarkControl = new CPDFBookmarkControl();
- if (pdfViewer != null && pdfViewer.Document != null)
- {
- pdfBookmarkControl.InitWithPDFViewer(pdfViewer);
- }
- SetBotaTool(pdfBookmarkControl);
- }
- checkBtn = bookmarkButton;
- }
- }
- break;
- case BOTATools.Search:
- {
- if(searchButton!=null)
- {
- searchButton.IsChecked = true;
- if (botaTool == null || !(botaTool is CPDFSearchControl))
- {
- if (searchControl == null)
- {
- searchControl = new CPDFSearchControl();
- if (pdfViewer != null && pdfViewer.Document != null)
- {
- searchControl.InitWithPDFViewer(pdfViewer);
- }
- }
- SetBotaTool(searchControl);
- checkBtn = searchButton;
- }
- }
- }
- break;
- case BOTATools.Annotation:
- {
- if(annotButton!=null)
- {
- annotButton.IsChecked = true;
- if (botaTool == null || !(botaTool is CPDFAnnotationListControl))
- {
- CPDFAnnotationListControl annotationListControl = new CPDFAnnotationListControl();
- if (pdfViewer != null && pdfViewer.Document != null)
- {
- annotationListControl.InitWithPDFViewer(pdfViewer);
- annotationListControl.LoadAnnotationList();
- }
- SetBotaTool(annotationListControl);
- checkBtn = annotButton;
- }
- }
- }
- break;
- default:
- break;
- }
- if(checkBtn!=null)
- {
- ExpandTool(checkBtn.IsChecked == true);
- ClearToolState(checkBtn);
- }
- }
- public void LoadAnnotationList()
- {
- UIElement currentBotaTool = GetBotaTool();
- if (currentBotaTool is CPDFAnnotationListControl)
- {
- ((CPDFAnnotationListControl)currentBotaTool).LoadAnnotationList();
- }
- }
- }
- }
|