using Dragablz; using ComPDFKitDemo.AnntControl; using ComPDFKitDemo.PageEditor; using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; using ComPDFKit.PDFAnnotation; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using ComPDFKitViewer.AnnotView.Form; using ComPDFKit.PDFAnnotation.Form; using ComPDFKitDemo.Helper; using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using System.Linq; using System.Threading.Tasks; using ComPDFKitDemo.Encryption; using System.Diagnostics; namespace ComPDFKitDemo { public partial class MainWindow : Window,INotifyPropertyChanged { private TabItem currentTab; private PDFViewerControl currentViewerCtrl; public List filePathList; public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string name = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } public MainWindow() { InitializeComponent(); filePathList = new List(); DataContext = this; System.Windows.Forms.Application.EnableVisualStyles(); } #region Event private void btnCollapsed_Clicked(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.CloseNavibar(); } } private void btnExpand_Clicked(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.OpenNavibar(); } } private void PDFTab_SelectionChanged(object sender, SelectionChangedEventArgs e) { PDFViewerControl newViewer = GetCurrentViewer(); if(currentViewerCtrl == newViewer) return; if (currentTab != null) { StackPanel sp = currentTab.Header as StackPanel; } currentViewerCtrl = GetCurrentViewer(); if(currentViewerCtrl!=null) { ToolbarBottom.IsEnabled = true; UpdateSidewindowButtons(); PagesTool.PdfViewer = currentViewerCtrl.currentViewer; ZoomTool.PdfViewer = currentViewerCtrl.currentViewer; ViewModelTool.PdfViewer = currentViewerCtrl.currentViewer; FitPageTool.PdfViewer = currentViewerCtrl.currentViewer; OnPropertyChanged("CanRedo"); OnPropertyChanged("CanUndo"); TextEditTool.IsChecked = false; TextSelectTool.IsChecked = false; FormEditTool.IsChecked = false; PanTool.IsChecked = false; SelectTool.IsChecked = false; SnapTool.IsChecked = false; switch (currentViewerCtrl.currentViewer.MouseMode) { case MouseModes.TextEdit: TextEditTool.IsChecked = true; HideToolsPanel(); break; case MouseModes.SelectTextTool: TextSelectTool.IsChecked = true; HideToolsPanel(); break; case MouseModes.FormEditTool: FormEditTool.IsChecked = true; FormEditTool_Clicked(FormEditTool,new RoutedEventArgs()); break; case MouseModes.PanTool: PanTool.IsChecked = true; PanTool_Clicked(PanTool, new RoutedEventArgs()); break; default: break; } } else { ToolbarBottom.IsEnabled = true; PagesTool.PdfViewer = null; ZoomTool.PdfViewer = null; ViewModelTool.PdfViewer = null; FitPageTool.PdfViewer = null; } } private void btnOpen_Clicked(object sender, RoutedEventArgs e) { var dlg = new OpenFileDialog(); dlg.Multiselect = false; dlg.Filter = ComPDFKitDemo.Properties.Resources.OpenDialogFilter; if (dlg.ShowDialog() == true) { if (filePathList.Contains(dlg.FileName)) return; PDFViewerControl pdfViewer = new PDFViewerControl(this); if(pdfViewer.OpenPDF(dlg.FileName)) { string fileName = dlg.FileName.Substring(dlg.FileName.LastIndexOf("\\") + 1); LoadViewer(pdfViewer, fileName); } CPDFDocument document = CPDFDocument.InitWithFilePath(dlg.FileName); CPDFPermissionsInfo permission = document.GetPermissionsInfo(); Trace.WriteLine("AllowsCommenting = " + permission.AllowsCommenting); Trace.WriteLine("AllowsCopying =" + permission.AllowsCopying); Trace.WriteLine("AllowsDocumentAssembly =" + permission.AllowsDocumentAssembly); Trace.WriteLine("AllowsDocumentChanges =" + permission.AllowsDocumentChanges); Trace.WriteLine("AllowsFormFieldEntry =" + permission.AllowsFormFieldEntry); Trace.WriteLine("AllowsHighQualityPrinting =" + permission.AllowsHighQualityPrinting); Trace.WriteLine("AllowsPrinting =" + permission.AllowsPrinting); } } private void btnClose_Clicked(ItemActionCallbackArgs args) { TabItem tab = null; PDFViewerControl view = null; tab = args.DragablzItem.DataContext as TabItem; if(tab!=null) { view = GetViewerForTab(tab); } if (PDFTab.Items.Count == 1) { PDFTab.Items.Remove(tab); args.Cancel(); } if (view!=null) { view.ClosePDF(); } } private void btnSave_Clicked(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); if (view != null) view.currentViewer.Document.WriteToLoadedPath(); } private void ClearMouseToolState() { TextEditTool.IsChecked = false; TextSelectTool.IsChecked = false; FormEditTool.IsChecked = false; PanTool.IsChecked = false; SelectTool.IsChecked = false; SnapTool.IsChecked = false; OtherTools.IsChecked = false; } private void HideToolsPanel() { AnnotTools.Visibility = Visibility.Collapsed; FormTools.Visibility = Visibility.Collapsed; OtherToolsPanel.Visibility = Visibility.Collapsed; } private void TextEditTool_Clicked(object sender, RoutedEventArgs e) { ClearMouseToolState(); TextEditTool.IsChecked = true; ClearToggleButtonState(AnnotTools); PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.NoneSplit(); view.SetMouseMode(MouseModes.TextEdit); HideToolsPanel(); } } private void TextSelectTool_Clicked(object sender, RoutedEventArgs e) { ClearMouseToolState(); TextSelectTool.IsChecked = true; ClearToggleButtonState(AnnotTools); PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.SetMouseMode(MouseModes.SelectTextTool); HideToolsPanel(); } } private void FormEditTool_Clicked(object sender, RoutedEventArgs e) { ClearMouseToolState(); FormEditTool.IsChecked = true; ClearToggleButtonState(AnnotTools); PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.SetMouseMode(MouseModes.FormEditTool); HideToolsPanel(); FormTools.Visibility = Visibility.Visible; } } private void PanTool_Clicked(object sender, RoutedEventArgs e) { ClearMouseToolState(); PanTool.IsChecked = true; ClearToggleButtonState(AnnotTools); ClearToggleButtonState(FormTools); PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.SetMouseMode(MouseModes.PanTool); HideToolsPanel(); AnnotTools.Visibility = Visibility.Visible; } } private void EnterPageEdit_Click(object sender, RoutedEventArgs e) { try { var item = PDFTab.SelectedItem as TabItem; if (item == null) return; var content = item.Content as Grid; if (content.Children.Count >= 2) { var editor = content.Children[1] as PageEditControl; if (editor != null) editor.Visibility = Visibility.Visible; return; } PageEditControl pageEditControl = new PageEditControl(); pageEditControl.LoadThumbnails(currentViewerCtrl.currentViewer); pageEditControl.DragAddTab += PageEditControl_DragAddTab; content.Children.Add(pageEditControl); pageEditControl.Visibility = Visibility.Visible; return; } catch (Exception ex) { return; } } private void PageEditControl_DragAddTab(object sender, RoutedEventArgs e) { try { var path = sender as string; PDFViewerControl pdfViewer = new PDFViewerControl(this); if (pdfViewer.OpenPDF(path)) { string fileName = path.Substring(path.LastIndexOf("\\") + 1); LoadViewer(pdfViewer, fileName); } } catch (Exception ex) { } } private void ExitPageEdit_Click(object sender, RoutedEventArgs e) { var item = PDFTab.SelectedItem as TabItem; if (item == null) return; var content = item.Content as Grid; if (!(content.Children.Count >= 2)) { return; } var realchild = content.Children[1] as PageEditControl; if (realchild != null) realchild.Visibility = Visibility.Collapsed; } #endregion #region Method private void LoadViewer(PDFViewerControl view, string title) { TabItem item = new TabItem(); // add a close button StackPanel tabHeaderStack = new StackPanel(); tabHeaderStack.Orientation = Orientation.Horizontal; TextBlock tabLabel = new TextBlock(); tabLabel.Text = title; tabHeaderStack.Children.Add(tabLabel); item.Header = tabHeaderStack; Grid grid = new Grid(); grid.Children.Add(view); item.Content = grid; PDFTab.Items.Add(item); item.IsSelected = true; } public PDFViewerControl GetCurrentViewer() { PDFViewerControl view = null; for (int i = 0; i < PDFTab.Items.Count; ++i) { TabItem current = (TabItem)PDFTab.Items.GetItemAt(i); if (current.IsSelected) { StackPanel sp = current.Header as StackPanel; Grid g = (Grid)current.Content; view = (PDFViewerControl)g.Children[0]; view.CurrentUndoManager.PropertyChanged -= CurrentUndoManager_PropertyChanged; view.CurrentUndoManager.PropertyChanged += CurrentUndoManager_PropertyChanged; currentTab = current; break; } } return view; } private void CurrentUndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e) { OnPropertyChanged(e.PropertyName); } public bool CanUndo { get { PDFViewerControl view = GetCurrentViewer(); if (view != null) { return view.CurrentUndoManager.CanUndo; } return false; } } public bool CanRedo { get { PDFViewerControl view = GetCurrentViewer(); if (view != null) { return view.CurrentUndoManager.CanRedo; } return false; } } public void UpdateSidewindowButtons() { PDFViewerControl view = GetCurrentViewer(); if (view == null) return; double length = view.NaviWidth; if (length > 10) { btn_collapse.IsEnabled = true; } else { btn_collapse.IsEnabled = false; } if (length < 200) { btn_expand.IsEnabled = true; } else { btn_expand.IsEnabled = false; } } public PDFViewerControl GetViewerForTab(TabItem tab) { StackPanel sp = tab.Header as StackPanel; Grid g = (Grid)tab.Content; return (PDFViewerControl)g.Children[0]; } private TabItem GetCurrentTab() { TabItem item = null; for (int i = 0; i < PDFTab.Items.Count; ++i) { item = (TabItem)PDFTab.Items.GetItemAt(i); if (item.IsSelected) { break; } } return item; } #endregion private void AnnotDemo_Click(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); ToggleButton clickBtn = sender as ToggleButton; if (view != null) { AnnotHandlerEventArgs annotArgs = null; Dictionary annotAttribsList = new Dictionary(); switch (clickBtn.Tag.ToString()) { case "Square": annotArgs = new SquareAnnotArgs(); ((SquareAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((SquareAnnotArgs)annotArgs).BgColor = GetCurrentViewer().AnnotSetControl.GetBgColor(); ((SquareAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth(); ((SquareAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color]= ((SquareAnnotArgs)annotArgs).LineColor; annotAttribsList[AnnotAttrib.FillColor] = ((SquareAnnotArgs)annotArgs).BgColor; annotAttribsList[AnnotAttrib.Thickness]=((SquareAnnotArgs)annotArgs).LineWidth; annotAttribsList[AnnotAttrib.Transparency]= ((SquareAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Circle": annotArgs = new CircleAnnotArgs(); ((CircleAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((CircleAnnotArgs)annotArgs).BgColor = GetCurrentViewer().AnnotSetControl.GetBgColor(); ((CircleAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth(); ((CircleAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color] = ((CircleAnnotArgs)annotArgs).LineColor; annotAttribsList[AnnotAttrib.FillColor]= ((CircleAnnotArgs)annotArgs).BgColor; annotAttribsList[AnnotAttrib.Thickness] = ((CircleAnnotArgs)annotArgs).LineWidth; annotAttribsList[AnnotAttrib.Transparency] = ((CircleAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Line": annotArgs = new LineAnnotArgs(); ((LineAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((LineAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth(); ((LineAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color] = ((LineAnnotArgs)annotArgs).LineColor; annotAttribsList[AnnotAttrib.Thickness] = ((LineAnnotArgs)annotArgs).LineWidth; annotAttribsList[AnnotAttrib.Transparency]= ((LineAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "LineArrow": annotArgs = new LineAnnotArgs(); ((LineAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((LineAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth(); ((LineAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); ((LineAnnotArgs)annotArgs).TailLineType = C_LINE_TYPE.LINETYPE_ARROW; annotAttribsList[AnnotAttrib.Color] = ((LineAnnotArgs)annotArgs).LineColor; annotAttribsList[AnnotAttrib.LineEnd]= ((LineAnnotArgs)annotArgs).TailLineType; annotAttribsList[AnnotAttrib.Thickness] = ((LineAnnotArgs)annotArgs).LineWidth; annotAttribsList[AnnotAttrib.Transparency] = ((LineAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Ink": annotArgs = new FreehandAnnotArgs(); ((FreehandAnnotArgs)annotArgs).InkColor = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((FreehandAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth(); ((FreehandAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color] = ((FreehandAnnotArgs)annotArgs).InkColor; annotAttribsList[AnnotAttrib.Thickness] = ((FreehandAnnotArgs)annotArgs).LineWidth; annotAttribsList[AnnotAttrib.Transparency] = ((FreehandAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Erase": annotArgs = new EraseArgs(); ((EraseArgs)annotArgs).UIBorderColor = Colors.Gray; ((EraseArgs)annotArgs).UIFillColor = Colors.Green; ((EraseArgs)annotArgs).Thickness = 5; ((EraseArgs)annotArgs).UIOpacity = 1; break; case "FreeText": annotArgs = new FreeTextAnnotArgs(); ((FreeTextAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((FreeTextAnnotArgs)annotArgs).BgColor = GetCurrentViewer().AnnotSetControl.GetBgColor(); ((FreeTextAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth(); ((FreeTextAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); ((FreeTextAnnotArgs)annotArgs).Align = TextAlignment.Left; ((FreeTextAnnotArgs)annotArgs).FontColor = Colors.Red; ((FreeTextAnnotArgs)annotArgs).FontStyle = FontStyles.Normal; annotAttribsList[AnnotAttrib.Color] = ((FreeTextAnnotArgs)annotArgs).LineColor; annotAttribsList[AnnotAttrib.FillColor] = ((FreeTextAnnotArgs)annotArgs).BgColor; annotAttribsList[AnnotAttrib.Thickness] = ((FreeTextAnnotArgs)annotArgs).LineWidth; annotAttribsList[AnnotAttrib.Transparency] = ((FreeTextAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.FontColor] = ((FreeTextAnnotArgs)annotArgs).FontColor; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Highlight": annotArgs = new TextHighlightAnnotArgs(); ((TextHighlightAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((TextHighlightAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color] = ((TextHighlightAnnotArgs)annotArgs).Color; annotAttribsList[AnnotAttrib.Transparency] = ((TextHighlightAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Underline": annotArgs = new TextUnderlineAnnotArgs(); ((TextUnderlineAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((TextUnderlineAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color] = ((TextUnderlineAnnotArgs)annotArgs).Color; annotAttribsList[AnnotAttrib.Transparency] = ((TextUnderlineAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Strikeout": annotArgs = new TextStrikeoutAnnotArgs(); ((TextStrikeoutAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((TextStrikeoutAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color]= ((TextStrikeoutAnnotArgs)annotArgs).Color; annotAttribsList[AnnotAttrib.Transparency] = ((TextStrikeoutAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Squiggly": annotArgs = new TextSquigglyAnnotArgs(); ((TextSquigglyAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((TextSquigglyAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); annotAttribsList[AnnotAttrib.Color] = ((TextSquigglyAnnotArgs)annotArgs).Color; annotAttribsList[AnnotAttrib.Transparency] = ((TextSquigglyAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "StickyNote": annotArgs = new StickyAnnotArgs(); ((StickyAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((StickyAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); ((StickyAnnotArgs)annotArgs).StickyNote = "hello world"; annotAttribsList[AnnotAttrib.Color] = ((StickyAnnotArgs)annotArgs).Color; annotAttribsList[AnnotAttrib.Transparency] = ((StickyAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Picture": annotArgs = new StampAnnotArgs(); StampAnnotArgs stampArgs = new StampAnnotArgs(); stampArgs.Opacity = 1; stampArgs.Type = StampType.IMAGE_STAMP; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;"; if (openFileDialog.ShowDialog() == true) { stampArgs.ImagePath = openFileDialog.FileName; annotArgs = stampArgs; } break; case "Stamp": annotArgs = new StampAnnotArgs(); ((StampAnnotArgs)annotArgs).Type = StampType.TEXT_STAMP; ((StampAnnotArgs)annotArgs).StampText = "Stamp Text"; ((StampAnnotArgs)annotArgs).StampTextDate = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd HH:mm:ss"); int stampWidth = 0; int stampHeight = 0; int width = 252; int height = 96; ((StampAnnotArgs)annotArgs).ImageArray = GetCurrentViewer().currentViewer.GetTextStampImage("Stamp Text", ((StampAnnotArgs)annotArgs).StampTextDate, C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED, out stampWidth, out stampHeight, out width, out height); ((StampAnnotArgs)annotArgs).TextColor = TextStampColor.TEXTSTAMP_RED; ((StampAnnotArgs)annotArgs).TextSharp = TextStampSharp.TEXTSTAMP_RECT; ((StampAnnotArgs)annotArgs).ImageWidth = width; ((StampAnnotArgs)annotArgs).ImageHeight = height; ((StampAnnotArgs)annotArgs).MaxWidth = (int)Helpers.GetDpiUnrelatedNum(stampWidth / 72D * Helpers.Dpi); ((StampAnnotArgs)annotArgs).MaxHeight = (int)Helpers.GetDpiUnrelatedNum(height / 72D * Helpers.Dpi); annotAttribsList[AnnotAttrib.Transparency]=1; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Sign": annotArgs = new FreehandAnnotArgs(); ((FreehandAnnotArgs)annotArgs).InkColor = GetCurrentViewer().AnnotSetControl.GetLineColor(); ((FreehandAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth(); ((FreehandAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency(); ((FreehandAnnotArgs)annotArgs).IsSaveAsStamp = true; annotAttribsList[AnnotAttrib.Color]= ((FreehandAnnotArgs)annotArgs).InkColor; annotAttribsList[AnnotAttrib.Thickness] = ((FreehandAnnotArgs)annotArgs).LineWidth; annotAttribsList[AnnotAttrib.Transparency] = ((FreehandAnnotArgs)annotArgs).Transparency; annotAttribsList[AnnotAttrib.Locked] = false; break; case "Link": annotArgs = new LinkAnnotArgs(); ((LinkAnnotArgs)annotArgs).LinkType = LINK_TYPE.GOTO; ((LinkAnnotArgs)annotArgs).DestIndex = 0; ((LinkAnnotArgs)annotArgs).URI = ""; annotAttribsList[AnnotAttrib.LinkType] = ((LinkAnnotArgs)annotArgs).LinkType; annotAttribsList[AnnotAttrib.LinkDestIndx] = ((LinkAnnotArgs)annotArgs).DestIndex; annotAttribsList[AnnotAttrib.LinkUri] = ((LinkAnnotArgs)annotArgs).URI; annotAttribsList[AnnotAttrib.Locked] = false; break; case "SelectTool": annotArgs = new SelectAnnotToolArgs(); break; case "SnapTool": annotArgs = new SnapshotToolArgs(); break; case "SnapEditTool": annotArgs = new SnapshotEditToolArgs(); (annotArgs as SnapshotEditToolArgs).ControlPointColor = Colors.White; SnapToolMenu snapMenu = new SnapToolMenu(); (annotArgs as SnapshotEditToolArgs).ToolPanel = snapMenu; snapMenu.ToolArgs = (annotArgs as SnapshotEditToolArgs); snapMenu.PDFViewer = view; break; case "Audio": { SoundAnnotArgs soundArgs = new SoundAnnotArgs(); OpenFileDialog openAudioDialog = new OpenFileDialog(); openAudioDialog.Filter = "Wave Files(*.wav;*.wave;)|*.wav;*.wave|All Files(*.*;)|*.*;"; if (openAudioDialog.ShowDialog() == true) { soundArgs.SoundFilePath = openAudioDialog.FileName; annotArgs = soundArgs; } } break; case "Redaction": { RedactionAnnotArgs redactionArgs =new RedactionAnnotArgs(); redactionArgs.LineColor = Colors.Red; redactionArgs.BgColor = Colors.Black; redactionArgs.FontColor = Colors.Red; redactionArgs.Align = TextAlignment.Right; redactionArgs.FontSize = 14; redactionArgs.Content = "hello world"; annotAttribsList[AnnotAttrib.Color] = redactionArgs.LineColor; annotAttribsList[AnnotAttrib.FontColor] = redactionArgs.LineColor; annotAttribsList[AnnotAttrib.FillColor] = redactionArgs.BgColor; annotAttribsList[AnnotAttrib.TextAlign] = redactionArgs.Align; annotAttribsList[AnnotAttrib.FontSize] = redactionArgs.FontSize; annotAttribsList[AnnotAttrib.Locked] = false; annotAttribsList[AnnotAttrib.NoteText] = redactionArgs.Content; annotArgs = redactionArgs; } break; default: break; } TextEditTool.IsChecked = false; TextSelectTool.IsChecked = false; FormEditTool.IsChecked = false; PanTool.IsChecked = false; if (clickBtn.IsChecked==true) { if (annotArgs != null) { view.SetMouseMode(MouseModes.AnnotCreate); view.SetToolParam(annotArgs); } SelectTool.IsChecked = false; SnapTool.IsChecked = false; ClearToggleButtonState(AnnotTools); ClearToggleButtonState(FormTools); if (clickBtn.Tag!=null && clickBtn.Tag.ToString() == "Stamp") { clickBtn.IsChecked = false; } else { clickBtn.IsChecked = true; } if (view.AnnotSetControl != null) { view.AnnotSetControl.lastAnnotEditEvent = null; } annotArgs.Author = "ComPDFKit"; annotAttribsList[AnnotAttrib.Author] = annotArgs.Author; view.CurrentView_AnnotActiveHandler(this, AnnotAttribEvent.GetAnnotAttribEvent(annotArgs, annotAttribsList)); } else { view.SetMouseMode(MouseModes.PanTool); PanTool.IsChecked = true; if (view.AnnotSetControl != null) { view.AnnotSetControl.lastAnnotEditEvent = null; } view.CurrentView_AnnotActiveHandler(this, new AnnotAttribEvent()); } } } private void Undo_Click(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.CurrentUndoManager.Undo(); } } private void Redo_Click(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.CurrentUndoManager.Redo(); } } private void ClearToggleButtonState(DependencyObject parentControl) { int childCount = VisualTreeHelper.GetChildrenCount(parentControl); for (int i = 0; i < childCount; i++) { var childElement = VisualTreeHelper.GetChild(parentControl, i); if (childElement is ToggleButton) { if((childElement as ToggleButton).Name== "FormHighlightBtn") { continue; } (childElement as ToggleButton).IsChecked = false; } } if(parentControl == AnnotTools) { RedactionBtn.IsChecked = false; } } private void ShowLink_Click(object sender, RoutedEventArgs e) { var button = sender as ToggleButton; PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.currentViewer.SetShowLink(button.IsChecked.Value); } } private void PageOffset_ClickEvent(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); view.currentViewer.SetPageSpacing((uint)(PageOffset.IsChecked==true? 8:0)); } private void FormTools_Click(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); ToggleButton clickBtn = sender as ToggleButton; ClearToggleButtonState(AnnotTools); view.AnnotSetControl.ProcessAnnotEvents(new AnnotAttribEvent()); foreach (ToggleButton child in FormTools.Children) { if(child.Name== "FormHighlightBtn") { continue; } if(clickBtn != child) { child.IsChecked = false; } } ClearMouseToolState(); if (view != null && clickBtn!=null) { if (clickBtn.IsChecked == true) { AnnotHandlerEventArgs annotArgs = null; switch(clickBtn.Tag.ToString()) { case "TextBox": { WidgetTextBoxArgs textArgs=new WidgetTextBoxArgs(); textArgs.BgColor = Colors.LightGray; annotArgs = textArgs; } break; case "RadioBtn": { WidgetRadioButtonArgs radioArgs = new WidgetRadioButtonArgs(); radioArgs.CheckStyle = C_CHECK_STYLE.CK_CIRCLE; radioArgs.BorderStyle = C_BORDER_STYLE.BS_SOLID; radioArgs.LineColor = Colors.Black; radioArgs.BgColor = Colors.LightGreen; radioArgs.LineWidth = 2; radioArgs.FieldName = GetRadioFieldName(); annotArgs = radioArgs; } break; case "CheckBox": { WidgetCheckBoxArgs checkBoxArgs = new WidgetCheckBoxArgs(); checkBoxArgs.CheckStyle = C_CHECK_STYLE.CK_CHECK; checkBoxArgs.BorderStyle = C_BORDER_STYLE.BS_SOLID; checkBoxArgs.LineColor = Colors.Black; checkBoxArgs.BgColor = Colors.LightGreen; checkBoxArgs.LineWidth = 1; annotArgs = checkBoxArgs; } break; case "DropDown": { WidgetComboBoxArgs dropDownArgs=new WidgetComboBoxArgs(); dropDownArgs.BgColor = Colors.Green; dropDownArgs.LineColor = Colors.Red; dropDownArgs.LineWidth = 1; annotArgs = dropDownArgs; } break; case "List": { WidgetListBoxArgs listArgs=new WidgetListBoxArgs(); listArgs.LineColor = Colors.Red; listArgs.LineWidth = 1; annotArgs = listArgs; } break; case "PushBtn": { WidgetPushButtonArgs pushButtonArgs=new WidgetPushButtonArgs(); pushButtonArgs.Text = "Push Button"; pushButtonArgs.BgColor = Colors.LightGray; annotArgs = pushButtonArgs; } break; case "Sign": { WidgetSignArgs signArgs=new WidgetSignArgs(); signArgs.BgColor= Colors.LightBlue; signArgs.LineWidth= 1; signArgs.LineColor= Colors.Red; annotArgs = signArgs; } break; default: break; } if (annotArgs != null) { view.SetMouseMode(MouseModes.FormEditTool); view.SetToolParam(annotArgs); } } else { view.SetMouseMode(MouseModes.PanTool); PanTool.IsChecked = true; if (view.AnnotSetControl != null) { view.AnnotSetControl.lastAnnotEditEvent = null; } view?.CurrentView_AnnotActiveHandler(this, new AnnotAttribEvent()); } } } private void FormHighlightBtn_Click(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); if(view != null) { view.currentViewer.SetFormFieldHighlight(FormHighlightBtn.IsChecked==true?true:false); } } private void CropButton_Click(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); ToggleButton sourceBtn = sender as ToggleButton; if (sourceBtn != null) { sourceBtn.IsChecked = false; } if (view != null && view.currentViewer != null) { CropWindow cropWindow = new CropWindow(); cropWindow.Owner = this; cropWindow.CurrentViewer = view.currentViewer; cropWindow.SetDoc(view.currentViewer.Document); cropWindow.Show(); } } private void AutoCrop_Click(object sender, RoutedEventArgs e) { PDFViewerControl view = GetCurrentViewer(); ToggleButton sourceBtn = sender as ToggleButton; if (sourceBtn != null) { view.LoadingAnimation.Visibility = Visibility.Visible; bool autoCrop = sourceBtn.IsChecked == true; Task.Factory.StartNew(() => { view.currentViewer?.SetCropMode(autoCrop); Dispatcher.Invoke(() => { view.LoadingAnimation.Visibility = Visibility.Collapsed; }); }); } } private void OtherTools_Click(object sender, RoutedEventArgs e) { ClearMouseToolState(); ClearToggleButtonState(AnnotTools); ClearToggleButtonState(FormTools); OtherTools.IsChecked = true; PDFViewerControl view = GetCurrentViewer(); if (view != null) { view.SetMouseMode(MouseModes.PanTool); HideToolsPanel(); OtherToolsPanel.Visibility = Visibility.Visible; } } private string GetRadioFieldName() { PDFViewerControl view = GetCurrentViewer(); if (view != null && view.currentViewer!=null && view.currentViewer.Document!=null) { CPDFDocument cPDFDocument = view.currentViewer.Document; int radioIndex = 0; for (int i = 0; i < cPDFDocument.PageCount; i++) { CPDFPage cPage = cPDFDocument.PageAtIndex(i, false); List annotList = cPage.GetAnnotations(); if (annotList != null && annotList.Count > 0) { List formList = annotList.AsEnumerable(). Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET) .Cast() .ToList(); radioIndex += formList.Where(x => x.WidgeType == C_WIDGET_TYPE.WIDGET_RADIOBUTTON).Count(); } } return "RadioGroup" + radioIndex.ToString(); } return string.Empty; } private void Encryption_Click(object sender, RoutedEventArgs e) { EncryptionPopup.IsOpen = true; } private void SetPassword_Click(object sender, RoutedEventArgs e) { EncryptionPopup.IsOpen = false; PDFViewerControl view = GetCurrentViewer(); if (view != null && view.currentViewer != null && view.currentViewer.Document != null) { EncryptionWindow windoow = new EncryptionWindow(view.currentViewer.Document); windoow.Owner = this; windoow.Show(); } } private void RemovePassword_Click(object sender, RoutedEventArgs e) { CPDFDocument document = null; PDFViewerControl view = GetCurrentViewer(); if (view != null && view.currentViewer != null && view.currentViewer.Document != null) document = view.currentViewer.Document; string SavePath; //创建一个保存文件的对话框 SaveFileDialog dialog = new SaveFileDialog() { Filter = "PDF Files| *.pdf", InitialDirectory = @"C:\", FileName = document.FileName + "_Decrypted" }; //调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮 if (dialog.ShowDialog().GetValueOrDefault()) { SavePath = dialog.FileName; } else { return; } if (!document.Descrypt(SavePath)) MessageBox.Show("Save file error!"); } } }