MainWindow.xaml.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 = passwordViewer;
  166. LoadDocument();
  167. }
  168. else
  169. {
  170. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  171. }
  172. }
  173. }
  174. private void PasswordUI_Canceled(object sender, EventArgs e)
  175. {
  176. PopupBorder.Visibility = Visibility.Collapsed;
  177. PasswordUI.Visibility = Visibility.Collapsed;
  178. }
  179. private void PasswordUI_Closed(object sender, EventArgs e)
  180. {
  181. PopupBorder.Visibility = Visibility.Collapsed;
  182. PasswordUI.Visibility = Visibility.Collapsed;
  183. }
  184. #endregion
  185. #region Expand and collapse Panel
  186. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  187. {
  188. PropertyContainer.Width = 260;
  189. PropertyContainer.Child = propertytPanel;
  190. PropertyContainer.Visibility = visible;
  191. if (visible == Visibility.Collapsed || visible == Visibility.Hidden)
  192. {
  193. AnnotationBarBtn.IsChecked = false;
  194. }
  195. }
  196. private void ExpandLeftPanel(bool isExpand)
  197. {
  198. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  199. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  200. if (isExpand)
  201. {
  202. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  203. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  204. }
  205. else
  206. {
  207. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  208. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  209. }
  210. }
  211. #endregion
  212. #region Context menu
  213. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  214. {
  215. switch (e.CommandType)
  216. {
  217. case CommandType.Context:
  218. e.Handle = true;
  219. if (e.CommandTarget == TargetType.Annot)
  220. {
  221. e.Handle = true;
  222. e.PopupMenu = new ContextMenu();
  223. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  224. {
  225. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  226. MenuItem propertyMenu = new MenuItem();
  227. propertyMenu = new MenuItem();
  228. propertyMenu.Header = "Edit";
  229. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  230. propertyMenu.CommandParameter = e;
  231. e.PopupMenu.Items.Add(propertyMenu);
  232. }
  233. else if (e.PressOnAnnot)
  234. {
  235. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  236. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  237. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  238. }
  239. else if (e.PressOnMedia || e.PressOnSound)
  240. {
  241. e.Handle = true;
  242. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  243. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  244. }
  245. else if (e.PressOnSelectedText)
  246. {
  247. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  248. MenuItem highLightMenu = new MenuItem();
  249. highLightMenu.Header = "HighLight";
  250. highLightMenu.Click += (o, p) =>
  251. {
  252. TextHighlightAnnotArgs highLightArgs = new TextHighlightAnnotArgs();
  253. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  254. if (pdfAnnotationControl != null)
  255. {
  256. highLightArgs.Color = System.Windows.Media.Colors.Red;
  257. highLightArgs.Transparency = 1;
  258. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  259. pdfViewControl.PDFView.SetToolParam(highLightArgs);
  260. pdfViewControl.PDFView.SetMouseMode(oldMode);
  261. }
  262. };
  263. e.PopupMenu.Items.Add(highLightMenu);
  264. MenuItem underlineMenu = new MenuItem();
  265. underlineMenu.Header = "UnderLine";
  266. underlineMenu.Click += (o, p) =>
  267. {
  268. TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
  269. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  270. if (pdfAnnotationControl != null)
  271. {
  272. underlineArgs.Color = System.Windows.Media.Colors.Red;
  273. underlineArgs.Transparency = 1;
  274. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  275. pdfViewControl.PDFView.SetToolParam(underlineArgs);
  276. pdfViewControl.PDFView.SetMouseMode(oldMode);
  277. }
  278. };
  279. e.PopupMenu.Items.Add(underlineMenu);
  280. MenuItem strikeOutMenu = new MenuItem();
  281. strikeOutMenu.Header = "StrikeOut";
  282. strikeOutMenu.Click += (o, p) =>
  283. {
  284. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  285. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  286. if (pdfAnnotationControl != null)
  287. {
  288. strikeoutAnnotArgs.Color = System.Windows.Media.Colors.Red;
  289. strikeoutAnnotArgs.Transparency = 1;
  290. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  291. pdfViewControl.PDFView.SetToolParam(strikeoutAnnotArgs);
  292. pdfViewControl.PDFView.SetMouseMode(oldMode);
  293. }
  294. };
  295. e.PopupMenu.Items.Add(strikeOutMenu);
  296. MenuItem SquiggleMenu = new MenuItem();
  297. SquiggleMenu.Header = "Squiggle";
  298. SquiggleMenu.Click += (o, p) =>
  299. {
  300. TextSquigglyAnnotArgs squigglyAnnotArgs = new TextSquigglyAnnotArgs();
  301. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  302. if (pdfAnnotationControl != null)
  303. {
  304. squigglyAnnotArgs.Color = System.Windows.Media.Colors.Red;
  305. squigglyAnnotArgs.Transparency = 1;
  306. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  307. pdfViewControl.PDFView.SetToolParam(squigglyAnnotArgs);
  308. pdfViewControl.PDFView.SetMouseMode(oldMode);
  309. }
  310. };
  311. e.PopupMenu.Items.Add(SquiggleMenu);
  312. }
  313. else
  314. {
  315. e.Handle = true;
  316. e.PopupMenu = new ContextMenu();
  317. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  318. e.PopupMenu.Items.Add(new Separator());
  319. MenuItem fitWidthMenu = new MenuItem();
  320. fitWidthMenu.Header = "Automatically Resize";
  321. fitWidthMenu.Click += (o, p) =>
  322. {
  323. if (pdfViewControl != null)
  324. {
  325. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  326. }
  327. };
  328. e.PopupMenu.Items.Add(fitWidthMenu);
  329. MenuItem fitSizeMenu = new MenuItem();
  330. fitSizeMenu.Header = "Actual Size";
  331. fitSizeMenu.Click += (o, p) =>
  332. {
  333. if (pdfViewControl != null)
  334. {
  335. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  336. }
  337. };
  338. e.PopupMenu.Items.Add(fitSizeMenu);
  339. MenuItem zoomInMenu = new MenuItem();
  340. zoomInMenu.Header = "Zoom In";
  341. zoomInMenu.Click += (o, p) =>
  342. {
  343. if (pdfViewControl != null)
  344. {
  345. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  346. pdfViewControl.PDFView?.Zoom(newZoom);
  347. }
  348. };
  349. e.PopupMenu.Items.Add(zoomInMenu);
  350. MenuItem zoomOutMenu = new MenuItem();
  351. zoomOutMenu.Header = "Zoom Out";
  352. zoomOutMenu.Click += (o, p) =>
  353. {
  354. if (pdfViewControl != null)
  355. {
  356. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  357. pdfViewControl.PDFView?.Zoom(newZoom);
  358. }
  359. };
  360. e.PopupMenu.Items.Add(zoomOutMenu);
  361. e.PopupMenu.Items.Add(new Separator());
  362. MenuItem singleView = new MenuItem();
  363. singleView.Header = "Single Page";
  364. singleView.Click += (o, p) =>
  365. {
  366. if (pdfViewControl != null)
  367. {
  368. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  369. }
  370. };
  371. e.PopupMenu.Items.Add(singleView);
  372. MenuItem singleContinuousView = new MenuItem();
  373. singleContinuousView.Header = "Single Page Continuous";
  374. singleContinuousView.Click += (o, p) =>
  375. {
  376. if (pdfViewControl != null)
  377. {
  378. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  379. }
  380. };
  381. e.PopupMenu.Items.Add(singleContinuousView);
  382. MenuItem doubleView = new MenuItem();
  383. doubleView.Header = "Two Pages";
  384. doubleView.Click += (o, p) =>
  385. {
  386. if (pdfViewControl != null)
  387. {
  388. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  389. }
  390. };
  391. e.PopupMenu.Items.Add(doubleView);
  392. MenuItem doubleContinuousView = new MenuItem();
  393. doubleContinuousView.Header = "Two Pages Continuous";
  394. doubleContinuousView.Click += (o, p) =>
  395. {
  396. if (pdfViewControl != null)
  397. {
  398. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  399. }
  400. };
  401. e.PopupMenu.Items.Add(doubleContinuousView);
  402. }
  403. }
  404. else if (e.CommandTarget == TargetType.ImageSelection)
  405. {
  406. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  407. {
  408. e.Handle = true;
  409. e.PopupMenu = new ContextMenu();
  410. MenuItem imageCopyMenu = new MenuItem();
  411. imageCopyMenu = new MenuItem();
  412. imageCopyMenu.Header = "Copy Images";
  413. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  414. imageCopyMenu.CommandParameter = e;
  415. e.PopupMenu.Items.Add(imageCopyMenu);
  416. MenuItem imageExtraMenu = new MenuItem();
  417. imageExtraMenu = new MenuItem();
  418. imageExtraMenu.Header = "Extract Images";
  419. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  420. imageExtraMenu.CommandParameter = e;
  421. e.PopupMenu.Items.Add(imageExtraMenu);
  422. }
  423. }
  424. break;
  425. case CommandType.Copy:
  426. e.DoCommand();
  427. break;
  428. case CommandType.Cut:
  429. case CommandType.Paste:
  430. case CommandType.Delete:
  431. e.DoCommand();
  432. break;
  433. default:
  434. break;
  435. }
  436. }
  437. private void CopyImage_Click(object sender, RoutedEventArgs e)
  438. {
  439. try
  440. {
  441. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  442. if (imageDict != null && imageDict.Count > 0)
  443. {
  444. foreach (int pageIndex in imageDict.Keys)
  445. {
  446. List<Bitmap> imageList = imageDict[pageIndex];
  447. foreach (Bitmap image in imageList)
  448. {
  449. MemoryStream ms = new MemoryStream();
  450. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  451. BitmapImage imageData = new BitmapImage();
  452. imageData.BeginInit();
  453. imageData.StreamSource = ms;
  454. imageData.CacheOption = BitmapCacheOption.OnLoad;
  455. imageData.EndInit();
  456. imageData.Freeze();
  457. Clipboard.SetImage(imageData);
  458. break;
  459. }
  460. }
  461. }
  462. }
  463. catch (Exception ex)
  464. {
  465. }
  466. }
  467. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  468. {
  469. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  470. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  471. {
  472. string choosePath = folderDialog.SelectedPath;
  473. string openPath = choosePath;
  474. try
  475. {
  476. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  477. if (imageDict != null && imageDict.Count > 0)
  478. {
  479. foreach (int pageIndex in imageDict.Keys)
  480. {
  481. List<Bitmap> imageList = imageDict[pageIndex];
  482. foreach (Bitmap image in imageList)
  483. {
  484. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  485. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  486. openPath = savePath;
  487. }
  488. }
  489. }
  490. Process.Start("explorer", "/select,\"" + openPath + "\"");
  491. }
  492. catch (Exception ex)
  493. {
  494. }
  495. }
  496. }
  497. #endregion
  498. #region UI
  499. private double CheckZoomLevel(double zoom, bool IsGrowth)
  500. {
  501. double standardZoom = 100;
  502. if (zoom <= 0.01)
  503. {
  504. return 0.01;
  505. }
  506. if (zoom >= 10)
  507. {
  508. return 10;
  509. }
  510. zoom *= 100;
  511. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  512. {
  513. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  514. {
  515. standardZoom = zoomLevelList[i + 1];
  516. break;
  517. }
  518. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  519. {
  520. standardZoom = zoomLevelList[i];
  521. break;
  522. }
  523. }
  524. return standardZoom / 100;
  525. }
  526. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  527. {
  528. ToggleButton expandBtn = sender as ToggleButton;
  529. if (expandBtn != null)
  530. {
  531. bool isExpand = expandBtn.IsChecked == true;
  532. ExpandLeftPanel(isExpand);
  533. }
  534. }
  535. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  536. {
  537. ExpandLeftPanel(true);
  538. BotaSideTool.SelectBotaTool(BOTATools.Search);
  539. }
  540. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  541. {
  542. ToggleButton toggleButton = sender as ToggleButton;
  543. if (toggleButton != null)
  544. {
  545. if (toggleButton.IsChecked == true)
  546. {
  547. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  548. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  549. PropertyContainer.Child = displayPanel;
  550. PropertyContainer.Visibility = Visibility.Visible;
  551. if ((bool)AnnotationBarBtn.IsChecked)
  552. {
  553. AnnotationBarBtn.IsChecked = false;
  554. }
  555. }
  556. else
  557. {
  558. PropertyContainer.Child = null;
  559. PropertyContainer.Visibility = Visibility.Collapsed;
  560. }
  561. }
  562. }
  563. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  564. {
  565. PasswordUI.Visibility = Visibility.Collapsed;
  566. FileInfoUI.Visibility = Visibility.Visible;
  567. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  568. PopupBorder.Visibility = Visibility.Visible;
  569. }
  570. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  571. {
  572. PopupBorder.Visibility = Visibility.Collapsed;
  573. }
  574. private void OpenFile_Click(object sender, RoutedEventArgs e)
  575. {
  576. string filePath = CommonHelper.GetFilePathOrEmpty();
  577. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  578. {
  579. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  580. {
  581. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  582. if (oldFilePath.ToLower() == filePath.ToLower())
  583. {
  584. return;
  585. }
  586. }
  587. passwordViewer = new PDFViewControl();
  588. passwordViewer.PDFView.InitDocument(filePath);
  589. if (passwordViewer.PDFView.Document == null)
  590. {
  591. MessageBox.Show("Open File Failed");
  592. return;
  593. }
  594. if (passwordViewer.PDFView.Document.IsLocked)
  595. {
  596. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  597. PasswordUI.ClearPassword();
  598. PopupBorder.Visibility = Visibility.Visible;
  599. PasswordUI.Visibility = Visibility.Visible;
  600. }
  601. else
  602. {
  603. pdfViewControl = passwordViewer;
  604. LoadDocument();
  605. }
  606. }
  607. }
  608. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  609. {
  610. SaveFile();
  611. pdfViewControl.PDFView.UndoManager.CanSave = false;
  612. }
  613. private void AnnotationBarControl_Click(object sender, RoutedEventArgs e)
  614. {
  615. ToggleButton toggleButton = sender as ToggleButton;
  616. if (toggleButton != null)
  617. {
  618. if (toggleButton.IsChecked == true)
  619. {
  620. if (pdfAnnotationControl != null)
  621. {
  622. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  623. if ((bool)ViewSettingBtn.IsChecked)
  624. {
  625. ViewSettingBtn.IsChecked = false;
  626. }
  627. }
  628. }
  629. else
  630. {
  631. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  632. }
  633. }
  634. }
  635. private void EditLink_Click(object sender, RoutedEventArgs e)
  636. {
  637. PropertyContainer.Visibility = Visibility.Visible;
  638. }
  639. private void UndoButton_Click(object sender, RoutedEventArgs e)
  640. {
  641. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  642. {
  643. pdfViewControl.PDFView.UndoManager?.Undo();
  644. }
  645. }
  646. private void RedoButton_Click(object sender, RoutedEventArgs e)
  647. {
  648. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  649. {
  650. pdfViewControl.PDFView.UndoManager?.Redo();
  651. }
  652. }
  653. #endregion
  654. #region Property changed
  655. protected void OnPropertyChanged([CallerMemberName] string name = null)
  656. {
  657. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  658. }
  659. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  660. {
  661. OnPropertyChanged(e.PropertyName);
  662. }
  663. #endregion
  664. #region Event handle
  665. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  666. {
  667. pdfAnnotationControl.AnnotationCancel();
  668. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  669. AnnotationBarBtn.IsChecked = false;
  670. ViewSettingBtn.IsChecked = false;
  671. }
  672. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  673. {
  674. pdfAnnotationControl.LoadAnnotationPanel(e);
  675. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  676. {
  677. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  678. AnnotationBarBtn.IsChecked = true;
  679. }
  680. }
  681. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  682. {
  683. PropertyContainer.Child = pdfAnnotationControl;
  684. pdfAnnotationControl.SetAnnotEventData(e);
  685. }
  686. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  687. {
  688. BotaSideTool.LoadAnnotationList();
  689. }
  690. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  691. {
  692. if (e.Key == "Zoom")
  693. {
  694. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  695. }
  696. }
  697. #endregion
  698. #region Save file
  699. private void SaveFile()
  700. {
  701. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  702. {
  703. try
  704. {
  705. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  706. if (pdfDoc.WriteToLoadedPath())
  707. {
  708. return;
  709. }
  710. SaveFileDialog saveDialog = new SaveFileDialog();
  711. saveDialog.Filter = "(*.pdf)|*.pdf";
  712. saveDialog.DefaultExt = ".pdf";
  713. saveDialog.OverwritePrompt = true;
  714. if (saveDialog.ShowDialog() == true)
  715. {
  716. pdfDoc.WriteToFilePath(saveDialog.FileName);
  717. }
  718. }
  719. catch (Exception ex)
  720. {
  721. }
  722. }
  723. }
  724. #endregion
  725. #region Selected changed
  726. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  727. {
  728. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  729. if ((string)item.Content == "Viewer")
  730. {
  731. AnnotationBarControl.ClearAllToolState();
  732. ToolBarContainer.Visibility = Visibility.Collapsed;
  733. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  734. pdfAnnotationControl.AnnotationCancel();
  735. AnnotationBarBtn.IsChecked = false;
  736. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  737. {
  738. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  739. }
  740. }
  741. else if ((string)item.Content == "Annotation")
  742. {
  743. ToolBarContainer.Visibility = Visibility.Visible;
  744. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  745. {
  746. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  747. }
  748. }
  749. }
  750. #endregion
  751. #region Close window
  752. protected override void OnClosing(CancelEventArgs e)
  753. {
  754. if (pdfViewControl.PDFView.UndoManager.CanSave)
  755. {
  756. MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
  757. if (result == MessageBoxResult.Yes)
  758. {
  759. SaveFile();
  760. }
  761. else if (result == MessageBoxResult.No)
  762. {
  763. }
  764. else
  765. {
  766. e.Cancel = true;
  767. }
  768. }
  769. }
  770. #endregion
  771. }
  772. }