using Compdfkit_Tools.Helper;
using Compdfkit_Tools.PDFControl;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using ComPDFKit.PDFDocument;
using Microsoft.Win32;
using System.Windows.Controls.Primitives;
using ComPDFKit.PDFPage;
using ComPDFKitViewer.PdfViewer;
namespace DocsEditor
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool isFirstLoad = true;
private string currentMode = "Page Edit";
private PDFViewControl passwordViewer;
private PDFViewControl pdfViewControl;
private CPDFAnnotationControl pdfAnnotationControl = null;
private CPDFSearchControl searchControl = null;
private CPDFPageEditControl pageEditControl = null;
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 event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
DataContext = this;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search);
pdfAnnotationControl = new CPDFAnnotationControl();
LoadDefaultDocument();
}
private void LoadDefaultDocument()
{
string defaultFilePath = "developer_guide_windows.pdf";
LeftToolPanelButton.IsEnabled = false;
SearchButton.IsEnabled = false;
RightToolPanelButton.IsEnabled = false;
ViewSettingBtn.IsEnabled = false;
pdfViewControl = new PDFViewControl();
pdfViewControl.PDFView.InitDocument(defaultFilePath);
LoadDocument();
}
private void PdfViewer_InfoChanged(object sender, KeyValuePair e)
{
if (e.Key == "Zoom")
{
CPDFSaclingControl.SetZoomTextBoxText(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 CopyImage_Click(object sender, RoutedEventArgs e)
{
try
{
Dictionary> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
if (imageDict != null && imageDict.Count > 0)
{
foreach (int pageIndex in imageDict.Keys)
{
List imageList = imageDict[pageIndex];
foreach (Bitmap image in imageList)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage imageData = new BitmapImage();
imageData.BeginInit();
imageData.StreamSource = ms;
imageData.CacheOption = BitmapCacheOption.OnLoad;
imageData.EndInit();
imageData.Freeze();
Clipboard.SetImage(imageData);
break;
}
}
}
}
catch (Exception ex)
{
}
}
private void ExtraImage_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string choosePath = folderDialog.SelectedPath;
string openPath = choosePath;
try
{
Dictionary> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
if (imageDict != null && imageDict.Count > 0)
{
foreach (int pageIndex in imageDict.Keys)
{
List imageList = imageDict[pageIndex];
foreach (Bitmap image in imageList)
{
string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
openPath = savePath;
}
}
}
Process.Start("explorer", "/select,\"" + openPath + "\"");
}
catch (Exception ex)
{
}
}
}
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