MainWindow.xaml.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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.PdfViewer;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Runtime.CompilerServices;
  13. using System.Windows;
  14. using System.Windows.Annotations;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Input;
  18. using System.Windows.Media.Animation;
  19. namespace viewer_ctrl_demo
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. private PDFViewControl passwordViewer;
  27. private PDFViewControl pdfViewControl;
  28. private CPDFAnnotationControl pdfAnnotationControl = null;
  29. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  30. public MainWindow()
  31. {
  32. InitializeComponent();
  33. DataContext = this;
  34. }
  35. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  36. {
  37. pdfAnnotationControl = new CPDFAnnotationControl();
  38. LoadDefaultDocument();
  39. BindZoomLevel();
  40. }
  41. private void BindZoomLevel()
  42. {
  43. foreach (double zoomLevel in zoomLevelList)
  44. {
  45. ComboBoxItem zoomItem = new ComboBoxItem();
  46. zoomItem.Content = zoomLevel + "%";
  47. ZoomComboBox.Items.Add(zoomItem);
  48. }
  49. }
  50. private void LoadDocument()
  51. {
  52. pdfViewControl.PDFView?.Load();
  53. PDFGrid.Child = pdfViewControl;
  54. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  55. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  56. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  57. PasswordUI.Closed += PasswordUI_Closed;
  58. PasswordUI.Canceled += PasswordUI_Canceled;
  59. PasswordUI.Confirmed += PasswordUI_Confirmed;
  60. UIElement currentBotaTool = GetBotaTool();
  61. if (currentBotaTool is CPDFSearchControl)
  62. {
  63. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  64. }
  65. if (currentBotaTool is CPDFThumbnailControl)
  66. {
  67. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  68. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  69. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  70. }
  71. if (currentBotaTool is CPDFBookmarkControl)
  72. {
  73. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  74. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  75. }
  76. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  77. InitialPDFViewControl(pdfViewControl);
  78. }
  79. private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.AnnotCommandArgs e)
  80. {
  81. switch (e.CommandType)
  82. {
  83. case CommandType.Context:
  84. e.Handle = true;
  85. if (e.CommandTarget == TargetType.Annot)
  86. {
  87. e.Handle = true;
  88. e.PopupMenu = new ContextMenu();
  89. if (e.PressOnLink)
  90. {
  91. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  92. MenuItem propertyMenu = new MenuItem();
  93. propertyMenu = new MenuItem();
  94. propertyMenu.Header = "Edit";
  95. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  96. propertyMenu.CommandParameter = e;
  97. e.PopupMenu.Items.Add(propertyMenu);
  98. }
  99. else if (e.PressOnAnnot)
  100. {
  101. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  102. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  103. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  104. }
  105. else if (e.PressOnMedia || e.PressOnSound)
  106. {
  107. e.Handle = true;
  108. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  109. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  110. }
  111. else
  112. {
  113. e.Handle = true;
  114. e.PopupMenu = new ContextMenu();
  115. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  116. e.PopupMenu.Items.Add(new Separator());
  117. MenuItem fitWidthMenu = new MenuItem();
  118. fitWidthMenu.Header = "Fit Width";
  119. fitWidthMenu.Click += (o, p) =>
  120. {
  121. if (pdfViewControl != null)
  122. {
  123. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  124. }
  125. };
  126. e.PopupMenu.Items.Add(fitWidthMenu);
  127. MenuItem fitSizeMenu = new MenuItem();
  128. fitSizeMenu.Header = "Actual Size";
  129. fitSizeMenu.Click += (o, p) =>
  130. {
  131. if (pdfViewControl != null)
  132. {
  133. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  134. }
  135. };
  136. e.PopupMenu.Items.Add(fitSizeMenu);
  137. MenuItem zoomInMenu = new MenuItem();
  138. zoomInMenu.Header = "Zoom In";
  139. zoomInMenu.Click += (o, p) =>
  140. {
  141. if (pdfViewControl != null)
  142. {
  143. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  144. pdfViewControl.PDFView?.Zoom(newZoom);
  145. }
  146. };
  147. e.PopupMenu.Items.Add(zoomInMenu);
  148. MenuItem zoomOutMenu = new MenuItem();
  149. zoomOutMenu.Header = "Zoom Out";
  150. zoomOutMenu.Click += (o, p) =>
  151. {
  152. if (pdfViewControl != null)
  153. {
  154. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  155. pdfViewControl.PDFView?.Zoom(newZoom);
  156. }
  157. };
  158. e.PopupMenu.Items.Add(zoomOutMenu);
  159. e.PopupMenu.Items.Add(new Separator());
  160. MenuItem singleView = new MenuItem();
  161. singleView.Header = "Single Page";
  162. singleView.Click += (o, p) =>
  163. {
  164. if (pdfViewControl != null)
  165. {
  166. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  167. }
  168. };
  169. e.PopupMenu.Items.Add(singleView);
  170. MenuItem singleContinuousView = new MenuItem();
  171. singleContinuousView.Header = "Single Continuous Page";
  172. singleContinuousView.Click += (o, p) =>
  173. {
  174. if (pdfViewControl != null)
  175. {
  176. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  177. }
  178. };
  179. e.PopupMenu.Items.Add(singleContinuousView);
  180. MenuItem doubleView = new MenuItem();
  181. doubleView.Header = "Double Page";
  182. doubleView.Click += (o, p) =>
  183. {
  184. if (pdfViewControl != null)
  185. {
  186. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  187. }
  188. };
  189. e.PopupMenu.Items.Add(doubleView);
  190. MenuItem doubleContinuousView = new MenuItem();
  191. doubleContinuousView.Header = "Double Continuous Page";
  192. doubleContinuousView.Click += (o, p) =>
  193. {
  194. if (pdfViewControl != null)
  195. {
  196. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  197. }
  198. };
  199. e.PopupMenu.Items.Add(doubleContinuousView);
  200. }
  201. }
  202. break;
  203. case CommandType.Copy:
  204. e.DoCommand();
  205. break;
  206. case CommandType.Cut:
  207. case CommandType.Paste:
  208. case CommandType.Delete:
  209. e.DoCommand();
  210. break;
  211. default:
  212. break;
  213. }
  214. }
  215. private void PasswordUI_Confirmed(object sender, string e)
  216. {
  217. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  218. {
  219. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  220. if (passwordViewer.PDFView.Document.IsLocked == false)
  221. {
  222. PasswordUI.SetShowError("", Visibility.Collapsed);
  223. PasswordUI.ClearPassword();
  224. PasswordUI.Visibility = Visibility.Collapsed;
  225. PopupBorder.Visibility = Visibility.Collapsed;
  226. pdfViewControl = passwordViewer;
  227. LoadDocument();
  228. }
  229. else
  230. {
  231. PasswordUI.SetShowError("error", Visibility.Visible);
  232. }
  233. }
  234. }
  235. private void PasswordUI_Canceled(object sender, EventArgs e)
  236. {
  237. PopupBorder.Visibility = Visibility.Collapsed;
  238. PasswordUI.Visibility = Visibility.Collapsed;
  239. }
  240. private void PasswordUI_Closed(object sender, EventArgs e)
  241. {
  242. PopupBorder.Visibility = Visibility.Collapsed;
  243. PasswordUI.Visibility = Visibility.Collapsed;
  244. }
  245. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  246. {
  247. if (e.Key == "PageNum")
  248. {
  249. PageRangeText.Text = string.Format("{0}/{1}", e.Value, pdfViewControl.PDFView.Document.PageCount);
  250. }
  251. if (e.Key == "Zoom")
  252. {
  253. ZoomTextBox.Text = string.Format("{0}", (int)((double)e.Value * 100)) + "%";
  254. }
  255. }
  256. private double CheckZoomLevel(double zoom, bool IsGrowth)
  257. {
  258. double standardZoom = 100;
  259. if (zoom <= 0.01)
  260. {
  261. return 0.01;
  262. }
  263. if (zoom >= 10)
  264. {
  265. return 10;
  266. }
  267. zoom *= 100;
  268. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  269. {
  270. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  271. {
  272. standardZoom = zoomLevelList[i + 1];
  273. break;
  274. }
  275. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  276. {
  277. standardZoom = zoomLevelList[i];
  278. break;
  279. }
  280. }
  281. return standardZoom / 100;
  282. }
  283. private void LoadDefaultDocument()
  284. {
  285. string defaultFilePath = "developer_guide_windows.pdf";
  286. pdfViewControl = new PDFViewControl();
  287. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  288. LoadDocument();
  289. }
  290. /// <summary>
  291. /// 搜索工具点击处理
  292. /// </summary>
  293. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  294. {
  295. UIElement botaTool = GetBotaTool();
  296. if (botaTool == null || !(botaTool is CPDFSearchControl))
  297. {
  298. CPDFSearchControl searchControl = new CPDFSearchControl();
  299. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  300. {
  301. searchControl.InitWithPDFViewer(pdfViewControl.PDFView);
  302. }
  303. SetBotaTool(searchControl);
  304. }
  305. ExpandBotaTool(SearchToolButton.IsChecked == true);
  306. ClearToolState(SearchToolButton);
  307. }
  308. /// <summary>
  309. /// 缩略图列表点击处理
  310. /// </summary>
  311. private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
  312. {
  313. UIElement botaTool = GetBotaTool();
  314. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  315. {
  316. CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
  317. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  318. {
  319. thumbControl.InitWithPDFViewer(pdfViewControl.PDFView);
  320. thumbControl.LoadThumb();
  321. }
  322. SetBotaTool(thumbControl);
  323. }
  324. ExpandBotaTool(ThumbToolButton.IsChecked == true);
  325. ClearToolState(ThumbToolButton);
  326. }
  327. /// <summary>
  328. /// 书签列表点击处理
  329. /// </summary>
  330. private void BookmarkToolButtonn_Click(object sender, RoutedEventArgs e)
  331. {
  332. UIElement botaTool = GetBotaTool();
  333. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  334. {
  335. CPDFBookmarkControl bookmarkControl = new CPDFBookmarkControl();
  336. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  337. {
  338. bookmarkControl.InitWithPDFViewer(pdfViewControl.PDFView);
  339. bookmarkControl.LoadBookmark();
  340. }
  341. SetBotaTool(bookmarkControl);
  342. }
  343. ExpandBotaTool(BookmarkToolButton.IsChecked == true);
  344. ClearToolState(BookmarkToolButton);
  345. }
  346. /// <summary>
  347. /// 大纲列表处理
  348. /// </summary>
  349. private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
  350. {
  351. UIElement botaTool = GetBotaTool();
  352. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  353. {
  354. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  355. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  356. {
  357. outlineControl.InitWithPDFViewer(pdfViewControl.PDFView);
  358. }
  359. SetBotaTool(outlineControl);
  360. }
  361. ExpandBotaTool(OutlineToolButton.IsChecked == true);
  362. ClearToolState(OutlineToolButton);
  363. }
  364. /// <summary>
  365. /// 设置Bota工具内容
  366. /// </summary>
  367. /// <param name="newChild"></param>
  368. private void SetBotaTool(UIElement newChild)
  369. {
  370. BotaToolContainer.Child = newChild;
  371. }
  372. /// <summary>
  373. /// 获取Bota工具
  374. /// </summary>
  375. /// <returns></returns>
  376. private UIElement GetBotaTool()
  377. {
  378. return BotaToolContainer.Child;
  379. }
  380. /// <summary>
  381. /// 展开Bota工具
  382. /// </summary>
  383. /// <param name="isExpand"></param>
  384. private void ExpandBotaTool(bool isExpand)
  385. {
  386. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  387. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  388. }
  389. /// <summary>
  390. /// 清除左边工具栏状态
  391. /// </summary>
  392. private void ClearToolState(UIElement ignoreTool)
  393. {
  394. foreach (UIElement child in BotaSideTool.Children)
  395. {
  396. if (child != ignoreTool && child is ToggleButton buttonTool)
  397. {
  398. buttonTool.IsChecked = false;
  399. }
  400. }
  401. }
  402. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  403. {
  404. ToggleButton expandBtn = sender as ToggleButton;
  405. if (expandBtn != null)
  406. {
  407. bool isExpand = expandBtn.IsChecked == true;
  408. ExpandLeftPanel(isExpand);
  409. if (isExpand)
  410. {
  411. ThumbToolButton.IsChecked = isExpand;
  412. ThumbToolButton_Click(ThumbToolButton, new RoutedEventArgs());
  413. }
  414. }
  415. }
  416. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  417. {
  418. ExpandLeftPanel(true);
  419. SearchToolButton.IsChecked = true;
  420. SearchToolButton_Click(SearchToolButton, new RoutedEventArgs());
  421. }
  422. private void ExpandLeftPanel(bool isExpand)
  423. {
  424. ExpandToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  425. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  426. if (isExpand)
  427. {
  428. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  429. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  430. }
  431. else
  432. {
  433. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  434. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  435. }
  436. }
  437. private void Border_MouseEnter(object sender, MouseEventArgs e)
  438. {
  439. FloatPageTool.Visibility = Visibility.Visible;
  440. }
  441. private void PDFGrid_MouseMove(object sender, MouseEventArgs e)
  442. {
  443. FloatPageTool.Visibility = Visibility.Collapsed;
  444. }
  445. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  446. {
  447. ToggleButton toggleButton = sender as ToggleButton;
  448. if (toggleButton != null)
  449. {
  450. if (toggleButton.IsChecked == true)
  451. {
  452. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  453. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  454. PropertyContainer.Child = displayPanel;
  455. PropertyContainer.Visibility = Visibility.Visible;
  456. }
  457. else
  458. {
  459. PropertyContainer.Child = null;
  460. PropertyContainer.Visibility = Visibility.Collapsed;
  461. }
  462. }
  463. }
  464. private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  465. {
  466. ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem;
  467. if (selectItem != null && selectItem.Content != null && pdfViewControl != null)
  468. {
  469. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double zoomLevel))
  470. {
  471. pdfViewControl.PDFView.Zoom(zoomLevel / 100D);
  472. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  473. }
  474. }
  475. }
  476. private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
  477. {
  478. if (pdfViewControl != null)
  479. {
  480. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  481. pdfViewControl.PDFView.Zoom(newZoom);
  482. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  483. }
  484. }
  485. private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
  486. {
  487. if (pdfViewControl != null)
  488. {
  489. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  490. pdfViewControl.PDFView.Zoom(newZoom);
  491. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  492. }
  493. }
  494. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  495. {
  496. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  497. }
  498. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  499. {
  500. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  501. }
  502. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  503. {
  504. PasswordUI.Visibility = Visibility.Collapsed;
  505. FileInfoUI.Visibility = Visibility.Visible;
  506. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  507. PopupBorder.Visibility = Visibility.Visible;
  508. }
  509. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  510. {
  511. PopupBorder.Visibility = Visibility.Collapsed;
  512. }
  513. private void OpenFile_Click(object sender, RoutedEventArgs e)
  514. {
  515. try
  516. {
  517. string filePath = CommonHelper.GetFilePathOrEmpty();
  518. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  519. {
  520. passwordViewer = new PDFViewControl();
  521. passwordViewer.PDFView.InitDocument(filePath);
  522. if (passwordViewer.PDFView.Document == null)
  523. {
  524. MessageBox.Show("Open File Failed");
  525. return;
  526. }
  527. if (passwordViewer.PDFView.Document.IsLocked)
  528. {
  529. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  530. PasswordUI.ClearPassword();
  531. PopupBorder.Visibility = Visibility.Visible;
  532. PasswordUI.Visibility = Visibility.Visible;
  533. }
  534. else
  535. {
  536. pdfViewControl = passwordViewer;
  537. LoadDocument();
  538. }
  539. }
  540. }
  541. catch (Exception ex)
  542. {
  543. }
  544. }
  545. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  546. {
  547. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  548. {
  549. try
  550. {
  551. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  552. if (pdfDoc.WriteToLoadedPath())
  553. {
  554. return;
  555. }
  556. SaveFileDialog saveDialog = new SaveFileDialog();
  557. saveDialog.Filter = "(*.pdf)|*.pdf";
  558. saveDialog.DefaultExt = ".pdf";
  559. saveDialog.OverwritePrompt = true;
  560. if (saveDialog.ShowDialog() == true)
  561. {
  562. pdfDoc.WriteToFilePath(saveDialog.FileName);
  563. }
  564. }
  565. catch (Exception ex)
  566. {
  567. }
  568. }
  569. }
  570. #region Annotation
  571. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  572. {
  573. pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  574. pdfAnnotationControl.AnnotationCancel();
  575. AnnotationBarControl.ClearAllToolState();
  576. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  577. }
  578. #endregion
  579. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  580. {
  581. 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.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio };
  582. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  583. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  584. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  585. AnnotationBarControl.UndoRedoEvent += AnnotationBarControl_UndoRedoEvent;
  586. }
  587. private void AnnotationBarControl_UndoRedoEvent(object sender, string e)
  588. {
  589. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  590. {
  591. if (e == "Undo")
  592. {
  593. pdfViewControl.PDFView.UndoManager?.Undo();
  594. }
  595. else
  596. {
  597. pdfViewControl.PDFView.UndoManager?.Redo();
  598. }
  599. }
  600. }
  601. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  602. {
  603. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  604. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  605. }
  606. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  607. {
  608. pdfAnnotationControl.AnnotationCancel();
  609. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  610. }
  611. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  612. {
  613. pdfAnnotationControl.LoadAnnotationPanel(e);
  614. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  615. }
  616. private void AnnotationBarControl_Click(object sender, RoutedEventArgs e)
  617. {
  618. ToggleButton toggleButton = sender as ToggleButton;
  619. if (toggleButton != null)
  620. {
  621. if (toggleButton.IsChecked == true)
  622. {
  623. if (pdfAnnotationControl != null)
  624. {
  625. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  626. }
  627. }
  628. else
  629. {
  630. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  631. }
  632. }
  633. }
  634. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  635. {
  636. PropertyContainer.Width = 260;
  637. PropertyContainer.Child = properytPanel;
  638. PropertyContainer.Visibility = visible;
  639. }
  640. private void EditLink_Click(object sender, RoutedEventArgs e)
  641. {
  642. PropertyContainer.Visibility = Visibility.Visible;
  643. }
  644. }
  645. }