MainWindow.xaml.cs 36 KB

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