MainWindow.xaml.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. using Dragablz;
  2. using ComPDFKitDemo.AnntControl;
  3. using ComPDFKitDemo.PageEditor;
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Runtime.CompilerServices;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Media;
  13. using ComPDFKit.PDFAnnotation;
  14. using ComPDFKitViewer;
  15. using ComPDFKitViewer.AnnotEvent;
  16. using ComPDFKitViewer.PdfViewer;
  17. using ComPDFKitViewer.AnnotView.Form;
  18. using ComPDFKit.PDFAnnotation.Form;
  19. using ComPDFKitDemo.Helper;
  20. using ComPDFKit.PDFDocument;
  21. using ComPDFKit.PDFPage;
  22. using System.Linq;
  23. using System.Threading.Tasks;
  24. using ComPDFKitDemo.Encryption;
  25. using System.Diagnostics;
  26. namespace ComPDFKitDemo
  27. {
  28. public partial class MainWindow : Window,INotifyPropertyChanged
  29. {
  30. private TabItem currentTab;
  31. private PDFViewerControl currentViewerCtrl;
  32. public List<string> filePathList;
  33. public event PropertyChangedEventHandler PropertyChanged;
  34. protected void OnPropertyChanged([CallerMemberName] string name = null)
  35. {
  36. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  37. }
  38. public MainWindow()
  39. {
  40. InitializeComponent();
  41. filePathList = new List<string>();
  42. DataContext = this;
  43. System.Windows.Forms.Application.EnableVisualStyles();
  44. }
  45. #region Event
  46. private void btnCollapsed_Clicked(object sender, RoutedEventArgs e)
  47. {
  48. PDFViewerControl view = GetCurrentViewer();
  49. if (view != null)
  50. {
  51. view.CloseNavibar();
  52. }
  53. }
  54. private void btnExpand_Clicked(object sender, RoutedEventArgs e)
  55. {
  56. PDFViewerControl view = GetCurrentViewer();
  57. if (view != null)
  58. {
  59. view.OpenNavibar();
  60. }
  61. }
  62. private void PDFTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
  63. {
  64. PDFViewerControl newViewer = GetCurrentViewer();
  65. if(currentViewerCtrl == newViewer)
  66. return;
  67. if (currentTab != null)
  68. {
  69. StackPanel sp = currentTab.Header as StackPanel;
  70. }
  71. currentViewerCtrl = GetCurrentViewer();
  72. if(currentViewerCtrl!=null)
  73. {
  74. ToolbarBottom.IsEnabled = true;
  75. UpdateSidewindowButtons();
  76. PagesTool.PdfViewer = currentViewerCtrl.currentViewer;
  77. ZoomTool.PdfViewer = currentViewerCtrl.currentViewer;
  78. ViewModelTool.PdfViewer = currentViewerCtrl.currentViewer;
  79. FitPageTool.PdfViewer = currentViewerCtrl.currentViewer;
  80. OnPropertyChanged("CanRedo");
  81. OnPropertyChanged("CanUndo");
  82. TextEditTool.IsChecked = false;
  83. TextSelectTool.IsChecked = false;
  84. FormEditTool.IsChecked = false;
  85. PanTool.IsChecked = false;
  86. SelectTool.IsChecked = false;
  87. SnapTool.IsChecked = false;
  88. switch (currentViewerCtrl.currentViewer.MouseMode)
  89. {
  90. case MouseModes.TextEdit:
  91. TextEditTool.IsChecked = true;
  92. HideToolsPanel();
  93. break;
  94. case MouseModes.SelectTextTool:
  95. TextSelectTool.IsChecked = true;
  96. HideToolsPanel();
  97. break;
  98. case MouseModes.FormEditTool:
  99. FormEditTool.IsChecked = true;
  100. FormEditTool_Clicked(FormEditTool,new RoutedEventArgs());
  101. break;
  102. case MouseModes.PanTool:
  103. PanTool.IsChecked = true;
  104. PanTool_Clicked(PanTool, new RoutedEventArgs());
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. else
  111. {
  112. ToolbarBottom.IsEnabled = true;
  113. PagesTool.PdfViewer = null;
  114. ZoomTool.PdfViewer = null;
  115. ViewModelTool.PdfViewer = null;
  116. FitPageTool.PdfViewer = null;
  117. }
  118. }
  119. private void btnOpen_Clicked(object sender, RoutedEventArgs e)
  120. {
  121. var dlg = new OpenFileDialog();
  122. dlg.Multiselect = false;
  123. dlg.Filter = ComPDFKitDemo.Properties.Resources.OpenDialogFilter;
  124. if (dlg.ShowDialog() == true)
  125. {
  126. if (filePathList.Contains(dlg.FileName))
  127. return;
  128. PDFViewerControl pdfViewer = new PDFViewerControl(this);
  129. if(pdfViewer.OpenPDF(dlg.FileName))
  130. {
  131. string fileName = dlg.FileName.Substring(dlg.FileName.LastIndexOf("\\") + 1);
  132. LoadViewer(pdfViewer, fileName);
  133. }
  134. CPDFDocument document = CPDFDocument.InitWithFilePath(dlg.FileName);
  135. CPDFPermissionsInfo permission = document.GetPermissionsInfo();
  136. Trace.WriteLine("AllowsCommenting = " + permission.AllowsCommenting);
  137. Trace.WriteLine("AllowsCopying =" + permission.AllowsCopying);
  138. Trace.WriteLine("AllowsDocumentAssembly =" + permission.AllowsDocumentAssembly);
  139. Trace.WriteLine("AllowsDocumentChanges =" + permission.AllowsDocumentChanges);
  140. Trace.WriteLine("AllowsFormFieldEntry =" + permission.AllowsFormFieldEntry);
  141. Trace.WriteLine("AllowsHighQualityPrinting =" + permission.AllowsHighQualityPrinting);
  142. Trace.WriteLine("AllowsPrinting =" + permission.AllowsPrinting);
  143. }
  144. }
  145. private void btnClose_Clicked(ItemActionCallbackArgs<TabablzControl> args)
  146. {
  147. TabItem tab = null;
  148. PDFViewerControl view = null;
  149. tab = args.DragablzItem.DataContext as TabItem;
  150. if(tab!=null)
  151. {
  152. view = GetViewerForTab(tab);
  153. }
  154. if (PDFTab.Items.Count == 1)
  155. {
  156. PDFTab.Items.Remove(tab);
  157. args.Cancel();
  158. }
  159. if (view!=null)
  160. {
  161. view.ClosePDF();
  162. }
  163. }
  164. private void btnSave_Clicked(object sender, RoutedEventArgs e)
  165. {
  166. PDFViewerControl view = GetCurrentViewer();
  167. if (view != null)
  168. view.currentViewer.Document.WriteToLoadedPath();
  169. }
  170. private void ClearMouseToolState()
  171. {
  172. TextEditTool.IsChecked = false;
  173. TextSelectTool.IsChecked = false;
  174. FormEditTool.IsChecked = false;
  175. PanTool.IsChecked = false;
  176. SelectTool.IsChecked = false;
  177. SnapTool.IsChecked = false;
  178. OtherTools.IsChecked = false;
  179. }
  180. private void HideToolsPanel()
  181. {
  182. AnnotTools.Visibility = Visibility.Collapsed;
  183. FormTools.Visibility = Visibility.Collapsed;
  184. OtherToolsPanel.Visibility = Visibility.Collapsed;
  185. }
  186. private void TextEditTool_Clicked(object sender, RoutedEventArgs e)
  187. {
  188. ClearMouseToolState();
  189. TextEditTool.IsChecked = true;
  190. ClearToggleButtonState(AnnotTools);
  191. PDFViewerControl view = GetCurrentViewer();
  192. if (view != null)
  193. {
  194. view.NoneSplit();
  195. view.SetMouseMode(MouseModes.TextEdit);
  196. HideToolsPanel();
  197. }
  198. }
  199. private void TextSelectTool_Clicked(object sender, RoutedEventArgs e)
  200. {
  201. ClearMouseToolState();
  202. TextSelectTool.IsChecked = true;
  203. ClearToggleButtonState(AnnotTools);
  204. PDFViewerControl view = GetCurrentViewer();
  205. if (view != null)
  206. {
  207. view.SetMouseMode(MouseModes.SelectTextTool);
  208. HideToolsPanel();
  209. }
  210. }
  211. private void FormEditTool_Clicked(object sender, RoutedEventArgs e)
  212. {
  213. ClearMouseToolState();
  214. FormEditTool.IsChecked = true;
  215. ClearToggleButtonState(AnnotTools);
  216. PDFViewerControl view = GetCurrentViewer();
  217. if (view != null)
  218. {
  219. view.SetMouseMode(MouseModes.FormEditTool);
  220. HideToolsPanel();
  221. FormTools.Visibility = Visibility.Visible;
  222. }
  223. }
  224. private void PanTool_Clicked(object sender, RoutedEventArgs e)
  225. {
  226. ClearMouseToolState();
  227. PanTool.IsChecked = true;
  228. ClearToggleButtonState(AnnotTools);
  229. ClearToggleButtonState(FormTools);
  230. PDFViewerControl view = GetCurrentViewer();
  231. if (view != null)
  232. {
  233. view.SetMouseMode(MouseModes.PanTool);
  234. HideToolsPanel();
  235. AnnotTools.Visibility = Visibility.Visible;
  236. }
  237. }
  238. private void EnterPageEdit_Click(object sender, RoutedEventArgs e)
  239. {
  240. try
  241. {
  242. var item = PDFTab.SelectedItem as TabItem;
  243. if (item == null)
  244. return;
  245. var content = item.Content as Grid;
  246. if (content.Children.Count >= 2)
  247. {
  248. var editor = content.Children[1] as PageEditControl;
  249. if (editor != null)
  250. editor.Visibility = Visibility.Visible;
  251. return;
  252. }
  253. PageEditControl pageEditControl = new PageEditControl();
  254. pageEditControl.LoadThumbnails(currentViewerCtrl.currentViewer);
  255. pageEditControl.DragAddTab += PageEditControl_DragAddTab;
  256. content.Children.Add(pageEditControl);
  257. pageEditControl.Visibility = Visibility.Visible;
  258. return;
  259. }
  260. catch (Exception ex)
  261. {
  262. return;
  263. }
  264. }
  265. private void PageEditControl_DragAddTab(object sender, RoutedEventArgs e)
  266. {
  267. try
  268. {
  269. var path = sender as string;
  270. PDFViewerControl pdfViewer = new PDFViewerControl(this);
  271. if (pdfViewer.OpenPDF(path))
  272. {
  273. string fileName = path.Substring(path.LastIndexOf("\\") + 1);
  274. LoadViewer(pdfViewer, fileName);
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. }
  280. }
  281. private void ExitPageEdit_Click(object sender, RoutedEventArgs e)
  282. {
  283. var item = PDFTab.SelectedItem as TabItem;
  284. if (item == null)
  285. return;
  286. var content = item.Content as Grid;
  287. if (!(content.Children.Count >= 2))
  288. {
  289. return;
  290. }
  291. var realchild = content.Children[1] as PageEditControl;
  292. if (realchild != null)
  293. realchild.Visibility = Visibility.Collapsed;
  294. }
  295. #endregion
  296. #region Method
  297. private void LoadViewer(PDFViewerControl view, string title)
  298. {
  299. TabItem item = new TabItem();
  300. // add a close button
  301. StackPanel tabHeaderStack = new StackPanel();
  302. tabHeaderStack.Orientation = Orientation.Horizontal;
  303. TextBlock tabLabel = new TextBlock();
  304. tabLabel.Text = title;
  305. tabHeaderStack.Children.Add(tabLabel);
  306. item.Header = tabHeaderStack;
  307. Grid grid = new Grid();
  308. grid.Children.Add(view);
  309. item.Content = grid;
  310. PDFTab.Items.Add(item);
  311. item.IsSelected = true;
  312. }
  313. public PDFViewerControl GetCurrentViewer()
  314. {
  315. PDFViewerControl view = null;
  316. for (int i = 0; i < PDFTab.Items.Count; ++i)
  317. {
  318. TabItem current = (TabItem)PDFTab.Items.GetItemAt(i);
  319. if (current.IsSelected)
  320. {
  321. StackPanel sp = current.Header as StackPanel;
  322. Grid g = (Grid)current.Content;
  323. view = (PDFViewerControl)g.Children[0];
  324. view.CurrentUndoManager.PropertyChanged -= CurrentUndoManager_PropertyChanged;
  325. view.CurrentUndoManager.PropertyChanged += CurrentUndoManager_PropertyChanged;
  326. currentTab = current;
  327. break;
  328. }
  329. }
  330. return view;
  331. }
  332. private void CurrentUndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  333. {
  334. OnPropertyChanged(e.PropertyName);
  335. }
  336. public bool CanUndo
  337. {
  338. get
  339. {
  340. PDFViewerControl view = GetCurrentViewer();
  341. if (view != null)
  342. {
  343. return view.CurrentUndoManager.CanUndo;
  344. }
  345. return false;
  346. }
  347. }
  348. public bool CanRedo
  349. {
  350. get
  351. {
  352. PDFViewerControl view = GetCurrentViewer();
  353. if (view != null)
  354. {
  355. return view.CurrentUndoManager.CanRedo;
  356. }
  357. return false;
  358. }
  359. }
  360. public void UpdateSidewindowButtons()
  361. {
  362. PDFViewerControl view = GetCurrentViewer();
  363. if (view == null)
  364. return;
  365. double length = view.NaviWidth;
  366. if (length > 10)
  367. {
  368. btn_collapse.IsEnabled = true;
  369. }
  370. else
  371. {
  372. btn_collapse.IsEnabled = false;
  373. }
  374. if (length < 200)
  375. {
  376. btn_expand.IsEnabled = true;
  377. }
  378. else
  379. {
  380. btn_expand.IsEnabled = false;
  381. }
  382. }
  383. public PDFViewerControl GetViewerForTab(TabItem tab)
  384. {
  385. StackPanel sp = tab.Header as StackPanel;
  386. Grid g = (Grid)tab.Content;
  387. return (PDFViewerControl)g.Children[0];
  388. }
  389. private TabItem GetCurrentTab()
  390. {
  391. TabItem item = null;
  392. for (int i = 0; i < PDFTab.Items.Count; ++i)
  393. {
  394. item = (TabItem)PDFTab.Items.GetItemAt(i);
  395. if (item.IsSelected)
  396. {
  397. break;
  398. }
  399. }
  400. return item;
  401. }
  402. #endregion
  403. private void AnnotDemo_Click(object sender, RoutedEventArgs e)
  404. {
  405. PDFViewerControl view = GetCurrentViewer();
  406. ToggleButton clickBtn = sender as ToggleButton;
  407. if (view != null)
  408. {
  409. AnnotHandlerEventArgs annotArgs = null;
  410. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  411. switch (clickBtn.Tag.ToString())
  412. {
  413. case "Square":
  414. annotArgs = new SquareAnnotArgs();
  415. ((SquareAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor();
  416. ((SquareAnnotArgs)annotArgs).BgColor = GetCurrentViewer().AnnotSetControl.GetBgColor();
  417. ((SquareAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth();
  418. ((SquareAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  419. annotAttribsList[AnnotAttrib.Color]= ((SquareAnnotArgs)annotArgs).LineColor;
  420. annotAttribsList[AnnotAttrib.FillColor] = ((SquareAnnotArgs)annotArgs).BgColor;
  421. annotAttribsList[AnnotAttrib.Thickness]=((SquareAnnotArgs)annotArgs).LineWidth;
  422. annotAttribsList[AnnotAttrib.Transparency]= ((SquareAnnotArgs)annotArgs).Transparency;
  423. annotAttribsList[AnnotAttrib.Locked] = false;
  424. break;
  425. case "Circle":
  426. annotArgs = new CircleAnnotArgs();
  427. ((CircleAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor();
  428. ((CircleAnnotArgs)annotArgs).BgColor = GetCurrentViewer().AnnotSetControl.GetBgColor();
  429. ((CircleAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth();
  430. ((CircleAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  431. annotAttribsList[AnnotAttrib.Color] = ((CircleAnnotArgs)annotArgs).LineColor;
  432. annotAttribsList[AnnotAttrib.FillColor]= ((CircleAnnotArgs)annotArgs).BgColor;
  433. annotAttribsList[AnnotAttrib.Thickness] = ((CircleAnnotArgs)annotArgs).LineWidth;
  434. annotAttribsList[AnnotAttrib.Transparency] = ((CircleAnnotArgs)annotArgs).Transparency;
  435. annotAttribsList[AnnotAttrib.Locked] = false;
  436. break;
  437. case "Line":
  438. annotArgs = new LineAnnotArgs();
  439. ((LineAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor();
  440. ((LineAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth();
  441. ((LineAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  442. annotAttribsList[AnnotAttrib.Color] = ((LineAnnotArgs)annotArgs).LineColor;
  443. annotAttribsList[AnnotAttrib.Thickness] = ((LineAnnotArgs)annotArgs).LineWidth;
  444. annotAttribsList[AnnotAttrib.Transparency]= ((LineAnnotArgs)annotArgs).Transparency;
  445. annotAttribsList[AnnotAttrib.Locked] = false;
  446. break;
  447. case "LineArrow":
  448. annotArgs = new LineAnnotArgs();
  449. ((LineAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor();
  450. ((LineAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth();
  451. ((LineAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  452. ((LineAnnotArgs)annotArgs).TailLineType = C_LINE_TYPE.LINETYPE_ARROW;
  453. annotAttribsList[AnnotAttrib.Color] = ((LineAnnotArgs)annotArgs).LineColor;
  454. annotAttribsList[AnnotAttrib.LineEnd]= ((LineAnnotArgs)annotArgs).TailLineType;
  455. annotAttribsList[AnnotAttrib.Thickness] = ((LineAnnotArgs)annotArgs).LineWidth;
  456. annotAttribsList[AnnotAttrib.Transparency] = ((LineAnnotArgs)annotArgs).Transparency;
  457. annotAttribsList[AnnotAttrib.Locked] = false;
  458. break;
  459. case "Ink":
  460. annotArgs = new FreehandAnnotArgs();
  461. ((FreehandAnnotArgs)annotArgs).InkColor = GetCurrentViewer().AnnotSetControl.GetLineColor();
  462. ((FreehandAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth();
  463. ((FreehandAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  464. annotAttribsList[AnnotAttrib.Color] = ((FreehandAnnotArgs)annotArgs).InkColor;
  465. annotAttribsList[AnnotAttrib.Thickness] = ((FreehandAnnotArgs)annotArgs).LineWidth;
  466. annotAttribsList[AnnotAttrib.Transparency] = ((FreehandAnnotArgs)annotArgs).Transparency;
  467. annotAttribsList[AnnotAttrib.Locked] = false;
  468. break;
  469. case "Erase":
  470. annotArgs = new EraseArgs();
  471. ((EraseArgs)annotArgs).UIBorderColor = Colors.Gray;
  472. ((EraseArgs)annotArgs).UIFillColor = Colors.Green;
  473. ((EraseArgs)annotArgs).Thickness = 5;
  474. ((EraseArgs)annotArgs).UIOpacity = 1;
  475. break;
  476. case "FreeText":
  477. annotArgs = new FreeTextAnnotArgs();
  478. ((FreeTextAnnotArgs)annotArgs).LineColor = GetCurrentViewer().AnnotSetControl.GetLineColor();
  479. ((FreeTextAnnotArgs)annotArgs).BgColor = GetCurrentViewer().AnnotSetControl.GetBgColor();
  480. ((FreeTextAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth();
  481. ((FreeTextAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  482. ((FreeTextAnnotArgs)annotArgs).Align = TextAlignment.Left;
  483. ((FreeTextAnnotArgs)annotArgs).FontColor = Colors.Red;
  484. ((FreeTextAnnotArgs)annotArgs).FontStyle = FontStyles.Normal;
  485. annotAttribsList[AnnotAttrib.Color] = ((FreeTextAnnotArgs)annotArgs).LineColor;
  486. annotAttribsList[AnnotAttrib.FillColor] = ((FreeTextAnnotArgs)annotArgs).BgColor;
  487. annotAttribsList[AnnotAttrib.Thickness] = ((FreeTextAnnotArgs)annotArgs).LineWidth;
  488. annotAttribsList[AnnotAttrib.Transparency] = ((FreeTextAnnotArgs)annotArgs).Transparency;
  489. annotAttribsList[AnnotAttrib.FontColor] = ((FreeTextAnnotArgs)annotArgs).FontColor;
  490. annotAttribsList[AnnotAttrib.Locked] = false;
  491. break;
  492. case "Highlight":
  493. annotArgs = new TextHighlightAnnotArgs();
  494. ((TextHighlightAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor();
  495. ((TextHighlightAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  496. annotAttribsList[AnnotAttrib.Color] = ((TextHighlightAnnotArgs)annotArgs).Color;
  497. annotAttribsList[AnnotAttrib.Transparency] = ((TextHighlightAnnotArgs)annotArgs).Transparency;
  498. annotAttribsList[AnnotAttrib.Locked] = false;
  499. break;
  500. case "Underline":
  501. annotArgs = new TextUnderlineAnnotArgs();
  502. ((TextUnderlineAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor();
  503. ((TextUnderlineAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  504. annotAttribsList[AnnotAttrib.Color] = ((TextUnderlineAnnotArgs)annotArgs).Color;
  505. annotAttribsList[AnnotAttrib.Transparency] = ((TextUnderlineAnnotArgs)annotArgs).Transparency;
  506. annotAttribsList[AnnotAttrib.Locked] = false;
  507. break;
  508. case "Strikeout":
  509. annotArgs = new TextStrikeoutAnnotArgs();
  510. ((TextStrikeoutAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor();
  511. ((TextStrikeoutAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  512. annotAttribsList[AnnotAttrib.Color]= ((TextStrikeoutAnnotArgs)annotArgs).Color;
  513. annotAttribsList[AnnotAttrib.Transparency] = ((TextStrikeoutAnnotArgs)annotArgs).Transparency;
  514. annotAttribsList[AnnotAttrib.Locked] = false;
  515. break;
  516. case "Squiggly":
  517. annotArgs = new TextSquigglyAnnotArgs();
  518. ((TextSquigglyAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor();
  519. ((TextSquigglyAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  520. annotAttribsList[AnnotAttrib.Color] = ((TextSquigglyAnnotArgs)annotArgs).Color;
  521. annotAttribsList[AnnotAttrib.Transparency] = ((TextSquigglyAnnotArgs)annotArgs).Transparency;
  522. annotAttribsList[AnnotAttrib.Locked] = false;
  523. break;
  524. case "StickyNote":
  525. annotArgs = new StickyAnnotArgs();
  526. ((StickyAnnotArgs)annotArgs).Color = GetCurrentViewer().AnnotSetControl.GetLineColor();
  527. ((StickyAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  528. ((StickyAnnotArgs)annotArgs).StickyNote = "hello world";
  529. annotAttribsList[AnnotAttrib.Color] = ((StickyAnnotArgs)annotArgs).Color;
  530. annotAttribsList[AnnotAttrib.Transparency] = ((StickyAnnotArgs)annotArgs).Transparency;
  531. annotAttribsList[AnnotAttrib.Locked] = false;
  532. break;
  533. case "Picture":
  534. annotArgs = new StampAnnotArgs();
  535. StampAnnotArgs stampArgs = new StampAnnotArgs();
  536. stampArgs.Opacity = 1;
  537. stampArgs.Type = StampType.IMAGE_STAMP;
  538. OpenFileDialog openFileDialog = new OpenFileDialog();
  539. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  540. if (openFileDialog.ShowDialog() == true)
  541. {
  542. stampArgs.ImagePath = openFileDialog.FileName;
  543. annotArgs = stampArgs;
  544. }
  545. break;
  546. case "Stamp":
  547. annotArgs = new StampAnnotArgs();
  548. ((StampAnnotArgs)annotArgs).Type = StampType.TEXT_STAMP;
  549. ((StampAnnotArgs)annotArgs).StampText = "Stamp Text";
  550. ((StampAnnotArgs)annotArgs).StampTextDate = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd HH:mm:ss");
  551. int stampWidth = 0;
  552. int stampHeight = 0;
  553. int width = 252;
  554. int height = 96;
  555. ((StampAnnotArgs)annotArgs).ImageArray = GetCurrentViewer().currentViewer.GetTextStampImage("Stamp Text", ((StampAnnotArgs)annotArgs).StampTextDate,
  556. C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED, out stampWidth, out stampHeight, out width, out height);
  557. ((StampAnnotArgs)annotArgs).TextColor = TextStampColor.TEXTSTAMP_RED;
  558. ((StampAnnotArgs)annotArgs).TextSharp = TextStampSharp.TEXTSTAMP_RECT;
  559. ((StampAnnotArgs)annotArgs).ImageWidth = width;
  560. ((StampAnnotArgs)annotArgs).ImageHeight = height;
  561. ((StampAnnotArgs)annotArgs).MaxWidth = (int)Helpers.GetDpiUnrelatedNum(stampWidth / 72D * Helpers.Dpi);
  562. ((StampAnnotArgs)annotArgs).MaxHeight = (int)Helpers.GetDpiUnrelatedNum(height / 72D * Helpers.Dpi);
  563. annotAttribsList[AnnotAttrib.Transparency]=1;
  564. annotAttribsList[AnnotAttrib.Locked] = false;
  565. break;
  566. case "Sign":
  567. annotArgs = new FreehandAnnotArgs();
  568. ((FreehandAnnotArgs)annotArgs).InkColor = GetCurrentViewer().AnnotSetControl.GetLineColor();
  569. ((FreehandAnnotArgs)annotArgs).LineWidth = GetCurrentViewer().AnnotSetControl.GetLineWidth();
  570. ((FreehandAnnotArgs)annotArgs).Transparency = GetCurrentViewer().AnnotSetControl.GetTransparency();
  571. ((FreehandAnnotArgs)annotArgs).IsSaveAsStamp = true;
  572. annotAttribsList[AnnotAttrib.Color]= ((FreehandAnnotArgs)annotArgs).InkColor;
  573. annotAttribsList[AnnotAttrib.Thickness] = ((FreehandAnnotArgs)annotArgs).LineWidth;
  574. annotAttribsList[AnnotAttrib.Transparency] = ((FreehandAnnotArgs)annotArgs).Transparency;
  575. annotAttribsList[AnnotAttrib.Locked] = false;
  576. break;
  577. case "Link":
  578. annotArgs = new LinkAnnotArgs();
  579. ((LinkAnnotArgs)annotArgs).LinkType = LINK_TYPE.GOTO;
  580. ((LinkAnnotArgs)annotArgs).DestIndex = 0;
  581. ((LinkAnnotArgs)annotArgs).URI = "";
  582. annotAttribsList[AnnotAttrib.LinkType] = ((LinkAnnotArgs)annotArgs).LinkType;
  583. annotAttribsList[AnnotAttrib.LinkDestIndx] = ((LinkAnnotArgs)annotArgs).DestIndex;
  584. annotAttribsList[AnnotAttrib.LinkUri] = ((LinkAnnotArgs)annotArgs).URI;
  585. annotAttribsList[AnnotAttrib.Locked] = false;
  586. break;
  587. case "SelectTool":
  588. annotArgs = new SelectAnnotToolArgs();
  589. break;
  590. case "SnapTool":
  591. annotArgs = new SnapshotToolArgs();
  592. break;
  593. case "SnapEditTool":
  594. annotArgs = new SnapshotEditToolArgs();
  595. (annotArgs as SnapshotEditToolArgs).ControlPointColor = Colors.White;
  596. SnapToolMenu snapMenu = new SnapToolMenu();
  597. (annotArgs as SnapshotEditToolArgs).ToolPanel = snapMenu;
  598. snapMenu.ToolArgs = (annotArgs as SnapshotEditToolArgs);
  599. snapMenu.PDFViewer = view;
  600. break;
  601. case "Audio":
  602. {
  603. SoundAnnotArgs soundArgs = new SoundAnnotArgs();
  604. OpenFileDialog openAudioDialog = new OpenFileDialog();
  605. openAudioDialog.Filter = "Wave Files(*.wav;*.wave;)|*.wav;*.wave|All Files(*.*;)|*.*;";
  606. if (openAudioDialog.ShowDialog() == true)
  607. {
  608. soundArgs.SoundFilePath = openAudioDialog.FileName;
  609. annotArgs = soundArgs;
  610. }
  611. }
  612. break;
  613. case "Redaction":
  614. {
  615. RedactionAnnotArgs redactionArgs =new RedactionAnnotArgs();
  616. redactionArgs.LineColor = Colors.Red;
  617. redactionArgs.BgColor = Colors.Black;
  618. redactionArgs.FontColor = Colors.Red;
  619. redactionArgs.Align = TextAlignment.Right;
  620. redactionArgs.FontSize = 14;
  621. redactionArgs.Content = "hello world";
  622. annotAttribsList[AnnotAttrib.Color] = redactionArgs.LineColor;
  623. annotAttribsList[AnnotAttrib.FontColor] = redactionArgs.LineColor;
  624. annotAttribsList[AnnotAttrib.FillColor] = redactionArgs.BgColor;
  625. annotAttribsList[AnnotAttrib.TextAlign] = redactionArgs.Align;
  626. annotAttribsList[AnnotAttrib.FontSize] = redactionArgs.FontSize;
  627. annotAttribsList[AnnotAttrib.Locked] = false;
  628. annotAttribsList[AnnotAttrib.NoteText] = redactionArgs.Content;
  629. annotArgs = redactionArgs;
  630. }
  631. break;
  632. default:
  633. break;
  634. }
  635. TextEditTool.IsChecked = false;
  636. TextSelectTool.IsChecked = false;
  637. FormEditTool.IsChecked = false;
  638. PanTool.IsChecked = false;
  639. if (clickBtn.IsChecked==true)
  640. {
  641. if (annotArgs != null)
  642. {
  643. view.SetMouseMode(MouseModes.AnnotCreate);
  644. view.SetToolParam(annotArgs);
  645. }
  646. SelectTool.IsChecked = false;
  647. SnapTool.IsChecked = false;
  648. ClearToggleButtonState(AnnotTools);
  649. ClearToggleButtonState(FormTools);
  650. if (clickBtn.Tag!=null && clickBtn.Tag.ToString() == "Stamp")
  651. {
  652. clickBtn.IsChecked = false;
  653. }
  654. else
  655. {
  656. clickBtn.IsChecked = true;
  657. }
  658. if (view.AnnotSetControl != null)
  659. {
  660. view.AnnotSetControl.lastAnnotEditEvent = null;
  661. }
  662. annotArgs.Author = "ComPDFKit";
  663. annotAttribsList[AnnotAttrib.Author] = annotArgs.Author;
  664. view.CurrentView_AnnotActiveHandler(this, AnnotAttribEvent.GetAnnotAttribEvent(annotArgs, annotAttribsList));
  665. }
  666. else
  667. {
  668. view.SetMouseMode(MouseModes.PanTool);
  669. PanTool.IsChecked = true;
  670. if (view.AnnotSetControl != null)
  671. {
  672. view.AnnotSetControl.lastAnnotEditEvent = null;
  673. }
  674. view.CurrentView_AnnotActiveHandler(this, new AnnotAttribEvent());
  675. }
  676. }
  677. }
  678. private void Undo_Click(object sender, RoutedEventArgs e)
  679. {
  680. PDFViewerControl view = GetCurrentViewer();
  681. if (view != null)
  682. {
  683. view.CurrentUndoManager.Undo();
  684. }
  685. }
  686. private void Redo_Click(object sender, RoutedEventArgs e)
  687. {
  688. PDFViewerControl view = GetCurrentViewer();
  689. if (view != null)
  690. {
  691. view.CurrentUndoManager.Redo();
  692. }
  693. }
  694. private void ClearToggleButtonState(DependencyObject parentControl)
  695. {
  696. int childCount = VisualTreeHelper.GetChildrenCount(parentControl);
  697. for (int i = 0; i < childCount; i++)
  698. {
  699. var childElement = VisualTreeHelper.GetChild(parentControl, i);
  700. if (childElement is ToggleButton)
  701. {
  702. if((childElement as ToggleButton).Name== "FormHighlightBtn")
  703. {
  704. continue;
  705. }
  706. (childElement as ToggleButton).IsChecked = false;
  707. }
  708. }
  709. if(parentControl == AnnotTools)
  710. {
  711. RedactionBtn.IsChecked = false;
  712. }
  713. }
  714. private void ShowLink_Click(object sender, RoutedEventArgs e)
  715. {
  716. var button = sender as ToggleButton;
  717. PDFViewerControl view = GetCurrentViewer();
  718. if (view != null)
  719. {
  720. view.currentViewer.SetShowLink(button.IsChecked.Value);
  721. }
  722. }
  723. private void PageOffset_ClickEvent(object sender, RoutedEventArgs e)
  724. {
  725. PDFViewerControl view = GetCurrentViewer();
  726. view.currentViewer.SetPageSpacing((uint)(PageOffset.IsChecked==true? 8:0));
  727. }
  728. private void FormTools_Click(object sender, RoutedEventArgs e)
  729. {
  730. PDFViewerControl view = GetCurrentViewer();
  731. ToggleButton clickBtn = sender as ToggleButton;
  732. ClearToggleButtonState(AnnotTools);
  733. view.AnnotSetControl.ProcessAnnotEvents(new AnnotAttribEvent());
  734. foreach (ToggleButton child in FormTools.Children)
  735. {
  736. if(child.Name== "FormHighlightBtn")
  737. {
  738. continue;
  739. }
  740. if(clickBtn != child)
  741. {
  742. child.IsChecked = false;
  743. }
  744. }
  745. ClearMouseToolState();
  746. if (view != null && clickBtn!=null)
  747. {
  748. if (clickBtn.IsChecked == true)
  749. {
  750. AnnotHandlerEventArgs annotArgs = null;
  751. switch(clickBtn.Tag.ToString())
  752. {
  753. case "TextBox":
  754. {
  755. WidgetTextBoxArgs textArgs=new WidgetTextBoxArgs();
  756. textArgs.BgColor = Colors.LightGray;
  757. annotArgs = textArgs;
  758. }
  759. break;
  760. case "RadioBtn":
  761. {
  762. WidgetRadioButtonArgs radioArgs = new WidgetRadioButtonArgs();
  763. radioArgs.CheckStyle = C_CHECK_STYLE.CK_CIRCLE;
  764. radioArgs.BorderStyle = C_BORDER_STYLE.BS_SOLID;
  765. radioArgs.LineColor = Colors.Black;
  766. radioArgs.BgColor = Colors.LightGreen;
  767. radioArgs.LineWidth = 2;
  768. radioArgs.FieldName = GetRadioFieldName();
  769. annotArgs = radioArgs;
  770. }
  771. break;
  772. case "CheckBox":
  773. {
  774. WidgetCheckBoxArgs checkBoxArgs = new WidgetCheckBoxArgs();
  775. checkBoxArgs.CheckStyle = C_CHECK_STYLE.CK_CHECK;
  776. checkBoxArgs.BorderStyle = C_BORDER_STYLE.BS_SOLID;
  777. checkBoxArgs.LineColor = Colors.Black;
  778. checkBoxArgs.BgColor = Colors.LightGreen;
  779. checkBoxArgs.LineWidth = 1;
  780. annotArgs = checkBoxArgs;
  781. }
  782. break;
  783. case "DropDown":
  784. {
  785. WidgetComboBoxArgs dropDownArgs=new WidgetComboBoxArgs();
  786. dropDownArgs.BgColor = Colors.Green;
  787. dropDownArgs.LineColor = Colors.Red;
  788. dropDownArgs.LineWidth = 1;
  789. annotArgs = dropDownArgs;
  790. }
  791. break;
  792. case "List":
  793. {
  794. WidgetListBoxArgs listArgs=new WidgetListBoxArgs();
  795. listArgs.LineColor = Colors.Red;
  796. listArgs.LineWidth = 1;
  797. annotArgs = listArgs;
  798. }
  799. break;
  800. case "PushBtn":
  801. {
  802. WidgetPushButtonArgs pushButtonArgs=new WidgetPushButtonArgs();
  803. pushButtonArgs.Text = "Push Button";
  804. pushButtonArgs.BgColor = Colors.LightGray;
  805. annotArgs = pushButtonArgs;
  806. }
  807. break;
  808. case "Sign":
  809. {
  810. WidgetSignArgs signArgs=new WidgetSignArgs();
  811. signArgs.BgColor= Colors.LightBlue;
  812. signArgs.LineWidth= 1;
  813. signArgs.LineColor= Colors.Red;
  814. annotArgs = signArgs;
  815. }
  816. break;
  817. default:
  818. break;
  819. }
  820. if (annotArgs != null)
  821. {
  822. view.SetMouseMode(MouseModes.FormEditTool);
  823. view.SetToolParam(annotArgs);
  824. }
  825. }
  826. else
  827. {
  828. view.SetMouseMode(MouseModes.PanTool);
  829. PanTool.IsChecked = true;
  830. if (view.AnnotSetControl != null)
  831. {
  832. view.AnnotSetControl.lastAnnotEditEvent = null;
  833. }
  834. view?.CurrentView_AnnotActiveHandler(this, new AnnotAttribEvent());
  835. }
  836. }
  837. }
  838. private void FormHighlightBtn_Click(object sender, RoutedEventArgs e)
  839. {
  840. PDFViewerControl view = GetCurrentViewer();
  841. if(view != null)
  842. {
  843. view.currentViewer.SetFormFieldHighlight(FormHighlightBtn.IsChecked==true?true:false);
  844. }
  845. }
  846. private void CropButton_Click(object sender, RoutedEventArgs e)
  847. {
  848. PDFViewerControl view = GetCurrentViewer();
  849. ToggleButton sourceBtn = sender as ToggleButton;
  850. if (sourceBtn != null)
  851. {
  852. sourceBtn.IsChecked = false;
  853. }
  854. if (view != null && view.currentViewer != null)
  855. {
  856. CropWindow cropWindow = new CropWindow();
  857. cropWindow.Owner = this;
  858. cropWindow.CurrentViewer = view.currentViewer;
  859. cropWindow.SetDoc(view.currentViewer.Document);
  860. cropWindow.Show();
  861. }
  862. }
  863. private void AutoCrop_Click(object sender, RoutedEventArgs e)
  864. {
  865. PDFViewerControl view = GetCurrentViewer();
  866. ToggleButton sourceBtn = sender as ToggleButton;
  867. if (sourceBtn != null)
  868. {
  869. view.LoadingAnimation.Visibility = Visibility.Visible;
  870. bool autoCrop = sourceBtn.IsChecked == true;
  871. Task.Factory.StartNew(() =>
  872. {
  873. view.currentViewer?.SetCropMode(autoCrop);
  874. Dispatcher.Invoke(() => {
  875. view.LoadingAnimation.Visibility = Visibility.Collapsed;
  876. });
  877. });
  878. }
  879. }
  880. private void OtherTools_Click(object sender, RoutedEventArgs e)
  881. {
  882. ClearMouseToolState();
  883. ClearToggleButtonState(AnnotTools);
  884. ClearToggleButtonState(FormTools);
  885. OtherTools.IsChecked = true;
  886. PDFViewerControl view = GetCurrentViewer();
  887. if (view != null)
  888. {
  889. view.SetMouseMode(MouseModes.PanTool);
  890. HideToolsPanel();
  891. OtherToolsPanel.Visibility = Visibility.Visible;
  892. }
  893. }
  894. private string GetRadioFieldName()
  895. {
  896. PDFViewerControl view = GetCurrentViewer();
  897. if (view != null && view.currentViewer!=null && view.currentViewer.Document!=null)
  898. {
  899. CPDFDocument cPDFDocument = view.currentViewer.Document;
  900. int radioIndex = 0;
  901. for (int i = 0; i < cPDFDocument.PageCount; i++)
  902. {
  903. CPDFPage cPage = cPDFDocument.PageAtIndex(i, false);
  904. List<CPDFAnnotation> annotList = cPage.GetAnnotations();
  905. if (annotList != null && annotList.Count > 0)
  906. {
  907. List<CPDFWidget> formList = annotList.AsEnumerable().
  908. Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  909. .Cast<CPDFWidget>()
  910. .ToList();
  911. radioIndex += formList.Where(x => x.WidgeType == C_WIDGET_TYPE.WIDGET_RADIOBUTTON).Count();
  912. }
  913. }
  914. return "RadioGroup" + radioIndex.ToString();
  915. }
  916. return string.Empty;
  917. }
  918. private void Encryption_Click(object sender, RoutedEventArgs e)
  919. {
  920. EncryptionPopup.IsOpen = true;
  921. }
  922. private void SetPassword_Click(object sender, RoutedEventArgs e)
  923. {
  924. EncryptionPopup.IsOpen = false;
  925. PDFViewerControl view = GetCurrentViewer();
  926. if (view != null && view.currentViewer != null && view.currentViewer.Document != null)
  927. {
  928. EncryptionWindow windoow = new EncryptionWindow(view.currentViewer.Document);
  929. windoow.Owner = this;
  930. windoow.Show();
  931. }
  932. }
  933. private void RemovePassword_Click(object sender, RoutedEventArgs e)
  934. {
  935. CPDFDocument document = null;
  936. PDFViewerControl view = GetCurrentViewer();
  937. if (view != null && view.currentViewer != null && view.currentViewer.Document != null)
  938. document = view.currentViewer.Document;
  939. string SavePath;
  940. //创建一个保存文件的对话框
  941. SaveFileDialog dialog = new SaveFileDialog()
  942. {
  943. Filter = "PDF Files| *.pdf",
  944. InitialDirectory = @"C:\",
  945. FileName = document.FileName + "_Decrypted"
  946. };
  947. //调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  948. if (dialog.ShowDialog().GetValueOrDefault())
  949. {
  950. SavePath = dialog.FileName;
  951. }
  952. else
  953. {
  954. return;
  955. }
  956. if (!document.Descrypt(SavePath))
  957. MessageBox.Show("Save file error!");
  958. }
  959. }
  960. }