using ComPDFKit.PDFDocument;
using compdfkit_tools;
using compdfkit_tools.PDFControl;
using compdfkit_tools.PDFControl;
using compdfkit_tools.PDFControlUI;
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;
namespace viewer_ctrl_demo
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public CPDFViewer pdfViewer;
public MainWindow()
{
InitializeComponent();
TitleBarControl.Loaded += TitleBarControl_Loaded;
LoadDefaultDocument();
}
private void LoadDocument()
{
pdfViewer.Load();
PDFGrid.Child = pdfViewer;
CPDFScalingControl.InitWithPDFViewer(pdfViewer);
CPDFPageTurningControl.InitWithPDFViewer(pdfViewer);
CPDFBrowseModeControl.InitWithPDFViewer(pdfViewer);
CPDFColorModeControl.InitWithPDFViewer(pdfViewer);
UIElement currentBotaTool = GetBotaTool();
if (currentBotaTool is CPDFSearchControl)
{
((CPDFSearchControl)currentBotaTool).SetPDFView(pdfViewer);
}
if (currentBotaTool is CPDFThumbnailControl)
{
((CPDFThumbnailControl)currentBotaTool).SetPDFView(pdfViewer);
((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
((CPDFThumbnailControl)currentBotaTool).LoadThumb();
}
}
private void LoadDefaultDocument()
{
string defaultFilePath = "..\\..\\..\\..\\developer_guide_windows.pdf";
pdfViewer = new CPDFViewer();
pdfViewer.InitDocument(defaultFilePath);
LoadDocument();
}
private void TitleBarControl_Loaded(object sender, RoutedEventArgs e)
{
TitleBarControl.OpenFileEvent += TitleBarControl_OpenFileEvent;
}
private void TitleBarControl_OpenFileEvent(object sender, CPDFViewer e)
{
this.pdfViewer = TitleBarControl.pdfViewer;
LoadDocument();
}
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;
}
}
///
/// 搜索工具点击处理
///
private void SearchToolButton_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.SetPDFView(pdfViewer);
}
SetBotaTool(searchControl);
}
ExpandTool(SearchToolButton.IsChecked == true);
ClearToolState(SearchToolButton);
}
///
/// 缩略图列表点击处理
///
private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
{
UIElement botaTool = GetBotaTool();
if (botaTool == null || !(botaTool is CPDFThumbnailControl))
{
CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
if (pdfViewer != null && pdfViewer.Document != null)
{
thumbControl.SetPDFView(pdfViewer);
thumbControl.LoadThumb();
}
SetBotaTool(thumbControl);
}
ExpandTool(ThumbToolButton.IsChecked == true);
ClearToolState(ThumbToolButton);
}
///
/// 大纲列表处理
///
private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
{
UIElement botaTool = GetBotaTool();
if (botaTool == null||!(botaTool is CPDFOutlineControl))
{
CPDFOutlineUI outlineControl = new CPDFOutlineUI();
if (pdfViewer != null && pdfViewer.Document != null)
{
}
SetBotaTool(outlineControl);
}
ExpandTool(OutlineToolButton.IsChecked == true);
ClearToolState(OutlineToolButton);
}
///
/// 设置Bota工具内容
///
///
private void SetBotaTool(UIElement newChild)
{
BotaToolContainer.Child = newChild;
}
///
/// 获取Bota工具
///
///
private UIElement GetBotaTool()
{
return BotaToolContainer.Child;
}
///
/// 展开Bota工具
///
///
private void ExpandTool(bool isExpand)
{
BotaToolContainer.Width = isExpand ? 200 : 0;
BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
}
///
/// 清除左边工具栏状态
///
private void ClearToolState(UIElement ignoreTool)
{
foreach (UIElement child in BotaSideTool.Children)
{
if (child != ignoreTool && child is ToggleButton buttonTool)
{
buttonTool.IsChecked = false;
}
}
}
private void ShowPDFInfoDialog()
{
CPDFInfoControl pdfInfoControl = new CPDFInfoControl();
pdfInfoControl.PDFDocument = pdfViewer.Document;
Window dialog = new Window
{
Title = "PDF Info", // 设置弹出窗口的标题
Content = pdfInfoControl, // 将UserControl添加到弹出窗口中
Width = 600, // 设置弹出窗口的宽度
Height = 300, // 设置弹出窗口的高度
ResizeMode = ResizeMode.NoResize, // 禁止用户调整弹出窗口的大小
WindowStartupLocation = WindowStartupLocation.CenterScreen, // 将弹出窗口显示在屏幕中央
WindowStyle = WindowStyle.ToolWindow, // 设置弹出窗口的样式
Owner = Application.Current.MainWindow // 将主窗口设置为弹出窗口的拥有者
};
dialog.ShowDialog(); // 显示弹出窗口
}
private void PDFInfoButton_Click(object sender, RoutedEventArgs e)
{
ShowPDFInfoDialog();
}
}
}