using ComPDFKit.PDFDocument; using compdfkit_tools.Helper; using compdfkit_tools.PDFControl; using compdfkit_tools.PDFControlUI; using ComPDFKitViewer.PdfViewer; using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Annotations; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; namespace viewer_ctrl_demo { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private PDFViewControl passwordViewer; private PDFViewControl pdfViewControl; private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; DataContext = this; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { LoadDefaultDocument(); BindZoomLevel(); } private void BindZoomLevel() { foreach (double zoomLevel in zoomLevelList) { ComboBoxItem zoomItem=new ComboBoxItem(); zoomItem.Content =zoomLevel+"%"; ZoomComboBox.Items.Add(zoomItem); } } private void LoadDocument() { pdfViewControl.PDFView?.Load(); PDFGrid.Child = pdfViewControl; pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged; pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged; PasswordUI.Closed += PasswordUI_Closed; PasswordUI.Canceled += PasswordUI_Canceled; PasswordUI.Confirmed += PasswordUI_Confirmed; UIElement currentBotaTool = GetBotaTool(); if (currentBotaTool is CPDFSearchControl) { ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView); } if (currentBotaTool is CPDFThumbnailControl) { ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView); ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false; ((CPDFThumbnailControl)currentBotaTool).LoadThumb(); } if (currentBotaTool is CPDFBookmarkControl) { ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView); ((CPDFBookmarkControl)currentBotaTool).LoadBookmark(); } ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%"; } private void PasswordUI_Confirmed(object sender, string e) { if(passwordViewer!=null && passwordViewer.PDFView!=null && passwordViewer.PDFView.Document!=null) { passwordViewer.PDFView.Document.UnlockWithPassword(e); if (passwordViewer.PDFView.Document.IsLocked == false) { PasswordUI.SetShowError("", Visibility.Collapsed); PasswordUI.ClearPassword(); PasswordUI.Visibility = Visibility.Collapsed; PopupBorder.Visibility = Visibility.Collapsed; pdfViewControl = passwordViewer; LoadDocument(); } else { PasswordUI.SetShowError("error", Visibility.Visible); } } } private void PasswordUI_Canceled(object sender, EventArgs e) { PopupBorder.Visibility = Visibility.Collapsed; PasswordUI.Visibility = Visibility.Collapsed; } private void PasswordUI_Closed(object sender, EventArgs e) { PopupBorder.Visibility = Visibility.Collapsed; PasswordUI.Visibility = Visibility.Collapsed; } private void PdfViewer_InfoChanged(object sender, KeyValuePair e) { if(e.Key== "PageNum") { PageRangeText.Text = string.Format("{0}/{1}", e.Value, pdfViewControl.PDFView.Document.PageCount); } if(e.Key=="Zoom") { ZoomTextBox.Text = string.Format("{0}", (int)((double)e.Value * 100)) + "%"; } } private double CheckZoomLevel(double zoom, bool IsGrowth) { double standardZoom = 100; if (zoom <= 0.01) { return 0.01; } if (zoom >= 10) { return 10; } zoom *= 100; for (int i = 0; i < zoomLevelList.Length - 1; i++) { if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth) { standardZoom = zoomLevelList[i + 1]; break; } if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth) { standardZoom = zoomLevelList[i]; break; } } return standardZoom / 100; } private void LoadDefaultDocument() { string defaultFilePath = "developer_guide_windows.pdf"; pdfViewControl = new PDFViewControl(); pdfViewControl.PDFView.InitDocument(defaultFilePath); LoadDocument(); } /// /// 搜索工具点击处理 /// private void SearchToolButton_Click(object sender, RoutedEventArgs e) { UIElement botaTool = GetBotaTool(); if (botaTool == null || !(botaTool is CPDFSearchControl)) { CPDFSearchControl searchControl = new CPDFSearchControl(); if (pdfViewControl != null && pdfViewControl.PDFView!=null && pdfViewControl.PDFView.Document != null) { searchControl.InitWithPDFViewer(pdfViewControl.PDFView); } SetBotaTool(searchControl); } ExpandBotaTool(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 (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { thumbControl.InitWithPDFViewer(pdfViewControl.PDFView); thumbControl.LoadThumb(); } SetBotaTool(thumbControl); } ExpandBotaTool(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 (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { bookmarkControl.InitWithPDFViewer(pdfViewControl.PDFView); bookmarkControl.LoadBookmark(); } SetBotaTool(bookmarkControl); } ExpandBotaTool(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 (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { outlineControl.InitWithPDFViewer(pdfViewControl.PDFView); } SetBotaTool(outlineControl); } ExpandBotaTool(OutlineToolButton.IsChecked == true); ClearToolState(OutlineToolButton); } /// /// 设置Bota工具内容 /// /// private void SetBotaTool(UIElement newChild) { BotaToolContainer.Child = newChild; } /// /// 获取Bota工具 /// /// private UIElement GetBotaTool() { return BotaToolContainer.Child; } /// /// 展开Bota工具 /// /// private void ExpandBotaTool(bool isExpand) { 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 ToolExpand_Click(object sender, RoutedEventArgs e) { ToggleButton expandBtn=sender as ToggleButton; if(expandBtn != null) { bool isExpand = expandBtn.IsChecked == true; ExpandLeftPanel(isExpand); if (isExpand) { ThumbToolButton.IsChecked = isExpand; ThumbToolButton_Click(ThumbToolButton, new RoutedEventArgs()); } } } private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e) { ExpandLeftPanel(true); SearchToolButton.IsChecked=true; SearchToolButton_Click(SearchToolButton, new RoutedEventArgs()); } private void ExpandLeftPanel(bool isExpand) { ExpandToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed; Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed; if (isExpand) { BodyGrid.ColumnDefinitions[0].Width = new GridLength(260); BodyGrid.ColumnDefinitions[1].Width = new GridLength(15); } else { BodyGrid.ColumnDefinitions[0].Width = new GridLength(0); BodyGrid.ColumnDefinitions[1].Width = new GridLength(0); } } private void Border_MouseEnter(object sender,MouseEventArgs e) { FloatPageTool.Visibility = Visibility.Visible; } private void PDFGrid_MouseMove(object sender, MouseEventArgs e) { FloatPageTool.Visibility = Visibility.Collapsed; } private void ViewSettingBtn_Click(object sender, RoutedEventArgs e) { ToggleButton toggleButton=sender as ToggleButton; if(toggleButton != null) { if(toggleButton.IsChecked==true) { CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl(); displayPanel.InitWithPDFViewer(pdfViewControl.PDFView); PropertyContainer.Child = displayPanel; PropertyContainer.Visibility = Visibility.Visible; } else { PropertyContainer.Child = null; PropertyContainer.Visibility = Visibility.Collapsed; } } } private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem; if (selectItem!=null && selectItem.Content!=null && pdfViewControl != null) { if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double zoomLevel)) { pdfViewControl.PDFView.Zoom(zoomLevel/100D); ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%"; } } } private void ZoomInBtn_Click(object sender, RoutedEventArgs e) { if (pdfViewControl != null) { double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true); pdfViewControl.PDFView.Zoom(newZoom); ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%"; } } private void ZoomOutBtn_Click(object sender, RoutedEventArgs e) { if(pdfViewControl!=null) { double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false); pdfViewControl.PDFView.Zoom(newZoom); ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%"; } } private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1); } private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1); } private void PageInfoBtn_Click(object sender, RoutedEventArgs e) { PasswordUI.Visibility=Visibility.Collapsed; FileInfoUI.Visibility = Visibility.Visible; FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView); PopupBorder.Visibility = Visibility.Visible; } private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e) { PopupBorder.Visibility = Visibility.Collapsed; } private void OpenFile_Click(object sender, RoutedEventArgs e) { try { string filePath = CommonHelper.GetFilePathOrEmpty(); if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null) { passwordViewer = new PDFViewControl(); passwordViewer.PDFView.InitDocument(filePath); if(passwordViewer.PDFView.Document == null) { MessageBox.Show("Open File Failed"); return; } if (passwordViewer.PDFView.Document.IsLocked) { PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath)+ " password encrypted."); PasswordUI.ClearPassword(); PopupBorder.Visibility=Visibility.Visible; PasswordUI.Visibility=Visibility.Visible; } else { pdfViewControl = passwordViewer; LoadDocument(); } } } catch(Exception ex) { } } private void SaveFileBtn_Click(object sender, RoutedEventArgs e) { if(pdfViewControl!=null && pdfViewControl.PDFView!=null &&pdfViewControl.PDFView.Document!=null) { try { CPDFDocument pdfDoc = pdfViewControl.PDFView.Document; if (pdfDoc.WriteToLoadedPath()) { return; } SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "(*.pdf)|*.pdf"; saveDialog.DefaultExt = ".pdf"; saveDialog.OverwritePrompt = true; if (saveDialog.ShowDialog() == true) { pdfDoc.WriteToFilePath(saveDialog.FileName); } } catch (Exception ex) { } } } } }