using ComPDFKit.PDFDocument;
using Compdfkit_Tools.Helper;
using Compdfkit_Tools.PDFControl;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Drawing;
using ComPDFKit.PDFPage;
using ComPDFKitViewer.PdfViewer;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Compdfkit_Tools.Edit;
namespace Forms
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region property
private PDFViewControl passwordViewer;
private PDFViewControl pdfViewControl;
private CPDFSearchControl searchControl = null;
private FromPropertyControl formPropertyControl;
bool LoadPDFFormTool = false;
private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
public bool CanSave
{
get
{
if (pdfViewControl != null && pdfViewControl.PDFView != null)
{
return pdfViewControl.PDFView.UndoManager.CanSave;
}
return false;
}
}
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 event PropertyChangedEventHandler PropertyChanged;
#endregion
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
DataContext = this;
}
#region Usercontrol loaded unload initialize
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search);
formPropertyControl = new FromPropertyControl();
LoadDefaultDocument();
}
private void PDFFormTool_Loaded(object sender, RoutedEventArgs e)
{
LoadPDFFormTool = true;
}
private void PDFFormTool_Unloaded(object sender, RoutedEventArgs e)
{
LoadPDFFormTool = false;
}
#endregion
#region Open, save and close document
private void LoadDocument()
{
pdfViewControl.PDFView?.Load();
pdfViewControl.PDFView?.SetShowLink(true);
PDFGrid.Child = pdfViewControl;
pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
pdfViewControl.PDFView.SetFormFieldHighlight(true);
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.ChangeFitMode(FitMode.FitWidth);
CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
ViewSettingBtn.IsChecked = false;
PropertyContainer.Child = null;
PropertyContainer.Visibility = Visibility.Collapsed;
FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
InitialPDFViewControl(pdfViewControl);
SetFromMode();
BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
}
private void LoadDefaultDocument()
{
string defaultFilePath = "Form_Widgets_Test.pdf";
pdfViewControl = new PDFViewControl();
pdfViewControl.PDFView.InitDocument(defaultFilePath);
LoadDocument();
}
public void InitialPDFViewControl(PDFViewControl newPDFViewer)
{
PDFFormTool.InitWithPDFViewer(newPDFViewer.PDFView, formPropertyControl);
formPropertyControl.SetPDFViewer(newPDFViewer.PDFView);
PDFFormTool.ClearAllToolState();
newPDFViewer.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
newPDFViewer.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
newPDFViewer.CustomSignHandle = true;
newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
}
///
/// Save the file in the current path.
///
private void SaveFile()
{
if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
{
CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
if (pdfDoc.WriteToLoadedPath())
{
pdfViewControl.PDFView.UndoManager.CanSave = false;
return;
}
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "(*.pdf)|*.pdf";
saveDialog.DefaultExt = ".pdf";
saveDialog.OverwritePrompt = true;
if (saveDialog.ShowDialog() == true)
{
pdfDoc.WriteToFilePath(saveDialog.FileName);
}
}
}
///
/// Save the file to another PDF file.
///
public void SaveAsFile()
{
{
if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
{
CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "(*.pdf)|*.pdf";
saveDialog.DefaultExt = ".pdf";
saveDialog.OverwritePrompt = true;
if (saveDialog.ShowDialog() == true)
{
pdfDoc.WriteToFilePath(saveDialog.FileName);
}
}
}
}
#endregion
#region Property changed
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private void PdfViewer_InfoChanged(object sender, KeyValuePair e)
{
if (e.Key == "Zoom")
{
CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
}
}
private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(e.PropertyName);
}
#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