using compdfkit_tools.PDFControl;
using compdfkit_tools.PDFControlUI;
using ComPDFKitViewer.PdfViewer;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace viewer_ctrl_demo
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private CPDFViewer pdfViewer;
public MainWindow()
{
InitializeComponent();
TitleBarControl.Loaded += TitleBarControl_Loaded;
LoadDefaultDocument();
}
private void LoadDocument()
{
pdfViewer.Load();
PDFGrid.Child = pdfViewer;
CPDFScalingControl.InitWithPDFViewer(pdfViewer);
CPDFPageTurningControl.InitWithPDFViewer(pdfViewer);
CPDFViewModeControl.InitWithPDFViewer(pdfViewer);
CPDFDrawModeControl.InitWithPDFViewer(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();
}
}
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, string filePath)
{
pdfViewer?.CloseDocument();
pdfViewer = new CPDFViewer();
pdfViewer.InitDocument(filePath);
LoadDocument();
}
///
/// 搜索工具点击处理
///
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.InitWithPDFViewer(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.InitWithPDFViewer(pdfViewer);
thumbControl.LoadThumb();
}
SetBotaTool(thumbControl);
}
ExpandTool(ThumbToolButton.IsChecked == true);
ClearToolState(ThumbToolButton);
}
///
/// 书签列表点击处理
///
private void BookmarkToolButtonn_Click(object sender, RoutedEventArgs e)
{
UIElement botaTool = GetBotaTool();
if (botaTool == null || !(botaTool is CPDFBookmarkControl))
{
CPDFBookmarkControl bookmarkControl = new CPDFBookmarkControl();
if (pdfViewer != null && pdfViewer.Document != null)
{
bookmarkControl.InitWithPDFViewer(pdfViewer);
bookmarkControl.LoadBookmark();
}
SetBotaTool(bookmarkControl);
}
ExpandTool(BookmarkToolButton.IsChecked == true);
ClearToolState(BookmarkToolButton);
}
///
/// 大纲列表处理
///
private void OutlineToolButton_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(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)
{
if(isExpand)
{
BodyGrid.ColumnDefinitions[1].Width = new GridLength(300);
}
else
{
BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
}
BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
Splitter.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 BotaButton_Click(object sender, RoutedEventArgs e)
{
UIElement botaTool = GetBotaTool();
if (botaTool == null)
{
CPDFBOTABarControl pdfBOTABarControl = new CPDFBOTABarControl(BOTATools.Bookmark|BOTATools.Outline|BOTATools.Thumbnail|BOTATools.Search);
if (pdfViewer != null && pdfViewer.Document != null)
{
pdfBOTABarControl.InitWithPDFViewer(pdfViewer);
}
SetBotaTool(pdfBOTABarControl);
}
ExpandTool(BotaButton.IsChecked == true);
ClearToolState(BotaButton);
}
private void ShowPDFInfoDialog()
{
CPDFInfoControl pdfInfoControl = new CPDFInfoControl();
pdfInfoControl.InitWithPDFViewer(pdfViewer);
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();
}
}
}