MainWindow.xaml.cs 36 KB

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