using ComPDFKit.PDFDocument;
using ComPDFKit.PDFPage;
using ComPDFKit.PDFPage.Edit;
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;
using System.Windows.Media.Imaging;
using Point = System.Windows.Point;
namespace ContentEditor
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region Property
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;
}
}
///
/// The last PDF edit object
///
private PDFEditEvent lastPDFEditEvent = null;
private PDFViewControl passwordViewer;
private PDFViewControl pdfViewControl;
private PDFImageEditControl imageEditControl = new PDFImageEditControl();
private PDFTextEditControl textEditControl = new PDFTextEditControl();
private Border empytPanel = new Border();
private UIElement prevPanel = null;
private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
public event PropertyChangedEventHandler PropertyChanged;
private PDFEditEvent pdfTextCreateParam;
private KeyEventHandler KeyDownHandler;
#endregion
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
DataContext = this;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
TextBlock emptyBlock = new TextBlock();
emptyBlock.HorizontalAlignment = HorizontalAlignment.Center;
emptyBlock.VerticalAlignment = VerticalAlignment.Center;
empytPanel.Child = emptyBlock;
empytPanel.Width = 260;
BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search);
LoadDefaultDocument();
}
#region Load document
private void LoadDocument()
{
if (pdfViewControl.PDFView.Document == null)
{
return;
}
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;
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;
pdfViewControl.PDFView.SetFormFieldHighlight(true);
pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
ViewSettingBtn.IsChecked = false;
PropertyContainer.Child = null;
PropertyContainer.Visibility = Visibility.Collapsed;
SetEditMode();
if (KeyDownHandler != null)
{
pdfViewControl.PDFView.RemoveHandler(KeyDownEvent, KeyDownHandler);
}
KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
pdfViewControl.PDFView.AddHandler(KeyDownEvent, KeyDownHandler, false, true);
}
private void LoadDefaultDocument()
{
string defaultFilePath = "PDF32000_2008.pdf";
pdfViewControl = new PDFViewControl();
pdfViewControl.PDFView.InitDocument(defaultFilePath);
LoadDocument();
}
#endregion
#region Password
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.PDFView.Document.Release();
pdfViewControl = passwordViewer;
LoadDocument();
}
else
{
PasswordUI.SetShowError("Wrong Password", 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;
}
#endregion
#region Expand and collapse Panel
private void ExpandLeftPanel(bool isExpand)
{
BotaSideTool.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);
}
}
#endregion
#region Context menu
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