|
@@ -1,10 +1,13 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
using System.Linq;
|
|
|
+using System.Runtime.CompilerServices;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
+using System.Windows.Controls.Primitives;
|
|
|
using System.Windows.Data;
|
|
|
using System.Windows.Documents;
|
|
|
using System.Windows.Input;
|
|
@@ -12,57 +15,415 @@ using System.Windows.Media;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
using System.Windows.Navigation;
|
|
|
using System.Windows.Shapes;
|
|
|
+using Compdfkit_Tools.Helper;
|
|
|
+using Compdfkit_Tools.PDFControl;
|
|
|
+using ComPDFKit.PDFDocument;
|
|
|
+using ComPDFKitViewer;
|
|
|
+using ComPDFKitViewer.PdfViewer;
|
|
|
+using Microsoft.Win32;
|
|
|
|
|
|
namespace PDFViewer_new
|
|
|
{
|
|
|
- /// <summary>
|
|
|
- /// MainPage.xaml 的交互逻辑
|
|
|
- /// </summary>
|
|
|
- public partial class MainPage : UserControl
|
|
|
+ public partial class MainPage : UserControl, INotifyPropertyChanged
|
|
|
{
|
|
|
+ private PDFViewControl pdfViewer;
|
|
|
+ private PDFViewControl passwordViewer;
|
|
|
+ private AnnotationControl annotationControl = new AnnotationControl();
|
|
|
+ private FormControl formControl = new FormControl();
|
|
|
+ private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl();
|
|
|
+
|
|
|
+ private bool _canSave = false;
|
|
|
+ public bool CanSave
|
|
|
+ {
|
|
|
+
|
|
|
+ get => _canSave;
|
|
|
+ set
|
|
|
+ {
|
|
|
+ _canSave = value;
|
|
|
+ OnPropertyChanged();
|
|
|
+ }
|
|
|
+ }
|
|
|
public MainPage()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
}
|
|
|
|
|
|
- private void OpenFile_Click(object sender, RoutedEventArgs e)
|
|
|
+ #region Load document
|
|
|
+
|
|
|
+ private void LoadDefaultDocument()
|
|
|
+ {
|
|
|
+ string defaultFilePath = "PDF32000_2008.pdf";
|
|
|
+ pdfViewer.PDFView.InitDocument(defaultFilePath);
|
|
|
+ LoadDocument();
|
|
|
+ PDFGrid.Child = formControl;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void LoadDocument()
|
|
|
+ {
|
|
|
+ pdfViewer.PDFView.Load();
|
|
|
+ pdfViewer.PDFView.SetShowLink(true);
|
|
|
+
|
|
|
+ pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
|
|
|
+ pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
|
|
|
+
|
|
|
+ pdfViewer.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;
|
|
|
+
|
|
|
+ pdfViewer.PDFView.ChangeFitMode(FitMode.FitWidth);
|
|
|
+ CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.PDFView.ZoomFactor * 100)));
|
|
|
+
|
|
|
+ ViewSettingBtn.IsChecked = false;
|
|
|
+ botaBarControl.InitWithPDFViewer(pdfViewer.PDFView);
|
|
|
+ botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
|
|
|
+ botaBarControl.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
|
|
|
+ LoadCustomControl();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void PdfFormControlRefreshAnnotList(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ botaBarControl.LoadAnnotationList();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void FormControlOnCanSaveChanged(object sender, bool e)
|
|
|
+ {
|
|
|
+ this.CanSave = e;
|
|
|
+ }
|
|
|
+
|
|
|
+ #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;
|
|
|
+ pdfViewer = 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 Load Unload custom control
|
|
|
+ private void LoadCustomControl()
|
|
|
+ {
|
|
|
+ formControl.PdfViewControl = pdfViewer;
|
|
|
+ formControl.InitWithPDFViewer(pdfViewer.PDFView);
|
|
|
+ InitialPDFViewControl(formControl.PdfViewControl);
|
|
|
+ formControl.OnCanSaveChanged -= FormControlOnCanSaveChanged;
|
|
|
+ formControl.OnCanSaveChanged += FormControlOnCanSaveChanged;
|
|
|
+ formControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
|
|
|
+ formControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
|
|
|
+ formControl.SetBOTAContainer(botaBarControl);
|
|
|
+ formControl.InitialPDFViewControl(formControl.PdfViewControl);
|
|
|
+
|
|
|
+ annotationControl.PdfViewControl = pdfViewer;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void MainWindow_Loaded(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
+ pdfViewer = new PDFViewControl();
|
|
|
+ LoadDefaultDocument();
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Annotation
|
|
|
|
|
|
+ public void InitialPDFViewControl(PDFViewControl newPDFViewer)
|
|
|
+ {
|
|
|
+ formControl.ClearAllToolState();
|
|
|
+ formControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
|
|
|
}
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Event handle
|
|
|
|
|
|
+ private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
|
|
|
+ {
|
|
|
+ if (e.Key == "Zoom")
|
|
|
+ {
|
|
|
+ CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
+ SaveFile();
|
|
|
+ pdfViewer.PDFView.UndoManager.CanSave = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OpenFile()
|
|
|
+ {
|
|
|
+ string filePath = CommonHelper.GetFilePathOrEmpty();
|
|
|
+ if (!string.IsNullOrEmpty(filePath) && formControl.PdfViewControl != null)
|
|
|
+ {
|
|
|
+ if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
|
|
|
+ {
|
|
|
+ string oldFilePath = pdfViewer.PDFView.Document.FilePath;
|
|
|
+ if (oldFilePath.ToLower() == filePath.ToLower())
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ passwordViewer = new PDFViewControl();
|
|
|
+ passwordViewer.PDFView.InitDocument(filePath);
|
|
|
+ if (passwordViewer.PDFView.Document == null)
|
|
|
+ {
|
|
|
+ MessageBox.Show("Open File Failed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (passwordViewer.PDFView.Document.IsLocked)
|
|
|
+ {
|
|
|
+ PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
|
|
|
+ PasswordUI.ClearPassword();
|
|
|
+ PopupBorder.Visibility = Visibility.Visible;
|
|
|
+ PasswordUI.Visibility = Visibility.Visible;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ pdfViewer = passwordViewer;
|
|
|
+ LoadDocument();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
|
|
|
+ private void OpenFile_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
+ OpenFile();
|
|
|
+ }
|
|
|
|
|
|
+ private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ ToggleButton expandBtn = sender as ToggleButton;
|
|
|
+ if (expandBtn != null)
|
|
|
+ {
|
|
|
+ bool isExpand = expandBtn.IsChecked == true;
|
|
|
+ formControl.ExpandLeftPanel(isExpand);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
{
|
|
|
-
|
|
|
+ var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
|
|
|
+ if ((string)item.Content == "Viewer")
|
|
|
+ {
|
|
|
+ formControl.ClearAllToolState();
|
|
|
+ formControl.SetToolBarContainerVisibility(Visibility.Collapsed);
|
|
|
+ formControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
|
|
|
+ //formControl.FromPropertyControl.AnnotationCancel();
|
|
|
+ RightPanelButton.IsChecked = false;
|
|
|
+ if (formControl.PdfViewControl != null && formControl.PdfViewControl.PDFView != null)
|
|
|
+ {
|
|
|
+ formControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if ((string)item.Content == "Annotation")
|
|
|
+ {
|
|
|
+ annotationControl.SetToolBarContainerVisibility(Visibility.Visible);
|
|
|
+ if (annotationControl.PdfViewControl != null && annotationControl.PdfViewControl.PDFView != null)
|
|
|
+ {
|
|
|
+ formControl.ClearViewerControl();
|
|
|
+ PDFGrid.Child = annotationControl;
|
|
|
+ annotationControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
|
|
|
+ annotationControl.InitWithPDFViewer(pdfViewer.PDFView);
|
|
|
+ InitialPDFViewControl(annotationControl.PdfViewControl);
|
|
|
+ annotationControl.OnCanSaveChanged -= FormControlOnCanSaveChanged;
|
|
|
+ annotationControl.OnCanSaveChanged += FormControlOnCanSaveChanged;
|
|
|
+ annotationControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
|
|
|
+ annotationControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
|
|
|
+ annotationControl.SetBOTAContainer(botaBarControl);
|
|
|
+ annotationControl.InitialPDFViewControl(annotationControl.PdfViewControl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if ((string)item.Content == "Form")
|
|
|
+ {
|
|
|
+ formControl.SetToolBarContainerVisibility(Visibility.Visible);
|
|
|
+ if (formControl.PdfViewControl != null && formControl.PdfViewControl.PDFView != null)
|
|
|
+ {
|
|
|
+ annotationControl.ClearViewerControl();
|
|
|
+ PDFGrid.Child = formControl;
|
|
|
+ formControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
|
|
|
+ formControl.PdfViewControl = pdfViewer;
|
|
|
+ formControl.InitWithPDFViewer(pdfViewer.PDFView);
|
|
|
+ InitialPDFViewControl(formControl.PdfViewControl);
|
|
|
+ formControl.OnCanSaveChanged -= FormControlOnCanSaveChanged;
|
|
|
+ formControl.OnCanSaveChanged += FormControlOnCanSaveChanged;
|
|
|
+ formControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
|
|
|
+ formControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
|
|
|
+ formControl.SetBOTAContainer(botaBarControl);
|
|
|
+ formControl.InitialPDFViewControl(formControl.PdfViewControl);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
+ PasswordUI.Visibility = Visibility.Collapsed;
|
|
|
+ FileInfoUI.Visibility = Visibility.Visible;
|
|
|
+ FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
|
|
|
+ PopupBorder.Visibility = Visibility.Visible;
|
|
|
+ }
|
|
|
|
|
|
+ private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ ShowViewSettings();
|
|
|
}
|
|
|
|
|
|
+ private void ShowViewSettings()
|
|
|
+ {
|
|
|
+ if (ViewSettingBtn != null)
|
|
|
+ {
|
|
|
+ if (ViewSettingBtn.IsChecked == true)
|
|
|
+ {
|
|
|
+ CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
|
|
|
+ displayPanel.InitWithPDFViewer(formControl.PdfViewControl.PDFView);
|
|
|
+ formControl.SetViewSettings(Visibility.Visible, displayPanel);
|
|
|
+ if ((bool)RightPanelButton.IsChecked)
|
|
|
+ {
|
|
|
+ RightPanelButton.IsChecked = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ formControl.SetViewSettings(Visibility.Collapsed);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
private void RightPanelButton_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
+ ControlRightPanel();
|
|
|
+ }
|
|
|
|
|
|
+ private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ formControl.ExpandLeftPanel(true);
|
|
|
+ botaBarControl.SelectBotaTool(BOTATools.Search);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ControlRightPanel()
|
|
|
+ {
|
|
|
+ if (RightPanelButton != null)
|
|
|
+ {
|
|
|
+ if (RightPanelButton.IsChecked == true)
|
|
|
+ {
|
|
|
+ if (formControl.FromPropertyControl != null)
|
|
|
+ {
|
|
|
+ formControl.ExpandRightPropertyPanel(formControl.FromPropertyControl, Visibility.Visible);
|
|
|
+ if ((bool)ViewSettingBtn.IsChecked)
|
|
|
+ {
|
|
|
+ ViewSettingBtn.IsChecked = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ formControl.ExpandRightPropertyPanel(null, Visibility.Collapsed);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ #region Save file
|
|
|
+ /// <summary>
|
|
|
+ /// Save the file to another PDF file.
|
|
|
+ /// </summary>
|
|
|
+ public void SaveAsFile()
|
|
|
{
|
|
|
+ {
|
|
|
+ if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
|
|
|
+ {
|
|
|
+ CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
|
|
|
+ SaveFileDialog saveDialog = new SaveFileDialog();
|
|
|
+ saveDialog.Filter = "(*.pdf)|*.pdf";
|
|
|
+ saveDialog.DefaultExt = ".pdf";
|
|
|
+ saveDialog.OverwritePrompt = true;
|
|
|
|
|
|
+ if (saveDialog.ShowDialog() == true)
|
|
|
+ {
|
|
|
+ pdfDoc.WriteToFilePath(saveDialog.FileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ /// <summary>
|
|
|
+ /// Save the file in the current path.
|
|
|
+ /// </summary>
|
|
|
+ private void SaveFile()
|
|
|
{
|
|
|
+ if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
|
|
|
+ if (pdfDoc.WriteToLoadedPath())
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
+ SaveFileDialog saveDialog = new SaveFileDialog();
|
|
|
+ saveDialog.Filter = "(*.pdf)|*.pdf";
|
|
|
+ saveDialog.DefaultExt = ".pdf";
|
|
|
+ saveDialog.OverwritePrompt = true;
|
|
|
+
|
|
|
+ if (saveDialog.ShowDialog() == true)
|
|
|
+ {
|
|
|
+ pdfDoc.WriteToFilePath(saveDialog.FileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ PopupBorder.Visibility = Visibility.Collapsed;
|
|
|
+ }
|
|
|
+
|
|
|
+ public event PropertyChangedEventHandler PropertyChanged;
|
|
|
+
|
|
|
+ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
+ {
|
|
|
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
}
|
|
|
}
|
|
|
}
|