12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058 |
- 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<string> filePathList;
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged([CallerMemberName] string name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- public MainWindow()
- {
- InitializeComponent();
- filePathList = new List<string>();
- 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<TabablzControl> 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<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
- 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<CPDFAnnotation> annotList = cPage.GetAnnotations();
- if (annotList != null && annotList.Count > 0)
- {
- List<CPDFWidget> formList = annotList.AsEnumerable().
- Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
- .Cast<CPDFWidget>()
- .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!");
- }
- }
- }
|