using ComPDFKit.PDFDocument; using compdfkit_tools.Data; using compdfkit_tools.Helper; using compdfkit_tools.PDFControl; using compdfkit_tools.PDFControlUI; using ComPDFKitViewer; 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; using System.Windows.Media.Animation; namespace viewer_ctrl_demo { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private PDFViewControl passwordViewer; private PDFViewControl pdfViewControl; private CPDFAnnotationControl pdfAnnotationControl = null; private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; public MainWindow() { InitializeComponent(); DataContext = this; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { pdfAnnotationControl = new CPDFAnnotationControl(); 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; pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler; 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(); } ViewSettingBtn.IsChecked = false; PropertyContainer.Child = null; PropertyContainer.Visibility = Visibility.Collapsed; ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%"; InitialPDFViewControl(pdfViewControl); } private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.AnnotCommandArgs e) { switch (e.CommandType) { case CommandType.Context: e.Handle = true; if (e.CommandTarget == TargetType.Annot) { e.Handle = true; e.PopupMenu = new ContextMenu(); if (e.PressOnLink) { e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); MenuItem propertyMenu = new MenuItem(); propertyMenu = new MenuItem(); propertyMenu.Header = "Edit"; WeakEventManager.AddHandler(propertyMenu, "Click", EditLink_Click); propertyMenu.CommandParameter = e; e.PopupMenu.Items.Add(propertyMenu); } else if (e.PressOnAnnot) { e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); } else if (e.PressOnMedia || e.PressOnSound) { e.Handle = true; e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); } else { e.Handle = true; e.PopupMenu = new ContextMenu(); e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new Separator()); MenuItem fitWidthMenu = new MenuItem(); fitWidthMenu.Header = "Fit Width"; fitWidthMenu.Click += (o, p) => { if (pdfViewControl != null) { pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth); } }; e.PopupMenu.Items.Add(fitWidthMenu); MenuItem fitSizeMenu = new MenuItem(); fitSizeMenu.Header = "Actual Size"; fitSizeMenu.Click += (o, p) => { if (pdfViewControl != null) { pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize); } }; e.PopupMenu.Items.Add(fitSizeMenu); MenuItem zoomInMenu = new MenuItem(); zoomInMenu.Header = "Zoom In"; zoomInMenu.Click += (o, p) => { if (pdfViewControl != null) { double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false); pdfViewControl.PDFView?.Zoom(newZoom); } }; e.PopupMenu.Items.Add(zoomInMenu); MenuItem zoomOutMenu = new MenuItem(); zoomOutMenu.Header = "Zoom Out"; zoomOutMenu.Click += (o, p) => { if (pdfViewControl != null) { double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true); pdfViewControl.PDFView?.Zoom(newZoom); } }; e.PopupMenu.Items.Add(zoomOutMenu); e.PopupMenu.Items.Add(new Separator()); MenuItem singleView = new MenuItem(); singleView.Header = "Single Page"; singleView.Click += (o, p) => { if (pdfViewControl != null) { pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single); } }; e.PopupMenu.Items.Add(singleView); MenuItem singleContinuousView = new MenuItem(); singleContinuousView.Header = "Single Continuous Page"; singleContinuousView.Click += (o, p) => { if (pdfViewControl != null) { pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous); } }; e.PopupMenu.Items.Add(singleContinuousView); MenuItem doubleView = new MenuItem(); doubleView.Header = "Double Page"; doubleView.Click += (o, p) => { if (pdfViewControl != null) { pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double); } }; e.PopupMenu.Items.Add(doubleView); MenuItem doubleContinuousView = new MenuItem(); doubleContinuousView.Header = "Double Continuous Page"; doubleContinuousView.Click += (o, p) => { if (pdfViewControl != null) { pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous); } }; e.PopupMenu.Items.Add(doubleContinuousView); } } break; case CommandType.Copy: e.DoCommand(); break; case CommandType.Cut: case CommandType.Paste: case CommandType.Delete: e.DoCommand(); break; default: break; } } 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 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) { if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { string oldFilePath = pdfViewControl.PDFView.Document.FilePath; if (oldFilePath.ToLower() == filePath.ToLower()) { return; } } 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) { } } } #region Annotation public void InitialPDFViewControl(PDFViewControl newPDFViewer) { pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView); pdfAnnotationControl.AnnotationCancel(); AnnotationBarControl.ClearAllToolState(); ExpandRightPropertyPanel(null, Visibility.Collapsed); } #endregion private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e) { CPDFAnnotationType[] annotationProperties = { CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout, CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText, CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square, CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio }; AnnotationBarControl.InitAnnotationBar(annotationProperties); AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged; AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel; AnnotationBarControl.UndoRedoEvent += AnnotationBarControl_UndoRedoEvent; } private void AnnotationBarControl_UndoRedoEvent(object sender, string e) { if (pdfViewControl != null && pdfViewControl.PDFView != null) { if (e == "Undo") { pdfViewControl.PDFView.UndoManager?.Undo(); } else { pdfViewControl.PDFView.UndoManager?.Redo(); } } } private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e) { AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged; AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel; } private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e) { pdfAnnotationControl.AnnotationCancel(); ExpandRightPropertyPanel(null, Visibility.Collapsed); } private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e) { pdfAnnotationControl.LoadAnnotationPanel(e); ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible); } private void AnnotationBarControl_Click(object sender, RoutedEventArgs e) { ToggleButton toggleButton = sender as ToggleButton; if (toggleButton != null) { if (toggleButton.IsChecked == true) { if (pdfAnnotationControl != null) { ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible); } } else { ExpandRightPropertyPanel(null, Visibility.Collapsed); } } } public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible) { PropertyContainer.Width = 260; PropertyContainer.Child = properytPanel; PropertyContainer.Visibility = visible; } private void EditLink_Click(object sender, RoutedEventArgs e) { PropertyContainer.Visibility = Visibility.Visible; } } }