MainWindow.xaml.cs 33 KB

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