MainWindow.xaml.cs 36 KB

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