MainWindow.xaml.cs 29 KB

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