123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- 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
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- 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<string, object> 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();
- }
- /// <summary>
- /// 搜索工具点击处理
- /// </summary>
- 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);
- }
- /// <summary>
- /// 缩略图列表点击处理
- /// </summary>
- 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);
- }
- /// <summary>
- /// 书签列表点击处理
- /// </summary>
- 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);
- }
- /// <summary>
- /// 大纲列表处理
- /// </summary>
- 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);
- }
- /// <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 ExpandBotaTool(bool isExpand)
- {
- BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
- Splitter.Visibility=isExpand ? Visibility.Visible : Visibility.Collapsed;
- }
- /// <summary>
- /// 清除左边工具栏状态
- /// </summary>
- 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)
- {
- }
- }
- }
- }
- }
|