123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- 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;
- using viewer_ctrl_demo.Bota;
- namespace viewer_ctrl_demo
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public CPDFViewer pdfViewer;
- public MainWindow()
- {
- InitializeComponent();
- AddPreViewModes();
- AddMenuItemToLogoButton();
- }
- public void AddMenuItemToLogoButton()
- {
- // 创建三个 MenuItem,并设置 Header 属性
- MenuItem openFileItem = new MenuItem();
- openFileItem.Header = "打开文件";
- openFileItem.Click += MenuItem_Click;
- openFileItem.Tag = "OpenFile";
- MenuItem goToWebItem = new MenuItem();
- goToWebItem.Header = "了解ComPDFKit SDK";
- goToWebItem.Click += MenuItem_Click;
- MenuItem aboutItem = new MenuItem();
- aboutItem.Header = "关于";
- aboutItem.Click += MenuItem_Click;
- // 将 MenuItem 添加到 ContextMenu 中
- logoMenu.Items.Add(openFileItem);
- logoMenu.Items.Add(goToWebItem);
- logoMenu.Items.Add(aboutItem);
- }
- private void MenuItem_Click(object sender, RoutedEventArgs e)
- {
- var menuItem = sender as MenuItem;
- if (menuItem.Tag.ToString() == "OpenFile")
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
- if (openFileDialog.ShowDialog() == true)
- {
- pdfViewer = new CPDFViewer();
- string selectedFilePath = openFileDialog.FileName;
- pdfViewer.InitDocument(selectedFilePath);
- if (pdfViewer.Document!=null&&
- pdfViewer.Document.ErrorType == ComPDFKit.PDFDocument.CPDFDocumentError.CPDFDocumentErrorSuccess)
- {
- pdfViewer.Load();
- PDFGrid.Children.Add(pdfViewer);
- }
- }
- }
- }
- public void AddPreViewModes()
- {
- // 创建ComboBoxItem元素
- ComboBoxItem ViewerMode = new ComboBoxItem();
- ViewerMode.Content = "预览模式";
- ViewerMode.IsSelected = true; // 将A设置为首选项
- // 添加ComboBoxItem元素到ComboBox中
- previewModeComboBox.Items.Add(ViewerMode);
- }
- private void displaySettingsToggleButton_Click(object sender, RoutedEventArgs e)
- {
- if (popup.IsOpen)
- {
- popup.IsOpen = false;
- }
- else
- {
- popup.IsOpen = true;
- }
- }
- private void logoButton_Click(object sender, RoutedEventArgs e)
- {
- Button button = sender as Button;
- if (button.ContextMenu.IsOpen)
- {
- button.ContextMenu.IsOpen = false;
- }
- else
- {
- button.ContextMenu.IsOpen = true;
- }
- }
- /// <summary>
- /// 搜索工具点击处理
- /// </summary>
- private void SearchToolButton_Click(object sender, RoutedEventArgs e)
- {
- UIElement botaTool = GetBotaTool();
- if (botaTool == null || !(botaTool is PDFSearch))
- {
- PDFSearch searchControl = new PDFSearch();
- SetBotaTool(searchControl);
- }
- ExpandTool(SearchToolButton.IsChecked == true);
- }
- /// <summary>
- /// 设置Bota工具内容
- /// </summary>
- /// <param name="newChild"></param>
- private void SetBotaTool(UIElement newChild)
- {
- BotaToolContainer.Child = newChild;
- }
- /// <summary>
- /// 获取Bota工具
- /// </summary>
- /// <returns></returns>
- private UIElement GetBotaTool()
- {
- return BotaToolContainer.Child;
- }
- /// <summary>
- /// 展开Bota工具
- /// </summary>
- /// <param name="isExpand"></param>
- private void ExpandTool(bool isExpand)
- {
- BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
- BotaToolContainer.Width = isExpand ? 200 : 0;
- }
- }
- }
|