1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540 |
- 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
- private string GetTime()
- {
- DateTime dateTime = DateTime.Now;
- return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
- }
- /// <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" || currentMode == "Forms")
- {
- 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.WidgetView)
- {
- e.Handle = true;
- e.PopupMenu = new ContextMenu();
- 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 });
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, 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<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()
- {
- if (pdfViewControl.PDFView.Document == null)
- {
- return;
- }
- //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;
- ModeComboBox.SelectedIndex = 0;
- }
- /// <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)
- {
- if (pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs != null)
- {
- WidgetArgs widgetArgs = pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs as WidgetArgs;
- if (widgetArgs != null)
- {
- switch (widgetArgs.WidgeType)
- {
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
- widgetArgs.FieldName = "Button" + GetTime();
- break;
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX:
- widgetArgs.FieldName = "Checkbox" + GetTime();
- break;
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
- widgetArgs.FieldName = "Radio button" + GetTime();
- break;
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD:
- widgetArgs.FieldName = "Text" + GetTime();
- break;
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
- widgetArgs.FieldName = "Combobox" + GetTime();
- break;
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
- widgetArgs.FieldName = "List" + GetTime();
- break;
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
- widgetArgs.FieldName = "Signature" + GetTime();
- break;
- case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_UNKNOWN:
- break;
- default:
- break;
- }
- }
- }
- pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
- }
- else if (editEvent.EditAction == ActionType.Del)
- {
- pdfFormControl.CleanProperty();
- }
- }
- }
- 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
- }
- }
|