MainWindow.xaml.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. using ComPDFKit.PDFDocument;
  2. using compdfkit_tools.Helper;
  3. using compdfkit_tools.PDFControl;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. using System.Drawing;
  25. using ComPDFKit.PDFPage;
  26. using ComPDFKitViewer.PdfViewer;
  27. using compdfkit_tools.Form;
  28. namespace form_ctrl_demo
  29. {
  30. /// <summary>
  31. /// Interaction logic for MainWindow.xaml
  32. /// </summary>
  33. public partial class MainWindow : Window
  34. {
  35. private PDFViewControl passwordViewer;
  36. private PDFViewControl pdfViewControl;
  37. private FromPropertyControl fromPropertyControl;
  38. bool LoadPDFFormTool = false;
  39. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  40. public bool CanSave
  41. {
  42. get
  43. {
  44. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  45. {
  46. return pdfViewControl.PDFView.UndoManager.CanSave;
  47. }
  48. return false;
  49. }
  50. }
  51. public MainWindow()
  52. {
  53. InitializeComponent();
  54. Loaded += MainWindow_Loaded;
  55. DataContext = this;
  56. }
  57. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  58. {
  59. fromPropertyControl = new FromPropertyControl();
  60. LoadDefaultDocument();
  61. BindZoomLevel();
  62. }
  63. private void BindZoomLevel()
  64. {
  65. foreach (double zoomLevel in zoomLevelList)
  66. {
  67. ComboBoxItem zoomItem = new ComboBoxItem();
  68. zoomItem.Content = zoomLevel + "%";
  69. ZoomComboBox.Items.Add(zoomItem);
  70. }
  71. }
  72. private void LoadDocument()
  73. {
  74. pdfViewControl.PDFView?.Load();
  75. pdfViewControl.PDFView?.SetShowLink(true);
  76. PDFGrid.Child = pdfViewControl;
  77. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  78. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  79. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  80. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  81. PasswordUI.Closed -= PasswordUI_Closed;
  82. PasswordUI.Canceled -= PasswordUI_Canceled;
  83. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  84. PasswordUI.Closed += PasswordUI_Closed;
  85. PasswordUI.Canceled += PasswordUI_Canceled;
  86. PasswordUI.Confirmed += PasswordUI_Confirmed;
  87. UIElement currentBotaTool = GetBotaTool();
  88. if (currentBotaTool is CPDFSearchControl)
  89. {
  90. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  91. }
  92. if (currentBotaTool is CPDFThumbnailControl)
  93. {
  94. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  95. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  96. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  97. }
  98. if (currentBotaTool is CPDFBookmarkControl)
  99. {
  100. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  101. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  102. }
  103. ViewSettingBtn.IsChecked = false;
  104. PropertyContainer.Child = null;
  105. PropertyContainer.Visibility = Visibility.Collapsed;
  106. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  107. InitialPDFViewControl(pdfViewControl);
  108. }
  109. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  110. {
  111. if (e != null && e.CommandType == CommandType.Context)
  112. {
  113. if (e.PressOnSelectedText)
  114. {
  115. e.Handle = true;
  116. e.PopupMenu = new ContextMenu();
  117. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  118. }
  119. else if (e.CommandTarget == TargetType.ImageSelection)
  120. {
  121. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  122. {
  123. e.Handle = true;
  124. e.PopupMenu = new ContextMenu();
  125. MenuItem imageCopyMenu = new MenuItem();
  126. imageCopyMenu = new MenuItem();
  127. imageCopyMenu.Header = "Copy Images";
  128. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  129. imageCopyMenu.CommandParameter = e;
  130. e.PopupMenu.Items.Add(imageCopyMenu);
  131. MenuItem imageExtraMenu = new MenuItem();
  132. imageExtraMenu = new MenuItem();
  133. imageExtraMenu.Header = "Extract Images";
  134. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  135. imageExtraMenu.CommandParameter = e;
  136. e.PopupMenu.Items.Add(imageExtraMenu);
  137. }
  138. }
  139. else
  140. {
  141. e.Handle = true;
  142. e.PopupMenu = new ContextMenu();
  143. MenuItem fitWidthMenu = new MenuItem();
  144. fitWidthMenu.Header = "Fit Width";
  145. fitWidthMenu.Click += (o, p) =>
  146. {
  147. if (pdfViewControl != null)
  148. {
  149. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  150. }
  151. };
  152. e.PopupMenu.Items.Add(fitWidthMenu);
  153. MenuItem fitSizeMenu = new MenuItem();
  154. fitSizeMenu.Header = "Actual Size";
  155. fitSizeMenu.Click += (o, p) =>
  156. {
  157. if (pdfViewControl != null)
  158. {
  159. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  160. }
  161. };
  162. e.PopupMenu.Items.Add(fitSizeMenu);
  163. MenuItem zoomInMenu = new MenuItem();
  164. zoomInMenu.Header = "Zoom In";
  165. zoomInMenu.Click += (o, p) =>
  166. {
  167. if (pdfViewControl != null)
  168. {
  169. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  170. pdfViewControl.PDFView?.Zoom(newZoom);
  171. }
  172. };
  173. e.PopupMenu.Items.Add(zoomInMenu);
  174. MenuItem zoomOutMenu = new MenuItem();
  175. zoomOutMenu.Header = "Zoom Out";
  176. zoomOutMenu.Click += (o, p) =>
  177. {
  178. if (pdfViewControl != null)
  179. {
  180. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  181. pdfViewControl.PDFView?.Zoom(newZoom);
  182. }
  183. };
  184. e.PopupMenu.Items.Add(zoomOutMenu);
  185. e.PopupMenu.Items.Add(new Separator());
  186. MenuItem singleView = new MenuItem();
  187. singleView.Header = "Single Page";
  188. singleView.Click += (o, p) =>
  189. {
  190. if (pdfViewControl != null)
  191. {
  192. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  193. }
  194. };
  195. e.PopupMenu.Items.Add(singleView);
  196. MenuItem singleContinuousView = new MenuItem();
  197. singleContinuousView.Header = "Single Page Continuous";
  198. singleContinuousView.Click += (o, p) =>
  199. {
  200. if (pdfViewControl != null)
  201. {
  202. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  203. }
  204. };
  205. e.PopupMenu.Items.Add(singleContinuousView);
  206. MenuItem doubleView = new MenuItem();
  207. doubleView.Header = "Two Pages";
  208. doubleView.Click += (o, p) =>
  209. {
  210. if (pdfViewControl != null)
  211. {
  212. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  213. }
  214. };
  215. e.PopupMenu.Items.Add(doubleView);
  216. MenuItem doubleContinuousView = new MenuItem();
  217. doubleContinuousView.Header = "Two Pages Continuous";
  218. doubleContinuousView.Click += (o, p) =>
  219. {
  220. if (pdfViewControl != null)
  221. {
  222. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  223. }
  224. };
  225. e.PopupMenu.Items.Add(doubleContinuousView);
  226. }
  227. }
  228. if (e != null && e.CommandType == CommandType.Copy)
  229. {
  230. e.DoCommand();
  231. }
  232. }
  233. private void PasswordUI_Confirmed(object sender, string e)
  234. {
  235. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  236. {
  237. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  238. if (passwordViewer.PDFView.Document.IsLocked == false)
  239. {
  240. PasswordUI.SetShowError("", Visibility.Collapsed);
  241. PasswordUI.ClearPassword();
  242. PasswordUI.Visibility = Visibility.Collapsed;
  243. PopupBorder.Visibility = Visibility.Collapsed;
  244. pdfViewControl = passwordViewer;
  245. LoadDocument();
  246. }
  247. else
  248. {
  249. PasswordUI.SetShowError("error", Visibility.Visible);
  250. }
  251. }
  252. }
  253. private void PasswordUI_Canceled(object sender, EventArgs e)
  254. {
  255. PopupBorder.Visibility = Visibility.Collapsed;
  256. PasswordUI.Visibility = Visibility.Collapsed;
  257. }
  258. private void PasswordUI_Closed(object sender, EventArgs e)
  259. {
  260. PopupBorder.Visibility = Visibility.Collapsed;
  261. PasswordUI.Visibility = Visibility.Collapsed;
  262. }
  263. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  264. {
  265. if (e.Key == "PageNum")
  266. {
  267. PageRangeText.Text = string.Format("{0}/{1}", e.Value, pdfViewControl.PDFView.Document.PageCount);
  268. }
  269. if (e.Key == "Zoom")
  270. {
  271. ZoomTextBox.Text = string.Format("{0}", (int)((double)e.Value * 100)) + "%";
  272. }
  273. }
  274. private double CheckZoomLevel(double zoom, bool IsGrowth)
  275. {
  276. double standardZoom = 100;
  277. if (zoom <= 0.01)
  278. {
  279. return 0.01;
  280. }
  281. if (zoom >= 10)
  282. {
  283. return 10;
  284. }
  285. zoom *= 100;
  286. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  287. {
  288. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  289. {
  290. standardZoom = zoomLevelList[i + 1];
  291. break;
  292. }
  293. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  294. {
  295. standardZoom = zoomLevelList[i];
  296. break;
  297. }
  298. }
  299. return standardZoom / 100;
  300. }
  301. private void LoadDefaultDocument()
  302. {
  303. string defaultFilePath = "developer_guide_windows.pdf";
  304. pdfViewControl = new PDFViewControl();
  305. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  306. LoadDocument();
  307. }
  308. /// <summary>
  309. /// 搜索工具点击处理
  310. /// </summary>
  311. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  312. {
  313. UIElement botaTool = GetBotaTool();
  314. if (botaTool == null || !(botaTool is CPDFSearchControl))
  315. {
  316. CPDFSearchControl searchControl = new CPDFSearchControl();
  317. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  318. {
  319. searchControl.InitWithPDFViewer(pdfViewControl.PDFView);
  320. }
  321. SetBotaTool(searchControl);
  322. }
  323. ExpandBotaTool(SearchToolButton.IsChecked == true);
  324. ClearToolState(SearchToolButton);
  325. }
  326. /// <summary>
  327. /// 缩略图列表点击处理
  328. /// </summary>
  329. private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
  330. {
  331. UIElement botaTool = GetBotaTool();
  332. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  333. {
  334. CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
  335. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  336. {
  337. thumbControl.InitWithPDFViewer(pdfViewControl.PDFView);
  338. thumbControl.LoadThumb();
  339. }
  340. SetBotaTool(thumbControl);
  341. }
  342. ExpandBotaTool(ThumbToolButton.IsChecked == true);
  343. ClearToolState(ThumbToolButton);
  344. }
  345. /// <summary>
  346. /// 书签列表点击处理
  347. /// </summary>
  348. private void BookmarkToolButtonn_Click(object sender, RoutedEventArgs e)
  349. {
  350. UIElement botaTool = GetBotaTool();
  351. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  352. {
  353. CPDFBookmarkControl bookmarkControl = new CPDFBookmarkControl();
  354. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  355. {
  356. bookmarkControl.InitWithPDFViewer(pdfViewControl.PDFView);
  357. bookmarkControl.LoadBookmark();
  358. }
  359. SetBotaTool(bookmarkControl);
  360. }
  361. ExpandBotaTool(BookmarkToolButton.IsChecked == true);
  362. ClearToolState(BookmarkToolButton);
  363. }
  364. /// <summary>
  365. /// 大纲列表处理
  366. /// </summary>
  367. private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
  368. {
  369. UIElement botaTool = GetBotaTool();
  370. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  371. {
  372. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  373. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  374. {
  375. outlineControl.InitWithPDFViewer(pdfViewControl.PDFView);
  376. }
  377. SetBotaTool(outlineControl);
  378. }
  379. ExpandBotaTool(OutlineToolButton.IsChecked == true);
  380. ClearToolState(OutlineToolButton);
  381. }
  382. /// <summary>
  383. /// 设置Bota工具内容
  384. /// </summary>
  385. /// <param name="newChild"></param>
  386. private void SetBotaTool(UIElement newChild)
  387. {
  388. BotaToolContainer.Child = newChild;
  389. }
  390. /// <summary>
  391. /// 获取Bota工具
  392. /// </summary>
  393. /// <returns></returns>
  394. private UIElement GetBotaTool()
  395. {
  396. return BotaToolContainer.Child;
  397. }
  398. /// <summary>
  399. /// 展开Bota工具
  400. /// </summary>
  401. /// <param name="isExpand"></param>
  402. private void ExpandBotaTool(bool isExpand)
  403. {
  404. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  405. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  406. }
  407. /// <summary>
  408. /// 清除左边工具栏状态
  409. /// </summary>
  410. private void ClearToolState(UIElement ignoreTool)
  411. {
  412. foreach (UIElement child in BotaSideTool.Children)
  413. {
  414. if (child != ignoreTool && child is ToggleButton buttonTool)
  415. {
  416. buttonTool.IsChecked = false;
  417. }
  418. }
  419. }
  420. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  421. {
  422. ToggleButton expandBtn = sender as ToggleButton;
  423. if (expandBtn != null)
  424. {
  425. bool isExpand = expandBtn.IsChecked == true;
  426. ExpandLeftPanel(isExpand);
  427. if (isExpand)
  428. {
  429. ThumbToolButton.IsChecked = isExpand;
  430. ThumbToolButton_Click(ThumbToolButton, new RoutedEventArgs());
  431. }
  432. }
  433. }
  434. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  435. {
  436. ExpandLeftPanel(true);
  437. SearchToolButton.IsChecked = true;
  438. SearchToolButton_Click(SearchToolButton, new RoutedEventArgs());
  439. }
  440. private void ExpandLeftPanel(bool isExpand)
  441. {
  442. ExpandToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  443. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  444. if (isExpand)
  445. {
  446. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  447. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  448. }
  449. else
  450. {
  451. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  452. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  453. }
  454. }
  455. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  456. {
  457. ToggleButton toggleButton = sender as ToggleButton;
  458. if (toggleButton != null)
  459. {
  460. if (toggleButton.IsChecked == true)
  461. {
  462. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  463. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  464. PropertyContainer.Child = displayPanel;
  465. PropertyContainer.Visibility = Visibility.Visible;
  466. if ((bool)FormBarBtn.IsChecked)
  467. {
  468. FormBarBtn.IsChecked = false;
  469. }
  470. }
  471. else
  472. {
  473. PropertyContainer.Child = null;
  474. PropertyContainer.Visibility = Visibility.Collapsed;
  475. }
  476. }
  477. }
  478. private void FormBarBtn_Click(object sender, RoutedEventArgs e)
  479. {
  480. ToggleButton toggleButton = sender as ToggleButton;
  481. if (toggleButton != null)
  482. {
  483. if (toggleButton.IsChecked == true)
  484. {
  485. //if (pdfAnnotationControl != null)
  486. //{
  487. ExpandRightPropertyPanel(fromPropertyControl, Visibility.Visible);
  488. if ((bool)ViewSettingBtn.IsChecked)
  489. {
  490. ViewSettingBtn.IsChecked = false;
  491. }
  492. //}
  493. }
  494. else
  495. {
  496. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  497. }
  498. }
  499. }
  500. private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  501. {
  502. ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem;
  503. if (selectItem != null && selectItem.Content != null && pdfViewControl != null)
  504. {
  505. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double zoomLevel))
  506. {
  507. pdfViewControl.PDFView.Zoom(zoomLevel / 100D);
  508. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  509. }
  510. }
  511. }
  512. private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
  513. {
  514. if (pdfViewControl != null)
  515. {
  516. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  517. pdfViewControl.PDFView.Zoom(newZoom);
  518. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  519. }
  520. }
  521. private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
  522. {
  523. if (pdfViewControl != null)
  524. {
  525. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  526. pdfViewControl.PDFView.Zoom(newZoom);
  527. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  528. }
  529. }
  530. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  531. {
  532. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  533. }
  534. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  535. {
  536. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  537. }
  538. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  539. {
  540. PasswordUI.Visibility = Visibility.Collapsed;
  541. FileInfoUI.Visibility = Visibility.Visible;
  542. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  543. PopupBorder.Visibility = Visibility.Visible;
  544. }
  545. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  546. {
  547. PropertyContainer.Width = 260;
  548. PropertyContainer.Child = properytPanel;
  549. PropertyContainer.Visibility = visible;
  550. }
  551. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  552. {
  553. PopupBorder.Visibility = Visibility.Collapsed;
  554. }
  555. private void OpenFile_Click(object sender, RoutedEventArgs e)
  556. {
  557. try
  558. {
  559. string filePath = CommonHelper.GetFilePathOrEmpty();
  560. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  561. {
  562. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  563. {
  564. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  565. if (oldFilePath.ToLower() == filePath.ToLower())
  566. {
  567. return;
  568. }
  569. }
  570. passwordViewer = new PDFViewControl();
  571. passwordViewer.PDFView.InitDocument(filePath);
  572. if (passwordViewer.PDFView.Document == null)
  573. {
  574. MessageBox.Show("Open File Failed");
  575. return;
  576. }
  577. if (passwordViewer.PDFView.Document.IsLocked)
  578. {
  579. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  580. PasswordUI.ClearPassword();
  581. PopupBorder.Visibility = Visibility.Visible;
  582. PasswordUI.Visibility = Visibility.Visible;
  583. }
  584. else
  585. {
  586. pdfViewControl = passwordViewer;
  587. LoadDocument();
  588. }
  589. }
  590. }
  591. catch (Exception ex)
  592. {
  593. }
  594. }
  595. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  596. {
  597. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  598. {
  599. try
  600. {
  601. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  602. if (pdfDoc.WriteToLoadedPath())
  603. {
  604. return;
  605. }
  606. SaveFileDialog saveDialog = new SaveFileDialog();
  607. saveDialog.Filter = "(*.pdf)|*.pdf";
  608. saveDialog.DefaultExt = ".pdf";
  609. saveDialog.OverwritePrompt = true;
  610. if (saveDialog.ShowDialog() == true)
  611. {
  612. pdfDoc.WriteToFilePath(saveDialog.FileName);
  613. }
  614. }
  615. catch (Exception ex)
  616. {
  617. }
  618. }
  619. }
  620. private void CopyImage_Click(object sender, RoutedEventArgs e)
  621. {
  622. try
  623. {
  624. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  625. if (imageDict != null && imageDict.Count > 0)
  626. {
  627. foreach (int pageIndex in imageDict.Keys)
  628. {
  629. List<Bitmap> imageList = imageDict[pageIndex];
  630. foreach (Bitmap image in imageList)
  631. {
  632. MemoryStream ms = new MemoryStream();
  633. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  634. BitmapImage imageData = new BitmapImage();
  635. imageData.BeginInit();
  636. imageData.StreamSource = ms;
  637. imageData.CacheOption = BitmapCacheOption.OnLoad;
  638. imageData.EndInit();
  639. imageData.Freeze();
  640. Clipboard.SetImage(imageData);
  641. break;
  642. }
  643. }
  644. }
  645. }
  646. catch (Exception ex)
  647. {
  648. }
  649. }
  650. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  651. {
  652. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  653. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  654. {
  655. string choosePath = folderDialog.SelectedPath;
  656. string openPath = choosePath;
  657. try
  658. {
  659. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  660. if (imageDict != null && imageDict.Count > 0)
  661. {
  662. foreach (int pageIndex in imageDict.Keys)
  663. {
  664. List<Bitmap> imageList = imageDict[pageIndex];
  665. foreach (Bitmap image in imageList)
  666. {
  667. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  668. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  669. openPath = savePath;
  670. }
  671. }
  672. }
  673. Process.Start("explorer", "/select,\"" + openPath + "\"");
  674. }
  675. catch (Exception ex)
  676. {
  677. }
  678. }
  679. }
  680. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  681. {
  682. SetFromMode();
  683. }
  684. private void SetFromMode()
  685. {
  686. if (LoadPDFFormTool)
  687. {
  688. PDFFormTool.ClearAllToolState();
  689. }
  690. if (pdfViewControl != null && ViewComboBox != null)
  691. {
  692. if (ViewComboBox.SelectedIndex == 0)
  693. {
  694. PDFFormTool.Visibility = Visibility.Collapsed;
  695. pdfViewControl.PDFView?.SetShowLink(true);
  696. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  697. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  698. pdfViewControl.PDFView?.ReloadDocument();
  699. if (PropertyContainer != null)
  700. {
  701. PropertyContainer.Child = null;
  702. }
  703. return;
  704. }
  705. if (ViewComboBox.SelectedIndex == 1)
  706. {
  707. PDFFormTool.Visibility = Visibility.Visible;
  708. pdfViewControl.PDFView?.SetShowLink(false);
  709. pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
  710. pdfViewControl.PDFView?.ReloadDocument();
  711. }
  712. }
  713. }
  714. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  715. {
  716. PDFFormTool.SetPDFViewer(newPDFViewer.PDFView);
  717. fromPropertyControl.SetPDFViewer(newPDFViewer.PDFView);
  718. PDFFormTool.ClearAllToolState();
  719. newPDFViewer.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  720. newPDFViewer.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  721. newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler; ;
  722. }
  723. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  724. {
  725. if (e is WidgetSignArgs)
  726. {
  727. var x = 1;
  728. }
  729. }
  730. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  731. {
  732. if (e == null || e.IsAnnotCreateReset)
  733. { }
  734. else
  735. {
  736. switch (e.GetAnnotTypes())
  737. {
  738. case AnnotArgsType.WidgetViewForm:
  739. WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
  740. switch (formArgs.WidgeType)
  741. {
  742. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  743. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  744. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  745. ExpandRightPropertyPanel(fromPropertyControl, Visibility.Visible);
  746. if ((bool)ViewSettingBtn.IsChecked)
  747. {
  748. ViewSettingBtn.IsChecked = false;
  749. }
  750. FormBarBtn.IsChecked = true;
  751. break;
  752. default:
  753. break;
  754. }
  755. fromPropertyControl.SetPropertyForType(formArgs,e);
  756. break;
  757. }
  758. }
  759. }
  760. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  761. {
  762. if (e != null && e.Count > 0)
  763. {
  764. AnnotEditEvent editEvent = e[e.Count - 1];
  765. if (editEvent.EditAction == ActionType.Add)
  766. {
  767. pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  768. }
  769. }
  770. }
  771. private void PDFFormTool_Loaded(object sender, RoutedEventArgs e)
  772. {
  773. LoadPDFFormTool = true;
  774. }
  775. private void PDFFormTool_Unloaded(object sender, RoutedEventArgs e)
  776. {
  777. LoadPDFFormTool = false;
  778. }
  779. }
  780. }