123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using Compdfkit_Tools.Data;
- 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.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;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using Path = System.IO.Path;
- namespace PDFViewer
- {
- public partial class MainPage : UserControl, INotifyPropertyChanged
- {
- #region Property
- private bool isFirstLoad = true;
- private string currentMode = "Viewer";
- private PDFViewControl passwordViewer;
- private PDFViewControl pdfViewControl = new PDFViewControl();
- private CPDFAnnotationControl pdfAnnotationControl = null;
- private UIElement pdfEditControl = null;
- private CPDFPageEditControl pageEditControl = null;
- private FromPropertyControl pdfFormControl = new FromPropertyControl();
- private PDFImageEditControl imageEditControl = null;
- private CPDFDisplaySettingsControl displayPanel = 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 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;
- }
- }
- private PDFEditEvent lastPDFEditEvent = null;
- public event Func<string[], bool> CheckExistBeforeOpenFileEvent;
- public event PropertyChangedEventHandler PropertyChanged;
- public event EventHandler FileChangeEvent;
- #endregion
- public MainPage()
- {
- InitializeComponent();
- DataContext = this;
- if (pdfFormControl != null)
- {
- pdfFormControl = new FromPropertyControl();
- }
- }
- #region Initial Load Unload
- public void InitialPDFViewControl(PDFViewControl newPDFViewer)
- {
- pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
- FormBarControl.InitWithPDFViewer(newPDFViewer.PDFView, pdfFormControl);
- pdfFormControl.SetPDFViewer(newPDFViewer.PDFView);
- pdfAnnotationControl.AnnotationCancel();
- AnnotationBarControl.ClearAllToolState();
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
- newPDFViewer.CustomSignHandle = true;
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- pdfAnnotationControl = new CPDFAnnotationControl();
- BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
- LoadDocument();
- }
- public void SetPDFViewer(PDFViewControl newPdfViewer)
- {
- if (newPdfViewer != null)
- {
- pdfViewControl = newPdfViewer;
- }
- }
- private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e)
- {
- CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent;
- CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent;
- CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent;
- CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent;
- CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent;
- CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent; ;
- }
- private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
- {
- AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
- AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
- }
- private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
- {
- CPDFAnnotationType[] annotationProperties = { CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout, CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText, CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square, CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Image, CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio };
- AnnotationBarControl.InitAnnotationBar(annotationProperties);
- AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
- AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
- }
- #endregion
- #region UI
- /// <summary>
- /// InfoChanged
- /// When the PDF zoom ratio changes, the value of the zoom control also changes.
- /// </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)));
- }
- }
- private void EditLink_Click(object sender, RoutedEventArgs e)
- {
- RightPanelButton.IsChecked = true;
- PropertyContainer.Visibility = Visibility.Visible;
- }
- 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 PDFView_AnnotEditHandler(object sender, List<ComPDFKitViewer.AnnotEvent.AnnotEditEvent> e)
- {
- BotaSideTool.LoadAnnotationList();
- }
- private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- OnPropertyChanged(e.PropertyName);
- }
- private void PasswordUI_Closed(object sender, EventArgs e)
- {
- PopupBorder.Visibility = Visibility.Collapsed;
- PasswordUI.Visibility = Visibility.Collapsed;
- }
- private void PasswordUI_Canceled(object sender, EventArgs e)
- {
- PopupBorder.Visibility = Visibility.Collapsed;
- PasswordUI.Visibility = Visibility.Collapsed;
- }
- 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 = passwordViewer;
- LoadDocument();
- FileChangeEvent?.Invoke(null, EventArgs.Empty);
- }
- else
- {
- PasswordUI.SetShowError("error", Visibility.Visible);
- }
- }
- }
- private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
- {
- SaveFile();
- }
- private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- UIElement pageView = sender as UIElement;
- if (pageView != null)
- {
- pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
- }
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- if (imageEditControl == null)
- {
- imageEditControl = new PDFImageEditControl();
- }
- imageEditControl.InitWithPDFViewer(pdfViewControl.PDFView);
- imageEditControl.SetPDFImageEditData(lastPDFEditEvent);
- pdfEditControl = imageEditControl as UIElement;
- PropertyContainer.Child = pdfEditControl;
- ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
- }
- }
- private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
- {
- PasswordUI.Visibility = Visibility.Collapsed;
- FileInfoUI.Visibility = Visibility.Visible;
- FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
- PopupBorder.Visibility = Visibility.Visible;
- }
- private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
- {
- PopupBorder.Visibility = Visibility.Collapsed;
- }
- private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton senderBtn = sender as ToggleButton;
- if (senderBtn != null && pdfViewControl != null)
- {
- ClearPDFEditState(senderBtn);
- if (senderBtn.IsChecked == true)
- {
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
- pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
- pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
- pdfViewControl.PDFView?.ReloadDocument();
- }
- else
- {
- pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
- pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
- pdfViewControl.PDFView?.ReloadDocument();
- }
- }
- }
- private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton senderBtn = sender as ToggleButton;
- if (senderBtn != null && pdfViewControl != null)
- {
- senderBtn.IsChecked = false;
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
- if (openFileDialog.ShowDialog() == true)
- {
- ClearPDFEditState(senderBtn);
- pdfViewControl.PDFView?.ClearSelectPDFEdit();
- pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
- pdfViewControl.PDFView?.ReloadDocument();
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
- pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
- }
- }
- }
- private void UndoBtn_Click(object sender, RoutedEventArgs e)
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- pdfViewControl.PDFView.UndoManager?.Undo();
- }
- }
- private void RedoBtn_Click(object sender, RoutedEventArgs e)
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- pdfViewControl.PDFView.UndoManager?.Redo();
- }
- }
- private void ClearPDFEditState(ToggleButton ignoreBtn = null)
- {
- List<ToggleButton> clearBtnList = new List<ToggleButton>()
- {
- PDFTextEditButton,
- PDFImageEditButton
- };
- foreach (ToggleButton item in clearBtnList)
- {
- if (ignoreBtn == item)
- {
- continue;
- }
- item.IsChecked = false;
- }
- }
- private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
- {
- AnnotationBarControl.ClearAllToolState();
- }
- private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
- {
- pdfAnnotationControl.AnnotationCancel();
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- RightPanelButton.IsChecked = false;
- ViewSettingBtn.IsChecked = false;
- }
- #endregion
- #region ContextMenu
- /// <summary>
- /// Annotation Context menu
- /// </summary>
- private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.AnnotCommandArgs e)
- {
- switch (e.CommandType)
- {
- case CommandType.Context:
- e.Handle = true;
- if (e.CommandTarget == TargetType.Annot)
- {
- e.Handle = true;
- e.PopupMenu = new ContextMenu();
- if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
- {
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- MenuItem propertyMenu = new MenuItem();
- propertyMenu = new MenuItem();
- propertyMenu.Header = "Edit";
- WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
- propertyMenu.CommandParameter = e;
- e.PopupMenu.Items.Add(propertyMenu);
- }
- else if (e.PressOnAnnot)
- {
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
- }
- else if (e.PressOnMedia || e.PressOnSound)
- {
- e.Handle = true;
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- }
- else if (e.PressOnSelectedText)
- {
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
- }
- else
- {
- e.Handle = true;
- e.PopupMenu = new ContextMenu();
- if (currentMode == "Annotations")
- {
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
- e.PopupMenu.Items.Add(new Separator());
- }
- MenuItem fitWidthMenu = new MenuItem();
- fitWidthMenu.Header = "Automatically Resize";
- fitWidthMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
- }
- };
- e.PopupMenu.Items.Add(fitWidthMenu);
- MenuItem fitSizeMenu = new MenuItem();
- fitSizeMenu.Header = "Actual Size";
- fitSizeMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
- }
- };
- e.PopupMenu.Items.Add(fitSizeMenu);
- MenuItem zoomInMenu = new MenuItem();
- zoomInMenu.Header = "Zoom In";
- zoomInMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
- pdfViewControl.PDFView?.Zoom(newZoom);
- }
- };
- e.PopupMenu.Items.Add(zoomInMenu);
- MenuItem zoomOutMenu = new MenuItem();
- zoomOutMenu.Header = "Zoom Out";
- zoomOutMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
- pdfViewControl.PDFView?.Zoom(newZoom);
- }
- };
- e.PopupMenu.Items.Add(zoomOutMenu);
- e.PopupMenu.Items.Add(new Separator());
- MenuItem singleView = new MenuItem();
- singleView.Header = "Single Page";
- singleView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
- }
- };
- e.PopupMenu.Items.Add(singleView);
- MenuItem singleContinuousView = new MenuItem();
- singleContinuousView.Header = "Single Page Continuous";
- singleContinuousView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
- }
- };
- e.PopupMenu.Items.Add(singleContinuousView);
- MenuItem doubleView = new MenuItem();
- doubleView.Header = "Two Pages";
- doubleView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
- }
- };
- e.PopupMenu.Items.Add(doubleView);
- MenuItem doubleContinuousView = new MenuItem();
- doubleContinuousView.Header = "Two Pages Continuous";
- doubleContinuousView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
- }
- };
- e.PopupMenu.Items.Add(doubleContinuousView);
- }
- }
- 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<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
- imageCopyMenu.CommandParameter = e;
- e.PopupMenu.Items.Add(imageCopyMenu);
- MenuItem imageExtraMenu = new MenuItem();
- imageExtraMenu = new MenuItem();
- imageExtraMenu.Header = "Extract Images";
- WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
- imageExtraMenu.CommandParameter = e;
- e.PopupMenu.Items.Add(imageExtraMenu);
- }
- }
- break;
- case CommandType.Copy:
- e.DoCommand();
- break;
- case CommandType.Cut:
- case CommandType.Paste:
- case CommandType.Delete:
- e.DoCommand();
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// TextEdit context menu.
- /// </summary>
- private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
- {
- editCommand.PopupMenu = new ContextMenu();
- if (lastPDFEditEvent != null)
- {
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
- }
- else
- {
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
- if (editCommand.TextAreaCopied)
- {
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
- }
- }
- }
- /// <summary>
- /// ImageEdit right-click menu.
- /// </summary>
- private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
- {
- editCommand.PopupMenu = new ContextMenu();
- if (lastPDFEditEvent != null)
- {
- MenuItem rotateLeftMenu = new MenuItem();
- rotateLeftMenu.Header = "Left Rotate";
- rotateLeftMenu.Click += (o, p) =>
- {
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- lastPDFEditEvent.Rotate = -90;
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- imageEditControl?.SetImageThumb();
- }
- };
- editCommand.PopupMenu.Items.Add(rotateLeftMenu);
- MenuItem rotateRightMenu = new MenuItem();
- rotateRightMenu.Header = "Right Rotate";
- rotateRightMenu.Click += (o, p) =>
- {
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- lastPDFEditEvent.Rotate = 90;
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- imageEditControl?.SetImageThumb();
- }
- };
- editCommand.PopupMenu.Items.Add(rotateRightMenu);
- MenuItem replaceMenu = new MenuItem();
- replaceMenu.Header = "Replace";
- replaceMenu.Click += (o, p) =>
- {
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
- if (openFileDialog.ShowDialog() == true)
- {
- lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- pdfViewControl.PDFView?.ClearSelectPDFEdit();
- imageEditControl?.SetImageThumb();
- }
- }
- };
- editCommand.PopupMenu.Items.Add(replaceMenu);
- MenuItem exportMenu = new MenuItem();
- exportMenu.Header = "Export";
- exportMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView.GetSelectedImages();
- if (imageDict != null && imageDict.Count > 0)
- {
- System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
- if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- string choosePath = folderBrowser.SelectedPath;
- string openPath = choosePath;
- foreach (int pageIndex in imageDict.Keys)
- {
- List<Bitmap> 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 + "\"");
- }
- }
- }
- };
- editCommand.PopupMenu.Items.Add(exportMenu);
- MenuItem opacityMenu = new MenuItem();
- opacityMenu.Header = "Opacity";
- editCommand.PopupMenu.Items.Add(opacityMenu);
- AppendOpacityMenu(opacityMenu);
- MenuItem horizonMirror = new MenuItem();
- horizonMirror.Header = "HMirror";
- horizonMirror.Click += (o, p) =>
- {
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- lastPDFEditEvent.HorizontalMirror = true;
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- }
- };
- editCommand.PopupMenu.Items.Add(horizonMirror);
- MenuItem verticalMirror = new MenuItem();
- verticalMirror.Header = "VMirror";
- verticalMirror.Click += (o, p) =>
- {
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- lastPDFEditEvent.VerticalMirror = true;
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- }
- };
- editCommand.PopupMenu.Items.Add(verticalMirror);
- MenuItem cropMenu = new MenuItem();
- cropMenu.Header = "Crop";
- cropMenu.Click += (o, p) =>
- {
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- lastPDFEditEvent.ClipImage = true;
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- }
- };
- editCommand.PopupMenu.Items.Add(cropMenu);
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- }
- else
- {
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
- }
- }
- /// <summary>
- /// ImageEdit transparency submenu.
- /// </summary>
- private void AppendOpacityMenu(MenuItem parentMenu)
- {
- List<int> opacityList = new List<int>()
- {
- 25,50,75,100
- };
- foreach (int opacity in opacityList)
- {
- MenuItem opacityMenu = new MenuItem();
- opacityMenu.Header = string.Format("{0}%", opacity);
- opacityMenu.Click += (o, p) =>
- {
- if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
- {
- lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- }
- };
- parentMenu.Items.Add(opacityMenu);
- }
- }
- 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;
- Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
- if (imageDict != null && imageDict.Count > 0)
- {
- foreach (int pageIndex in imageDict.Keys)
- {
- List<Bitmap> imageList = imageDict[pageIndex];
- foreach (Bitmap image in imageList)
- {
- string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
- image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
- openPath = savePath;
- }
- }
- }
- Process.Start("explorer", "/select,\"" + openPath + "\"");
- }
- }
- private void CopyImage_Click(object sender, RoutedEventArgs e)
- {
- Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
- if (imageDict != null && imageDict.Count > 0)
- {
- foreach (int pageIndex in imageDict.Keys)
- {
- List<Bitmap> 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;
- }
- }
- }
- }
- #endregion
- #region Property changed
- protected void OnPropertyChanged([CallerMemberName] string name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- #endregion
- #region Open and save file
- public void InitWithFilePath(string filePath)
- {
- pdfViewControl = new PDFViewControl();
- pdfViewControl.PDFView.InitDocument(filePath);
- }
- private void LoadDocument()
- {
- //Load the Document
- pdfViewControl.PDFView?.Load();
- //Highlight the link annotation
- pdfViewControl.PDFView?.SetShowLink(true);
- //Add PDFViewControl to PDFGrid
- PDFGrid.Child = pdfViewControl;
- // Register the InfoChanged event handler,
- // which is used to notify the CPDFScalingControl of real-time changes in the displayed number when the page size changes.
- pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
- pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
- //
- pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
- pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
- 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.Confirmed += PasswordUI_Confirmed;
- PasswordUI.Canceled += PasswordUI_Canceled;
- PasswordUI.Closed += PasswordUI_Closed;
- pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
- CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
- CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
- InitialPDFViewControl(pdfViewControl);
- pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
- FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
- BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
- BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
- ViewSettingBtn.IsChecked = false;
- PropertyContainer.Child = null;
- PropertyContainer.Visibility = Visibility.Collapsed;
- }
- /// <summary>
- /// Save the file to another PDF file.
- /// </summary>
- 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);
- }
- }
- }
- }
- /// <summary>
- /// Save the file in the current path.
- /// </summary>
- public 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);
- }
- }
- }
- public void OpenFile()
- {
- string filePath = CommonHelper.GetFilePathOrEmpty();
- string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
- if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
- {
- if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
- {
- if (oldFilePath.ToLower() == filePath.ToLower())
- {
- return;
- }
- }
- if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
- {
- 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
- {
- pdfViewControl = passwordViewer;
- LoadDocument();
- FileChangeEvent?.Invoke(null, EventArgs.Empty);
- }
- }
- }
- private void OpenFile_Click(object sender, RoutedEventArgs e)
- {
- OpenFile();
- }
- #endregion
- #region ToolPanel expand and collapse
- 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(320);
- BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
- }
- else
- {
- BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
- BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
- }
- }
- private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton expandBtn = sender as ToggleButton;
- if (expandBtn != null)
- {
- bool isExpand = expandBtn.IsChecked == true;
- ExpandLeftPanel(isExpand);
- }
- }
- private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Search);
- }
- private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton toggleButton = sender as ToggleButton;
- if (toggleButton != null)
- {
- if (toggleButton.IsChecked == true)
- {
- if (displayPanel == null)
- {
- displayPanel = new CPDFDisplaySettingsControl();
- }
- displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
- PropertyContainer.Child = displayPanel;
- PropertyContainer.Visibility = Visibility.Visible;
- if ((bool)RightPanelButton.IsChecked)
- {
- RightPanelButton.IsChecked = false;
- }
- }
- else
- {
- PropertyContainer.Child = null;
- PropertyContainer.Visibility = Visibility.Collapsed;
- }
- }
- }
- public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
- {
- PropertyContainer.Width = 260;
- PropertyContainer.Child = properytPanel;
- PropertyContainer.Visibility = visible;
- if (visible == Visibility.Hidden || visible == Visibility.Collapsed)
- {
- RightPanelButton.IsChecked = false;
- }
- }
- private void RightPanelButton_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton toggleButton = sender as ToggleButton;
- if (toggleButton != null)
- {
- if (toggleButton.IsChecked == true)
- {
- if (currentMode == "Annotation")
- {
- ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
- }
- else if (currentMode == "Edit")
- {
- ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
- }
- else if (currentMode == "Forms")
- {
- ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
- }
- else
- {
- ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
- }
- if ((bool)ViewSettingBtn.IsChecked)
- {
- ViewSettingBtn.IsChecked = false;
- }
- }
- else
- {
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- }
- }
- }
- #endregion
- #region Event handle
- private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
- {
- pdfAnnotationControl.LoadAnnotationPanel(e);
- if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
- {
- ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
- RightPanelButton.IsChecked = true;
- }
- }
- private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
- {
- PropertyContainer.Child = pdfAnnotationControl;
- pdfAnnotationControl.SetAnnotEventData(e);
- }
- private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
- {
- if (e is WidgetSignArgs)
- {
- RightPanelButton.IsChecked = true;
- ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
- pdfFormControl.SetPropertyForType(e, null);
- }
- else
- {
- RightPanelButton.IsChecked = false;
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- }
- }
- private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
- {
- if (e == null)
- {
- return;
- }
- if (e.EditType == CPDFEditType.EditText)
- {
- e.Handle = true;
- PDFEditTextContextMenu(sender, e);
- }
- if (e.EditType == CPDFEditType.EditImage)
- {
- e.Handle = true;
- PDFEditImageContextMenu(sender, e);
- }
- }
- /// <summary>
- /// Text and Image Selected Event
- /// </summary>
- private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e)
- {
- lastPDFEditEvent = e;
- ViewSettingBtn.IsChecked = false;
- if (e == null)
- {
- pdfEditControl = null;
- PropertyContainer.Child = pdfEditControl;
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- return;
- }
- if (Mouse.RightButton == MouseButtonState.Pressed)
- {
- return;
- }
- if (e.EditType == CPDFEditType.EditText)
- {
- RightPanelButton.IsChecked = true;
- PDFTextEditControl textEditControl = new PDFTextEditControl();
- textEditControl.SetPDFTextEditData(e);
- pdfEditControl = textEditControl as UIElement;
- PropertyContainer.Child = pdfEditControl;
- ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
- return;
- }
- if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
- {
- RightPanelButton.IsChecked = true;
- UIElement pageView = sender as UIElement;
- if (pageView != null)
- {
- pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; ;
- }
- return;
- }
- }
- private void PDFView_FormEditHandler(object sender, List<AnnotEditEvent> e)
- {
- if (e != null && e.Count > 0)
- {
- AnnotEditEvent editEvent = e[e.Count - 1];
- if (editEvent.EditAction == ActionType.Add)
- {
- pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
- }
- }
- }
- private void PDFView_FormActiveHandler(object sender, AnnotAttribEvent e)
- {
- if (e == null || e.IsAnnotCreateReset)
- {
- pdfFormControl.SetPropertyForType(null, null);
- }
- else
- {
- switch (e.GetAnnotTypes())
- {
- case AnnotArgsType.WidgetViewForm:
- WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
- switch (formArgs.WidgeType)
- {
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
- ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
- if ((bool)ViewSettingBtn.IsChecked)
- {
- ViewSettingBtn.IsChecked = false;
- }
- RightPanelButton.IsChecked = true;
- break;
- default:
- break;
- }
- pdfFormControl.SetPropertyForType(formArgs, e);
- break;
- }
- }
- }
- private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
- {
- ModeComboBox.SelectedIndex = 0;
- }
- private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
- {
- if (pdfViewControl == null || pdfViewControl.PDFView == null)
- {
- return;
- }
- BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
- }
- private void CPDFPageEditBarControl_PageEditEvent(object sender, string e)
- {
- pageEditControl.PageEdit(e);
- }
- private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
- {
- SaveFile();
- }
- private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
- {
- SaveAsFile();
- }
- private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
- {
- OpenFile();
- }
- #endregion
- #region Change mode
- /// <summary>
- /// Actions performed when switching modes:
- /// Separated into actions performed upon entering and exiting the mode.
- /// </summary>
- private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (isFirstLoad)
- {
- isFirstLoad = false;
- return;
- }
- var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
- //Behavior when exit mode
- switch (currentMode)
- {
- case "Viewer":
- break;
- /*
- * Behavior when exit Edit mode
- * Clear and collapse the ToolBar
- * Clear And collapse the property panel.
- * Change the mousemode to PanTool
- * Unsubscribe the event handles
- * Clear the undo history
- */
- case "Annotations":
- AnnotationBarControl.Visibility = Visibility.Collapsed;
- AnnotationBarControl.ClearAllToolState();
- pdfAnnotationControl.ClearPanel();
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
- pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
- pdfAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
- pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
- pdfViewControl.PDFView?.UndoManager.ClearHistory();
- break;
- /*
- * Behavior when exit edit mode:
- * Collapsed the ToolBar
- * Highlight the link annotation
- * Set the mode to not allow edit
- * Set the mouse mode to PanTool
- * Reload Document
- * Unsubscribe click event.
- * Clear And collapse the property panel.
- * Clear and collapse the ToolBar
- * Clear undo history
- * CPDFDisplaySettingsControl
- * Enable the configuration of the split view.
- */
- case "Content Editor":
- PDFEditTool.Visibility = Visibility.Collapsed;
- pdfViewControl.PDFView?.SetShowLink(true);
- pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
- pdfViewControl.PDFView?.ReloadDocument();
- pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- ClearPDFEditState();
- pdfViewControl.PDFView?.UndoManager.ClearHistory();
- if (displayPanel != null)
- {
- displayPanel.SetSplitContainerVisibility(Visibility.Visible);
- }
- break;
- /*
- * Behavior when exit form mode:
- * Clear and collapsed the ToolBar
- * Clear And Collapse the property panel.
- * Unsubscribe event.
- * Clear undo history
- */
- case "Forms":
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- FormBarControl.ClearAllToolState();
- FormBarControl.Visibility = Visibility.Collapsed;
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
- pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_FormActiveHandler;
- pdfViewControl.PDFView.AnnotEditHandler -= PDFView_FormEditHandler;
- pdfViewControl.PDFView?.UndoManager.ClearHistory();
- break;
- /*
- * Unsubscribe event.
- * Show Undo Redo Button
- * Collapse PageEditBarControl
- * Replace PDFGrid.Child to pdfViewControl.
- * Reload Document
- * Enable Button
- */
- case "Docs Editor":
- pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
- pageEditControl.PageMoved -= PageEditControl_PageMoved;
- PageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent;
- UndoRedoStackPanel.Visibility = Visibility.Visible;
- PageEditBarControl.Visibility = Visibility.Collapsed;
- PDFGrid.Child = pdfViewControl;
- FloatPageTool.Visibility = Visibility.Visible;
- pdfViewControl.PDFView.ReloadDocument();
- LeftToolPanelButton.IsEnabled = true;
- SearchButton.IsEnabled = true;
- RightPanelButton.IsEnabled = true;
- ViewSettingBtn.IsEnabled = true;
- break;
- default:
- break;
- }
- /*
- * Behavior when switching to Viewer mode:
- * Hide the ToolBar
- * Set mousemode to Viewer
- */
- if ((string)item.Content == "Viewer")
- {
- ToolBarContainer.Visibility = Visibility.Collapsed;
- pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
- }
- /*
- * Behavior when switching to Annotation mode:
- * Show the ToolBar
- * Show the AnnotationBar
- */
- else if ((string)item.Content == "Annotations")
- {
- ToolBarContainer.Visibility = Visibility.Visible;
- AnnotationBarControl.Visibility = Visibility.Visible;
- pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
- pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
- pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
- pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
- }
- /*
- * Behavior when switching to Edit mode:
- * Show the ToolBar
- * Show the EditBar
- * Set the mode to allow editing of both text and images
- * Set the mouse mode to PDFEdit
- * Reload Document
- * Set the splite mode to None and unable the split setting
- */
- else if ((string)item.Content == "Content Editor")
- {
- ToolBarContainer.Visibility = Visibility.Visible;
- PDFEditTool.Visibility = Visibility.Visible;
- PageEditBarControl.Visibility = Visibility.Collapsed;
- pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
- pdfViewControl.PDFView?.ReloadDocument();
- pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
- pdfViewControl.PDFView.SetSplitMode(SplitMode.None);
- if (displayPanel != null)
- {
- displayPanel.SetSplitContainerVisibility(Visibility.Collapsed);
- }
- }
- /*
- * Behavior when switching to Form mode:
- *
- */
- else if ((string)item.Content == "Forms")
- {
- ToolBarContainer.Visibility = Visibility.Visible;
- FormBarControl.Visibility = Visibility.Visible;
- pdfViewControl.PDFView?.SetShowLink(true);
- pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
- pdfViewControl.PDFView?.ReloadDocument();
- pdfViewControl.PDFView.AnnotActiveHandler += PDFView_FormActiveHandler;
- pdfViewControl.PDFView.AnnotEditHandler += PDFView_FormEditHandler;
- }
- /*
- *
- */
- else if ((string)item.Content == "Docs Editor")
- {
- ToolBarContainer.Visibility = Visibility.Visible;
- if (pageEditControl == null)
- {
- pageEditControl = new CPDFPageEditControl();
- }
- pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit; ;
- pageEditControl.PageMoved += PageEditControl_PageMoved; ;
- PageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent;
- pageEditControl.LoadThumbnails(pdfViewControl.PDFView);
- PDFGrid.Child = pageEditControl;
- UndoRedoStackPanel.Visibility = Visibility.Collapsed;
- PageEditBarControl.Visibility = Visibility.Visible;
- LeftToolPanelButton.IsChecked = false;
- LeftToolPanelButton.IsEnabled = false;
- SearchButton.IsEnabled = false;
- RightPanelButton.IsChecked = false;
- RightPanelButton.IsEnabled = false;
- ViewSettingBtn.IsChecked = false;
- ViewSettingBtn.IsEnabled = false;
- ExpandLeftPanel(false);
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- FloatPageTool.Visibility = Visibility.Collapsed;
- }
- currentMode = (string)item.Content;
- }
- #endregion
- }
- }
|