using ComPDFKit.PDFDocument;
using ComPDFKit.PDFPage;
using compdfkit_tools.Edit;
using compdfkit_tools.Helper;
using compdfkit_tools.PDFControl;
using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace edit_ctrl_demo
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
///
/// 是否能够撤销
///
public bool CanUndo
{
get
{
if (pdfViewControl != null && pdfViewControl.PDFView != null)
{
return pdfViewControl.PDFView.UndoManager.CanUndo;
}
return false;
}
}
///
/// 是否能够重做
///
public bool CanRedo
{
get
{
if (pdfViewControl != null && pdfViewControl.PDFView != null)
{
return pdfViewControl.PDFView.UndoManager.CanRedo;
}
return false;
}
}
public bool CanSave
{
get
{
if (pdfViewControl != null && pdfViewControl.PDFView != null)
{
return pdfViewControl.PDFView.UndoManager.CanSave;
}
return false;
}
}
///
/// 上一次的PDF编辑对象
///
private PDFEditEvent lastPDFEditEvent = null;
///
/// 缩放比例
///
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;
}
///
/// 属性更改事件
///
public event PropertyChangedEventHandler PropertyChanged;
///
///触发属性更改事件通知
///
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
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;
pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
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)) + "%";
SetEditMode();
}
private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
{
if (e != null && e.CommandType == CommandType.Context)
{
if (e.PressOnSelectedText)
{
e.Handle = true;
e.PopupMenu = new ContextMenu();
e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
}
else if (e.CommandTarget == TargetType.ImageSelection)
{
if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
{
e.Handle = true;
e.PopupMenu = new ContextMenu();
MenuItem imageCopyMenu = new MenuItem();
imageCopyMenu = new MenuItem();
imageCopyMenu.Header = "Copy Images";
WeakEventManager