MainWindow.xaml.cs 36 KB

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