MainWindow.xaml.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Data;
  3. using Compdfkit_Tools.Helper;
  4. using Compdfkit_Tools.PDFControl;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.AnnotEvent;
  7. using ComPDFKitViewer.PdfViewer;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Drawing;
  14. using System.IO;
  15. using System.Runtime.CompilerServices;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Controls.Primitives;
  19. using System.Windows.Input;
  20. using System.Windows.Media.Imaging;
  21. namespace Annotations
  22. {
  23. public partial class MainWindow : Window, INotifyPropertyChanged
  24. {
  25. #region Property
  26. private PDFViewControl passwordViewer;
  27. private PDFViewControl pdfViewControl;
  28. private CPDFAnnotationControl pdfAnnotationControl = null;
  29. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  30. public bool CanSave
  31. {
  32. get
  33. {
  34. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  35. {
  36. return pdfViewControl.PDFView.UndoManager.CanSave;
  37. }
  38. return false;
  39. }
  40. }
  41. public bool CanUndo
  42. {
  43. get
  44. {
  45. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  46. {
  47. return pdfViewControl.PDFView.UndoManager.CanUndo;
  48. }
  49. return false;
  50. }
  51. }
  52. public bool CanRedo
  53. {
  54. get
  55. {
  56. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  57. {
  58. return pdfViewControl.PDFView.UndoManager.CanRedo;
  59. }
  60. return false;
  61. }
  62. }
  63. public event PropertyChangedEventHandler PropertyChanged;
  64. public ICommand CloseTabCommand;
  65. #endregion
  66. public MainWindow()
  67. {
  68. InitializeComponent();
  69. DataContext = this;
  70. }
  71. #region Load document
  72. private void LoadDefaultDocument()
  73. {
  74. string defaultFilePath = "PDF32000_2008.pdf";
  75. pdfViewControl = new PDFViewControl();
  76. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  77. LoadDocument();
  78. }
  79. private void LoadDocument()
  80. {
  81. if (pdfViewControl.PDFView.Document == null)
  82. {
  83. return;
  84. }
  85. pdfViewControl.PDFView?.Load();
  86. pdfViewControl.PDFView?.SetShowLink(true);
  87. PDFGrid.Child = pdfViewControl;
  88. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  89. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  90. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  91. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  92. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  93. pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  94. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  95. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  96. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  97. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  98. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  99. PasswordUI.Closed -= PasswordUI_Closed;
  100. PasswordUI.Canceled -= PasswordUI_Canceled;
  101. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  102. PasswordUI.Closed += PasswordUI_Closed;
  103. PasswordUI.Canceled += PasswordUI_Canceled;
  104. PasswordUI.Confirmed += PasswordUI_Confirmed;
  105. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  106. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  107. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  108. ViewSettingBtn.IsChecked = false;
  109. PropertyContainer.Child = null;
  110. PropertyContainer.Visibility = Visibility.Collapsed;
  111. InitialPDFViewControl(pdfViewControl);
  112. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  113. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  114. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  115. }
  116. #endregion
  117. #region Load Unload custom control
  118. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  119. {
  120. pdfAnnotationControl = new CPDFAnnotationControl();
  121. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  122. LoadDefaultDocument();
  123. }
  124. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  125. {
  126. CPDFAnnotationType[] annotationProperties = { CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout, CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText, CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square, CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Image, CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio };
  127. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  128. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  129. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  130. }
  131. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  132. {
  133. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  134. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  135. }
  136. #endregion
  137. #region Annotation
  138. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  139. {
  140. pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  141. pdfAnnotationControl.AnnotationCancel();
  142. AnnotationBarControl.ClearAllToolState();
  143. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  144. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  145. }
  146. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  147. {
  148. AnnotationBarControl.ClearAllToolState();
  149. }
  150. #endregion
  151. #region Load Document
  152. #endregion
  153. #region Password
  154. private void PasswordUI_Confirmed(object sender, string e)
  155. {
  156. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  157. {
  158. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  159. if (passwordViewer.PDFView.Document.IsLocked == false)
  160. {
  161. PasswordUI.SetShowError("", Visibility.Collapsed);
  162. PasswordUI.ClearPassword();
  163. PasswordUI.Visibility = Visibility.Collapsed;
  164. PopupBorder.Visibility = Visibility.Collapsed;
  165. pdfViewControl.PDFView.Document.Release();
  166. pdfViewControl = passwordViewer;
  167. LoadDocument();
  168. }
  169. else
  170. {
  171. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  172. }
  173. }
  174. }
  175. private void PasswordUI_Canceled(object sender, EventArgs e)
  176. {
  177. PopupBorder.Visibility = Visibility.Collapsed;
  178. PasswordUI.Visibility = Visibility.Collapsed;
  179. }
  180. private void PasswordUI_Closed(object sender, EventArgs e)
  181. {
  182. PopupBorder.Visibility = Visibility.Collapsed;
  183. PasswordUI.Visibility = Visibility.Collapsed;
  184. }
  185. #endregion
  186. #region Expand and collapse Panel
  187. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  188. {
  189. PropertyContainer.Width = 260;
  190. PropertyContainer.Child = propertytPanel;
  191. PropertyContainer.Visibility = visible;
  192. if (visible == Visibility.Collapsed || visible == Visibility.Hidden)
  193. {
  194. RightPanelButton.IsChecked = false;
  195. }
  196. }
  197. private void ExpandLeftPanel(bool isExpand)
  198. {
  199. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  200. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  201. if (isExpand)
  202. {
  203. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  204. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  205. }
  206. else
  207. {
  208. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  209. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  210. }
  211. }
  212. #endregion
  213. #region Context menu
  214. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  215. {
  216. switch (e.CommandType)
  217. {
  218. case CommandType.Context:
  219. e.Handle = true;
  220. if (e.CommandTarget == TargetType.Annot)
  221. {
  222. e.Handle = true;
  223. e.PopupMenu = new ContextMenu();
  224. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  225. {
  226. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  227. MenuItem propertyMenu = new MenuItem();
  228. propertyMenu = new MenuItem();
  229. propertyMenu.Header = "Edit";
  230. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  231. propertyMenu.CommandParameter = e;
  232. e.PopupMenu.Items.Add(propertyMenu);
  233. }
  234. else if (e.PressOnAnnot)
  235. {
  236. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  237. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  238. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  239. }
  240. else if (e.PressOnMedia || e.PressOnSound)
  241. {
  242. e.Handle = true;
  243. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  244. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  245. }
  246. else if (e.PressOnSelectedText)
  247. {
  248. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  249. MenuItem highLightMenu = new MenuItem();
  250. highLightMenu.Header = "HighLight";
  251. highLightMenu.Click += (o, p) =>
  252. {
  253. TextHighlightAnnotArgs highLightArgs = new TextHighlightAnnotArgs();
  254. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  255. if (pdfAnnotationControl != null)
  256. {
  257. highLightArgs.Color = System.Windows.Media.Colors.Red;
  258. highLightArgs.Transparency = 1;
  259. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  260. pdfViewControl.PDFView.SetToolParam(highLightArgs);
  261. pdfViewControl.PDFView.SetMouseMode(oldMode);
  262. }
  263. };
  264. e.PopupMenu.Items.Add(highLightMenu);
  265. MenuItem underlineMenu = new MenuItem();
  266. underlineMenu.Header = "UnderLine";
  267. underlineMenu.Click += (o, p) =>
  268. {
  269. TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
  270. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  271. if (pdfAnnotationControl != null)
  272. {
  273. underlineArgs.Color = System.Windows.Media.Colors.Red;
  274. underlineArgs.Transparency = 1;
  275. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  276. pdfViewControl.PDFView.SetToolParam(underlineArgs);
  277. pdfViewControl.PDFView.SetMouseMode(oldMode);
  278. }
  279. };
  280. e.PopupMenu.Items.Add(underlineMenu);
  281. MenuItem strikeOutMenu = new MenuItem();
  282. strikeOutMenu.Header = "StrikeOut";
  283. strikeOutMenu.Click += (o, p) =>
  284. {
  285. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  286. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  287. if (pdfAnnotationControl != null)
  288. {
  289. strikeoutAnnotArgs.Color = System.Windows.Media.Colors.Red;
  290. strikeoutAnnotArgs.Transparency = 1;
  291. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  292. pdfViewControl.PDFView.SetToolParam(strikeoutAnnotArgs);
  293. pdfViewControl.PDFView.SetMouseMode(oldMode);
  294. }
  295. };
  296. e.PopupMenu.Items.Add(strikeOutMenu);
  297. MenuItem SquiggleMenu = new MenuItem();
  298. SquiggleMenu.Header = "Squiggle";
  299. SquiggleMenu.Click += (o, p) =>
  300. {
  301. TextSquigglyAnnotArgs squigglyAnnotArgs = new TextSquigglyAnnotArgs();
  302. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  303. if (pdfAnnotationControl != null)
  304. {
  305. squigglyAnnotArgs.Color = System.Windows.Media.Colors.Red;
  306. squigglyAnnotArgs.Transparency = 1;
  307. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  308. pdfViewControl.PDFView.SetToolParam(squigglyAnnotArgs);
  309. pdfViewControl.PDFView.SetMouseMode(oldMode);
  310. }
  311. };
  312. e.PopupMenu.Items.Add(SquiggleMenu);
  313. }
  314. else
  315. {
  316. e.Handle = true;
  317. e.PopupMenu = new ContextMenu();
  318. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  319. e.PopupMenu.Items.Add(new Separator());
  320. MenuItem fitWidthMenu = new MenuItem();
  321. fitWidthMenu.Header = "Automatically Resize";
  322. fitWidthMenu.Click += (o, p) =>
  323. {
  324. if (pdfViewControl != null)
  325. {
  326. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  327. }
  328. };
  329. e.PopupMenu.Items.Add(fitWidthMenu);
  330. MenuItem fitSizeMenu = new MenuItem();
  331. fitSizeMenu.Header = "Actual Size";
  332. fitSizeMenu.Click += (o, p) =>
  333. {
  334. if (pdfViewControl != null)
  335. {
  336. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  337. }
  338. };
  339. e.PopupMenu.Items.Add(fitSizeMenu);
  340. MenuItem zoomInMenu = new MenuItem();
  341. zoomInMenu.Header = "Zoom In";
  342. zoomInMenu.Click += (o, p) =>
  343. {
  344. if (pdfViewControl != null)
  345. {
  346. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  347. pdfViewControl.PDFView?.Zoom(newZoom);
  348. }
  349. };
  350. e.PopupMenu.Items.Add(zoomInMenu);
  351. MenuItem zoomOutMenu = new MenuItem();
  352. zoomOutMenu.Header = "Zoom Out";
  353. zoomOutMenu.Click += (o, p) =>
  354. {
  355. if (pdfViewControl != null)
  356. {
  357. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  358. pdfViewControl.PDFView?.Zoom(newZoom);
  359. }
  360. };
  361. e.PopupMenu.Items.Add(zoomOutMenu);
  362. e.PopupMenu.Items.Add(new Separator());
  363. MenuItem singleView = new MenuItem();
  364. singleView.Header = "Single Page";
  365. singleView.Click += (o, p) =>
  366. {
  367. if (pdfViewControl != null)
  368. {
  369. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  370. }
  371. };
  372. e.PopupMenu.Items.Add(singleView);
  373. MenuItem singleContinuousView = new MenuItem();
  374. singleContinuousView.Header = "Single Page Continuous";
  375. singleContinuousView.Click += (o, p) =>
  376. {
  377. if (pdfViewControl != null)
  378. {
  379. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  380. }
  381. };
  382. e.PopupMenu.Items.Add(singleContinuousView);
  383. MenuItem doubleView = new MenuItem();
  384. doubleView.Header = "Two Pages";
  385. doubleView.Click += (o, p) =>
  386. {
  387. if (pdfViewControl != null)
  388. {
  389. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  390. }
  391. };
  392. e.PopupMenu.Items.Add(doubleView);
  393. MenuItem doubleContinuousView = new MenuItem();
  394. doubleContinuousView.Header = "Two Pages Continuous";
  395. doubleContinuousView.Click += (o, p) =>
  396. {
  397. if (pdfViewControl != null)
  398. {
  399. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  400. }
  401. };
  402. e.PopupMenu.Items.Add(doubleContinuousView);
  403. }
  404. }
  405. else if (e.CommandTarget == TargetType.ImageSelection)
  406. {
  407. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  408. {
  409. e.Handle = true;
  410. e.PopupMenu = new ContextMenu();
  411. MenuItem imageCopyMenu = new MenuItem();
  412. imageCopyMenu = new MenuItem();
  413. imageCopyMenu.Header = "Copy Images";
  414. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  415. imageCopyMenu.CommandParameter = e;
  416. e.PopupMenu.Items.Add(imageCopyMenu);
  417. MenuItem imageExtraMenu = new MenuItem();
  418. imageExtraMenu = new MenuItem();
  419. imageExtraMenu.Header = "Extract Images";
  420. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  421. imageExtraMenu.CommandParameter = e;
  422. e.PopupMenu.Items.Add(imageExtraMenu);
  423. }
  424. }
  425. break;
  426. case CommandType.Copy:
  427. e.DoCommand();
  428. break;
  429. case CommandType.Cut:
  430. case CommandType.Paste:
  431. case CommandType.Delete:
  432. e.DoCommand();
  433. break;
  434. default:
  435. break;
  436. }
  437. }
  438. private void CopyImage_Click(object sender, RoutedEventArgs e)
  439. {
  440. try
  441. {
  442. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  443. if (imageDict != null && imageDict.Count > 0)
  444. {
  445. foreach (int pageIndex in imageDict.Keys)
  446. {
  447. List<Bitmap> imageList = imageDict[pageIndex];
  448. foreach (Bitmap image in imageList)
  449. {
  450. MemoryStream ms = new MemoryStream();
  451. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  452. BitmapImage imageData = new BitmapImage();
  453. imageData.BeginInit();
  454. imageData.StreamSource = ms;
  455. imageData.CacheOption = BitmapCacheOption.OnLoad;
  456. imageData.EndInit();
  457. imageData.Freeze();
  458. Clipboard.SetImage(imageData);
  459. break;
  460. }
  461. }
  462. }
  463. }
  464. catch (Exception ex)
  465. {
  466. }
  467. }
  468. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  469. {
  470. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  471. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  472. {
  473. string choosePath = folderDialog.SelectedPath;
  474. string openPath = choosePath;
  475. try
  476. {
  477. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  478. if (imageDict != null && imageDict.Count > 0)
  479. {
  480. foreach (int pageIndex in imageDict.Keys)
  481. {
  482. List<Bitmap> imageList = imageDict[pageIndex];
  483. foreach (Bitmap image in imageList)
  484. {
  485. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  486. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  487. openPath = savePath;
  488. }
  489. }
  490. }
  491. Process.Start("explorer", "/select,\"" + openPath + "\"");
  492. }
  493. catch (Exception ex)
  494. {
  495. }
  496. }
  497. }
  498. #endregion
  499. #region UI
  500. private double CheckZoomLevel(double zoom, bool IsGrowth)
  501. {
  502. double standardZoom = 100;
  503. if (zoom <= 0.01)
  504. {
  505. return 0.01;
  506. }
  507. if (zoom >= 10)
  508. {
  509. return 10;
  510. }
  511. zoom *= 100;
  512. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  513. {
  514. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  515. {
  516. standardZoom = zoomLevelList[i + 1];
  517. break;
  518. }
  519. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  520. {
  521. standardZoom = zoomLevelList[i];
  522. break;
  523. }
  524. }
  525. return standardZoom / 100;
  526. }
  527. private void ControlLeftPanel()
  528. {
  529. if (LeftToolPanelButton != null)
  530. {
  531. bool isExpand = LeftToolPanelButton.IsChecked == true;
  532. ExpandLeftPanel(isExpand);
  533. }
  534. }
  535. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  536. {
  537. ControlLeftPanel();
  538. }
  539. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  540. {
  541. ExpandLeftPanel(true);
  542. BotaSideTool.SelectBotaTool(BOTATools.Search);
  543. }
  544. private void ShowViewSettings()
  545. {
  546. if (ViewSettingBtn != null)
  547. {
  548. if (ViewSettingBtn.IsChecked == true)
  549. {
  550. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  551. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  552. PropertyContainer.Child = displayPanel;
  553. PropertyContainer.Visibility = Visibility.Visible;
  554. if ((bool)RightPanelButton.IsChecked)
  555. {
  556. RightPanelButton.IsChecked = false;
  557. }
  558. }
  559. else
  560. {
  561. PropertyContainer.Child = null;
  562. PropertyContainer.Visibility = Visibility.Collapsed;
  563. }
  564. }
  565. }
  566. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  567. {
  568. ShowViewSettings();
  569. }
  570. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  571. {
  572. PasswordUI.Visibility = Visibility.Collapsed;
  573. CPDFInfoControl.Visibility = Visibility.Visible;
  574. CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  575. CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  576. CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  577. PopupBorder.Visibility = Visibility.Visible;
  578. }
  579. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  580. {
  581. PopupBorder.Visibility = Visibility.Collapsed;
  582. }
  583. private void OpenFile()
  584. {
  585. string filePath = CommonHelper.GetFilePathOrEmpty();
  586. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  587. {
  588. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  589. {
  590. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  591. if (oldFilePath.ToLower() == filePath.ToLower())
  592. {
  593. return;
  594. }
  595. }
  596. passwordViewer = new PDFViewControl();
  597. passwordViewer.PDFView.InitDocument(filePath);
  598. if (passwordViewer.PDFView.Document == null)
  599. {
  600. MessageBox.Show("Open File Failed");
  601. return;
  602. }
  603. if (passwordViewer.PDFView.Document.IsLocked)
  604. {
  605. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  606. PasswordUI.ClearPassword();
  607. PopupBorder.Visibility = Visibility.Visible;
  608. PasswordUI.Visibility = Visibility.Visible;
  609. }
  610. else
  611. {
  612. pdfViewControl.PDFView.Document.Release();
  613. pdfViewControl = passwordViewer;
  614. LoadDocument();
  615. }
  616. }
  617. }
  618. private void OpenFile_Click(object sender, RoutedEventArgs e)
  619. {
  620. OpenFile();
  621. }
  622. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  623. {
  624. SaveFile();
  625. pdfViewControl.PDFView.UndoManager.CanSave = false;
  626. }
  627. private void ControlRightPanel()
  628. {
  629. if (RightPanelButton != null)
  630. {
  631. if (RightPanelButton.IsChecked == true)
  632. {
  633. if (pdfAnnotationControl != null)
  634. {
  635. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  636. if ((bool)ViewSettingBtn.IsChecked)
  637. {
  638. ViewSettingBtn.IsChecked = false;
  639. }
  640. }
  641. }
  642. else
  643. {
  644. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  645. }
  646. }
  647. }
  648. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  649. {
  650. ControlRightPanel();
  651. }
  652. private void EditLink_Click(object sender, RoutedEventArgs e)
  653. {
  654. PropertyContainer.Visibility = Visibility.Visible;
  655. }
  656. private void UndoButton_Click(object sender, RoutedEventArgs e)
  657. {
  658. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  659. {
  660. pdfViewControl.PDFView.UndoManager?.Undo();
  661. }
  662. }
  663. private void RedoButton_Click(object sender, RoutedEventArgs e)
  664. {
  665. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  666. {
  667. pdfViewControl.PDFView.UndoManager?.Redo();
  668. }
  669. }
  670. #endregion
  671. #region Property changed
  672. protected void OnPropertyChanged([CallerMemberName] string name = null)
  673. {
  674. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  675. }
  676. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  677. {
  678. OnPropertyChanged(e.PropertyName);
  679. }
  680. #endregion
  681. #region Event handle
  682. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  683. {
  684. pdfAnnotationControl.AnnotationCancel();
  685. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  686. RightPanelButton.IsChecked = false;
  687. ViewSettingBtn.IsChecked = false;
  688. }
  689. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  690. {
  691. pdfAnnotationControl.LoadAnnotationPanel(e);
  692. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  693. {
  694. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  695. RightPanelButton.IsChecked = true;
  696. }
  697. }
  698. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  699. {
  700. PropertyContainer.Child = pdfAnnotationControl;
  701. pdfAnnotationControl.SetAnnotEventData(e);
  702. }
  703. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  704. {
  705. BotaSideTool.LoadAnnotationList();
  706. }
  707. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  708. {
  709. if (e.Key == "Zoom")
  710. {
  711. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  712. }
  713. }
  714. #endregion
  715. #region Save file
  716. /// <summary>
  717. /// Save the file to another PDF file.
  718. /// </summary>
  719. public void SaveAsFile()
  720. {
  721. {
  722. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  723. {
  724. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  725. SaveFileDialog saveDialog = new SaveFileDialog();
  726. saveDialog.Filter = "(*.pdf)|*.pdf";
  727. saveDialog.DefaultExt = ".pdf";
  728. saveDialog.OverwritePrompt = true;
  729. if (saveDialog.ShowDialog() == true)
  730. {
  731. pdfDoc.WriteToFilePath(saveDialog.FileName);
  732. }
  733. }
  734. }
  735. }
  736. /// <summary>
  737. /// Save the file in the current path.
  738. /// </summary>
  739. private void SaveFile()
  740. {
  741. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  742. {
  743. try
  744. {
  745. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  746. if (pdfDoc.WriteToLoadedPath())
  747. {
  748. return;
  749. }
  750. SaveFileDialog saveDialog = new SaveFileDialog();
  751. saveDialog.Filter = "(*.pdf)|*.pdf";
  752. saveDialog.DefaultExt = ".pdf";
  753. saveDialog.OverwritePrompt = true;
  754. if (saveDialog.ShowDialog() == true)
  755. {
  756. pdfDoc.WriteToFilePath(saveDialog.FileName);
  757. }
  758. }
  759. catch (Exception ex)
  760. {
  761. }
  762. }
  763. }
  764. #endregion
  765. #region Selected changed
  766. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  767. {
  768. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  769. if ((string)item.Content == "Viewer")
  770. {
  771. AnnotationBarControl.ClearAllToolState();
  772. ToolBarContainer.Visibility = Visibility.Collapsed;
  773. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  774. pdfAnnotationControl.AnnotationCancel();
  775. RightPanelButton.IsChecked = false;
  776. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  777. {
  778. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  779. }
  780. }
  781. else if ((string)item.Content == "Annotation")
  782. {
  783. ToolBarContainer.Visibility = Visibility.Visible;
  784. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  785. {
  786. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  787. }
  788. }
  789. }
  790. #endregion
  791. #region Close window
  792. protected override void OnClosing(CancelEventArgs e)
  793. {
  794. if (pdfViewControl.PDFView.UndoManager.CanSave)
  795. {
  796. MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
  797. if (result == MessageBoxResult.Yes)
  798. {
  799. SaveFile();
  800. }
  801. else if (result == MessageBoxResult.No)
  802. {
  803. }
  804. else
  805. {
  806. e.Cancel = true;
  807. }
  808. }
  809. }
  810. #endregion
  811. #region Shortcut
  812. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  813. {
  814. OpenFile();
  815. }
  816. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  817. {
  818. if (CanSave)
  819. {
  820. SaveFile();
  821. }
  822. }
  823. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  824. {
  825. SaveAsFile();
  826. }
  827. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  828. {
  829. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanUndo)
  830. {
  831. pdfViewControl.PDFView.UndoManager?.Undo();
  832. }
  833. }
  834. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  835. {
  836. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanRedo)
  837. {
  838. pdfViewControl.PDFView.UndoManager?.Redo();
  839. }
  840. }
  841. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  842. {
  843. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  844. ControlLeftPanel();
  845. }
  846. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  847. {
  848. RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
  849. ControlRightPanel();
  850. }
  851. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  852. {
  853. if (ModeComboBox.SelectedIndex == 1)
  854. {
  855. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  856. }
  857. }
  858. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  859. {
  860. if (ModeComboBox.SelectedIndex == 1)
  861. {
  862. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  863. }
  864. }
  865. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  866. {
  867. if (ModeComboBox.SelectedIndex == 1)
  868. {
  869. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  870. }
  871. }
  872. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  873. {
  874. if (ModeComboBox.SelectedIndex == 1)
  875. {
  876. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  877. }
  878. }
  879. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  880. {
  881. ExpandLeftPanel(true);
  882. LeftToolPanelButton.IsChecked = true;
  883. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  884. }
  885. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  886. {
  887. ExpandLeftPanel(true);
  888. LeftToolPanelButton.IsChecked = true;
  889. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  890. }
  891. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  892. {
  893. ExpandLeftPanel(true);
  894. LeftToolPanelButton.IsChecked = true;
  895. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  896. }
  897. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  898. {
  899. ExpandLeftPanel(true);
  900. LeftToolPanelButton.IsChecked = true;
  901. BotaSideTool.SelectBotaTool(BOTATools.Annotation);
  902. }
  903. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  904. {
  905. ExpandLeftPanel(true);
  906. LeftToolPanelButton.IsChecked = true;
  907. BotaSideTool.SelectBotaTool(BOTATools.Search);
  908. }
  909. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  910. {
  911. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  912. pdfViewControl.PDFView?.Zoom(newZoom);
  913. }
  914. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  915. {
  916. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  917. pdfViewControl.PDFView?.Zoom(newZoom);
  918. }
  919. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  920. {
  921. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  922. ShowViewSettings();
  923. }
  924. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  925. {
  926. if (PopupBorder.Visibility != Visibility.Visible)
  927. {
  928. PasswordUI.Visibility = Visibility.Collapsed;
  929. CPDFInfoControl.Visibility = Visibility.Visible;
  930. CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  931. CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  932. CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  933. PopupBorder.Visibility = Visibility.Visible;
  934. }
  935. else
  936. {
  937. CPDFInfoControl.Visibility = Visibility.Collapsed;
  938. PopupBorder.Visibility = Visibility.Collapsed;
  939. }
  940. }
  941. #endregion
  942. }
  943. }