123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- using ComPDFKit.PDFDocument;
- using Compdfkit_Tools.Helper;
- using Compdfkit_Tools.PDFControl;
- using ComPDFKitViewer;
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- 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;
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool;
- namespace Viewer
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- #region Property
- private PDFViewControl passwordViewer;
- private PDFViewControl pdfViewControl;
- CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
- private int[] zoomLevelList = { 10, 25, 50, 100, 150, 200, 300, 400, 500, 1000 };
- public event PropertyChangedEventHandler PropertyChanged;
- private KeyEventHandler KeyDownHandler;
- public bool CanSave
- {
- get
- {
- if (pdfViewControl != null && pdfViewControl.PDFViewTool.GetCPDFViewer() != null)
- {
- if (pdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
- pdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
- {
- return true;
- }
- }
- return false;
- }
- }
- public string AppInfo
- {
- get { return Assembly.GetExecutingAssembly().GetName().Name + " " + string.Join(".", Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Take(3)); }
- }
- #endregion
- public MainWindow()
- {
- InitializeComponent();
- Loaded += MainWindow_Loaded;
- DataContext = this;
- }
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- BotaSideTool.AddBOTAContent(new[] { BOTATools.Thumbnail, BOTATools.Outline, BOTATools.Bookmark, BOTATools.Search });
- LoadDefaultDocument();
- }
- #region Load Document
- private void LoadDocument()
- {
- if (pdfViewControl.PDFToolManager.GetDocument() == null)
- {
- return;
- }
- pdfViewControl.PDFToolManager?.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
- PDFGrid.Child = pdfViewControl;
- 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.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
- pdfViewControl.MouseRightButtonDownHandler += PDFViewControl_MouseRightButtonDownHandler;
- pdfViewControl.PDFViewTool.GetCPDFViewer().SetFitMode(FitMode.FitWidth);
- CPDFSaclingControl.InitWithPDFViewer(pdfViewControl);
- CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() * 100)));
- FloatPageTool.InitWithPDFViewer(pdfViewControl);
- BotaSideTool.InitWithPDFViewer(pdfViewControl);
- BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
- ViewSettingBtn.IsChecked = false;
- PropertyContainer.Child = null;
- PropertyContainer.Visibility = Visibility.Collapsed;
- }
- private void LoadDefaultDocument()
- {
- string defaultFilePath = "ComPDFKit_Sample_File_Windows.pdf";
- pdfViewControl = new PDFViewControl();
- pdfViewControl.InitDocument(defaultFilePath);
- LoadDocument();
- }
- #endregion
- #region Password
- private void PasswordUI_Confirmed(object sender, string e)
- {
- if (passwordViewer != null && passwordViewer.PDFToolManager != null && passwordViewer.PDFToolManager.GetDocument() != null)
- {
- passwordViewer.PDFToolManager.GetDocument().UnlockWithPassword(e);
- if (passwordViewer.PDFToolManager.GetDocument().IsLocked == false)
- {
- PasswordUI.SetShowError("", Visibility.Collapsed);
- PasswordUI.ClearPassword();
- PasswordUI.Visibility = Visibility.Collapsed;
- PopupBorder.Visibility = Visibility.Collapsed;
- 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 or Hide Panel
- private void ControlLeftPanel()
- {
- if (LeftToolPanelButton != null)
- {
- bool isExpand = LeftToolPanelButton.IsChecked == true;
- ExpandLeftPanel(isExpand);
- }
- }
- 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);
- }
- }
- private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- BotaSideTool.SelectBotaTool(BOTATools.Search);
- }
- private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton expandBtn = sender as ToggleButton;
- if (expandBtn != null)
- {
- bool isExpand = expandBtn.IsChecked == true;
- ExpandLeftPanel(isExpand);
- }
- }
- #endregion
- #region UI
- 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 ShowViewSettings()
- {
- if (ViewSettingBtn != null)
- {
- if (ViewSettingBtn.IsChecked == true)
- {
- displayPanel.InitWithPDFViewer(pdfViewControl);
- PropertyContainer.Child = displayPanel;
- PropertyContainer.Visibility = Visibility.Visible;
- }
- else
- {
- PropertyContainer.Child = null;
- PropertyContainer.Visibility = Visibility.Collapsed;
- }
- }
- }
- private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
- {
- ShowViewSettings();
- }
- private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
- {
- if (pdfViewControl != null)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() + 0.01, true);
- pdfViewControl.PDFViewTool.GetCPDFViewer().SetZoom(newZoom);
- }
- }
- private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
- {
- if (pdfViewControl != null)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() - 0.01, false);
- pdfViewControl.PDFViewTool.GetCPDFViewer().SetZoom(newZoom);
- }
- }
- private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- pdfViewControl.PDFViewTool.GetCPDFViewer()?.GoToPage(pdfViewControl.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex + 1,new System.Windows.Point(0,0));
- }
- private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- pdfViewControl.PDFViewTool.GetCPDFViewer()?.GoToPage(pdfViewControl.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex - 1, new System.Windows.Point(0, 0));
- }
- private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
- {
- PasswordUI.Visibility = Visibility.Collapsed;
- FileInfoUI.Visibility = Visibility.Visible;
- FileInfoControl.InitWithPDFViewer(pdfViewControl);
- PopupBorder.Visibility = Visibility.Visible;
- }
- private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
- {
- PopupBorder.Visibility = Visibility.Collapsed;
- }
- #endregion
- #region Open and save file
- /// <summary>
- /// Save the file to another PDF file.
- /// </summary>
- public void SaveAsFile()
- {
- {
- if (pdfViewControl != null && pdfViewControl.PDFToolManager != null && pdfViewControl.PDFToolManager.GetDocument() != null)
- {
- CPDFDocument pdfDoc = pdfViewControl.PDFToolManager.GetDocument();
- SaveFileDialog saveDialog = new SaveFileDialog();
- saveDialog.Filter = "(*.pdf)|*.pdf";
- saveDialog.DefaultExt = ".pdf";
- saveDialog.OverwritePrompt = true;
- if (saveDialog.ShowDialog() == true)
- {
- pdfDoc.WriteToFilePath(saveDialog.FileName);
- }
- }
- }
- }
- private void SaveFile()
- {
- if (pdfViewControl != null && pdfViewControl.PDFToolManager != null && pdfViewControl.PDFToolManager.GetDocument() != null)
- {
- CPDFDocument pdfDoc = pdfViewControl.PDFToolManager.GetDocument();
- if (pdfDoc.WriteToLoadedPath())
- {
- pdfViewControl.PDFViewTool.IsDocumentModified = false;
- return;
- }
- SaveFileDialog saveDialog = new SaveFileDialog();
- saveDialog.Filter = "(*.pdf)|*.pdf";
- saveDialog.DefaultExt = ".pdf";
- saveDialog.OverwritePrompt = true;
- if (saveDialog.ShowDialog() == true)
- {
- if (pdfDoc.WriteToFilePath(saveDialog.FileName))
- {
- pdfViewControl.PDFViewTool.IsDocumentModified = false;
- }
- }
- }
- }
- private void OpenFile()
- {
- string filePath = CommonHelper.GetExistedPathOrEmpty();
- if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
- {
- if (pdfViewControl.PDFToolManager != null && pdfViewControl.PDFToolManager.GetDocument() != null)
- {
- string oldFilePath = pdfViewControl.PDFToolManager.GetDocument().FilePath;
- if (oldFilePath.ToLower() == filePath.ToLower())
- {
- return;
- }
- }
- passwordViewer = new PDFViewControl();
- passwordViewer.InitDocument(filePath);
- if (passwordViewer.PDFToolManager.GetDocument() == null)
- {
- MessageBox.Show("Open File Failed");
- return;
- }
- if (passwordViewer.PDFToolManager.GetDocument().IsLocked)
- {
- PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
- PasswordUI.ClearPassword();
- PopupBorder.Visibility = Visibility.Visible;
- PasswordUI.Visibility = Visibility.Visible;
- }
- else
- {
- pdfViewControl.PDFToolManager.GetDocument().Release();
- pdfViewControl = passwordViewer;
- LoadDocument();
- }
- }
- }
- private void OpenFile_Click(object sender, RoutedEventArgs e)
- {
- OpenFile();
- }
- private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
- {
- SaveFile();
- }
- #endregion
- #region Context Menu
- private void ExtractImage_Click(object sender, RoutedEventArgs e)
- {
- System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
- if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- var image = pdfViewControl.FocusPDFViewTool.GetSelectImage();
- if(image == null)
- {
- return;
- }
- CPDFPage page = pdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
- string savePath = Path.Combine(folderDialog.SelectedPath, Guid.NewGuid() + ".jpg");
- string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
- page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
- Bitmap bitmap = new Bitmap(tempPath);
- bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
- Process.Start("explorer", "/select,\"" + savePath + "\"");
- }
- }
- private void CopyImage_Click(object sender, RoutedEventArgs e)
- {
- var image = pdfViewControl.FocusPDFViewTool.GetSelectImage();
- if(image == null)
- {
- return;
- }
- CPDFPage page = pdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
- string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
- page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
- Bitmap bitmap = new Bitmap(tempPath);
- BitmapImage imageData;
- using (MemoryStream ms = new MemoryStream())
- {
- bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
- imageData = new BitmapImage();
- imageData.BeginInit();
- imageData.StreamSource = ms;
-
- imageData.CacheOption = BitmapCacheOption.OnLoad;
- imageData.EndInit();
- imageData.Freeze();
- Clipboard.SetImage(imageData);
- bitmap.Dispose();
- File.Delete(tempPath);
- }
- }
-
- private void CreateImageContextMenu(object sender, ref ContextMenu menu)
- {
- if (menu == null)
- {
- menu = new ContextMenu();
- }
- MenuItem copyImage = new MenuItem();
- copyImage.Header = "Copy Image";
- copyImage.Click += CopyImage_Click;
- menu.Items.Add(copyImage);
- MenuItem extractImage = new MenuItem();
- extractImage.Header = "Extract Image";
- extractImage.Click += ExtractImage_Click;
- menu.Items.Add(extractImage);
- }
-
- private void PDFViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
- {
- ContextMenu menu = pdfViewControl.GetRightMenu();
- if (menu == null)
- {
- menu = new ContextMenu();
- }
- switch (e.hitTestType)
- {
- case MouseHitTestType.ImageSelect:
- CreateImageContextMenu(sender, ref menu);
- break;
- default:
- pdfViewControl.CreateViewerMenu(sender,ref menu);
- break;
- }
- pdfViewControl.SetRightMenu(menu);
- }
- #endregion
- #region Close Window
- protected override void OnClosing(CancelEventArgs e)
- {
- if (pdfViewControl.PDFViewTool.IsDocumentModified)
- {
- MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
- if (result == MessageBoxResult.Yes)
- {
- SaveFile();
- }
- else if (result == MessageBoxResult.No)
- {
- }
- else
- {
- e.Cancel = true;
- }
- }
- }
- #endregion
- #region PropertyChanged
- /// <summary>
- /// Zoom
- /// </summary>
- private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
- {
- if (e.Key == "Zoom")
- {
- CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
- }
- }
- /// <summary>
- /// Undo Redo Event Noitfy
- /// </summary>
- private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- OnPropertyChanged(e.PropertyName);
- }
- protected void OnPropertyChanged([CallerMemberName] string name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- #endregion
- #region Shortcut
- private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
- {
- OpenFile();
- }
- private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
- {
- if (CanSave)
- {
- SaveFile();
- }
- }
- private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
- {
- SaveAsFile();
- }
- private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
- {
- LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
- ControlLeftPanel();
- }
- private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
- }
- private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Outline);
- }
- private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
- }
- private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Search);
- }
- private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
- {
- //double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
- //pdfViewControl.PDFView?.Zoom(newZoom);
- }
- private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
- {
- //double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
- //pdfViewControl.PDFView?.Zoom(newZoom);
- }
- private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
- {
- ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
- ShowViewSettings();
- }
- private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
- {
- if (PopupBorder.Visibility != Visibility.Visible)
- {
- PasswordUI.Visibility = Visibility.Collapsed;
- FileInfoUI.Visibility = Visibility.Visible;
- FileInfoControl.InitWithPDFViewer(pdfViewControl);
- PopupBorder.Visibility = Visibility.Visible;
- }
- else
- {
- FileInfoUI.Visibility = Visibility.Collapsed;
- PopupBorder.Visibility = Visibility.Collapsed;
- }
- }
- #endregion
- }
- }
|