MainWindow.xaml.cs 36 KB

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