MainWindow.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. ToolExpandBtn.IsChecked = true;
  449. SearchToolButton_Click(SearchToolButton, new RoutedEventArgs());
  450. }
  451. private void ExpandLeftPanel(bool isExpand)
  452. {
  453. ExpandToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  454. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  455. if (isExpand)
  456. {
  457. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  458. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  459. }
  460. else
  461. {
  462. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  463. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  464. }
  465. }
  466. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  467. {
  468. ToggleButton toggleButton = sender as ToggleButton;
  469. if (toggleButton != null)
  470. {
  471. if (toggleButton.IsChecked == true)
  472. {
  473. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  474. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  475. PropertyContainer.Child = displayPanel;
  476. PropertyContainer.Visibility = Visibility.Visible;
  477. if ((bool)FormBarBtn.IsChecked)
  478. {
  479. FormBarBtn.IsChecked = false;
  480. }
  481. }
  482. else
  483. {
  484. PropertyContainer.Child = null;
  485. PropertyContainer.Visibility = Visibility.Collapsed;
  486. }
  487. }
  488. }
  489. private void FormBarBtn_Click(object sender, RoutedEventArgs e)
  490. {
  491. ToggleButton toggleButton = sender as ToggleButton;
  492. if (toggleButton != null)
  493. {
  494. if (toggleButton.IsChecked == true)
  495. {
  496. //if (pdfAnnotationControl != null)
  497. //{
  498. ExpandRightPropertyPanel(fromPropertyControl, Visibility.Visible);
  499. if ((bool)ViewSettingBtn.IsChecked)
  500. {
  501. ViewSettingBtn.IsChecked = false;
  502. }
  503. //}
  504. }
  505. else
  506. {
  507. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  508. }
  509. }
  510. }
  511. private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  512. {
  513. ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem;
  514. if (selectItem != null && selectItem.Content != null && pdfViewControl != null)
  515. {
  516. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double zoomLevel))
  517. {
  518. pdfViewControl.PDFView.Zoom(zoomLevel / 100D);
  519. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  520. }
  521. }
  522. }
  523. private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
  524. {
  525. if (pdfViewControl != null)
  526. {
  527. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  528. pdfViewControl.PDFView.Zoom(newZoom);
  529. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  530. }
  531. }
  532. private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
  533. {
  534. if (pdfViewControl != null)
  535. {
  536. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  537. pdfViewControl.PDFView.Zoom(newZoom);
  538. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  539. }
  540. }
  541. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  542. {
  543. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  544. }
  545. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  546. {
  547. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  548. }
  549. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  550. {
  551. PasswordUI.Visibility = Visibility.Collapsed;
  552. FileInfoUI.Visibility = Visibility.Visible;
  553. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  554. PopupBorder.Visibility = Visibility.Visible;
  555. }
  556. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  557. {
  558. PropertyContainer.Width = 260;
  559. PropertyContainer.Child = properytPanel;
  560. PropertyContainer.Visibility = visible;
  561. }
  562. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  563. {
  564. PopupBorder.Visibility = Visibility.Collapsed;
  565. }
  566. private void OpenFile_Click(object sender, RoutedEventArgs e)
  567. {
  568. try
  569. {
  570. string filePath = CommonHelper.GetFilePathOrEmpty();
  571. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  572. {
  573. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  574. {
  575. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  576. if (oldFilePath.ToLower() == filePath.ToLower())
  577. {
  578. return;
  579. }
  580. }
  581. passwordViewer = new PDFViewControl();
  582. passwordViewer.PDFView.InitDocument(filePath);
  583. if (passwordViewer.PDFView.Document == null)
  584. {
  585. MessageBox.Show("Open File Failed");
  586. return;
  587. }
  588. if (passwordViewer.PDFView.Document.IsLocked)
  589. {
  590. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  591. PasswordUI.ClearPassword();
  592. PopupBorder.Visibility = Visibility.Visible;
  593. PasswordUI.Visibility = Visibility.Visible;
  594. }
  595. else
  596. {
  597. pdfViewControl = passwordViewer;
  598. LoadDocument();
  599. }
  600. }
  601. }
  602. catch (Exception ex)
  603. {
  604. }
  605. }
  606. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  607. {
  608. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  609. {
  610. try
  611. {
  612. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  613. if (pdfDoc.WriteToLoadedPath())
  614. {
  615. return;
  616. }
  617. SaveFileDialog saveDialog = new SaveFileDialog();
  618. saveDialog.Filter = "(*.pdf)|*.pdf";
  619. saveDialog.DefaultExt = ".pdf";
  620. saveDialog.OverwritePrompt = true;
  621. if (saveDialog.ShowDialog() == true)
  622. {
  623. pdfDoc.WriteToFilePath(saveDialog.FileName);
  624. }
  625. }
  626. catch (Exception ex)
  627. {
  628. }
  629. }
  630. }
  631. private void CopyImage_Click(object sender, RoutedEventArgs e)
  632. {
  633. try
  634. {
  635. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  636. if (imageDict != null && imageDict.Count > 0)
  637. {
  638. foreach (int pageIndex in imageDict.Keys)
  639. {
  640. List<Bitmap> imageList = imageDict[pageIndex];
  641. foreach (Bitmap image in imageList)
  642. {
  643. MemoryStream ms = new MemoryStream();
  644. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  645. BitmapImage imageData = new BitmapImage();
  646. imageData.BeginInit();
  647. imageData.StreamSource = ms;
  648. imageData.CacheOption = BitmapCacheOption.OnLoad;
  649. imageData.EndInit();
  650. imageData.Freeze();
  651. Clipboard.SetImage(imageData);
  652. break;
  653. }
  654. }
  655. }
  656. }
  657. catch (Exception ex)
  658. {
  659. }
  660. }
  661. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  662. {
  663. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  664. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  665. {
  666. string choosePath = folderDialog.SelectedPath;
  667. string openPath = choosePath;
  668. try
  669. {
  670. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  671. if (imageDict != null && imageDict.Count > 0)
  672. {
  673. foreach (int pageIndex in imageDict.Keys)
  674. {
  675. List<Bitmap> imageList = imageDict[pageIndex];
  676. foreach (Bitmap image in imageList)
  677. {
  678. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  679. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  680. openPath = savePath;
  681. }
  682. }
  683. }
  684. Process.Start("explorer", "/select,\"" + openPath + "\"");
  685. }
  686. catch (Exception ex)
  687. {
  688. }
  689. }
  690. }
  691. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  692. {
  693. SetFromMode();
  694. }
  695. private void SetFromMode()
  696. {
  697. if (LoadPDFFormTool)
  698. {
  699. PDFFormTool.ClearAllToolState();
  700. }
  701. if (pdfViewControl != null && ViewComboBox != null)
  702. {
  703. if (ViewComboBox.SelectedIndex == 0)
  704. {
  705. PDFFormTool.Visibility = Visibility.Collapsed;
  706. pdfViewControl.PDFView?.SetShowLink(true);
  707. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  708. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  709. pdfViewControl.PDFView?.ReloadDocument();
  710. if (PropertyContainer != null)
  711. {
  712. fromPropertyControl.CleanProperty();
  713. PropertyContainer.Width = 0;
  714. PropertyContainer.Child = null;
  715. ViewSettingBtn.IsChecked = false;
  716. FormBarBtn.IsChecked = false;
  717. }
  718. return;
  719. }
  720. if (ViewComboBox.SelectedIndex == 1)
  721. {
  722. PDFFormTool.Visibility = Visibility.Visible;
  723. pdfViewControl.PDFView?.SetShowLink(false);
  724. pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
  725. pdfViewControl.PDFView?.ReloadDocument();
  726. }
  727. }
  728. }
  729. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  730. {
  731. PDFFormTool.SetPDFViewer(newPDFViewer.PDFView, fromPropertyControl);
  732. fromPropertyControl.SetPDFViewer(newPDFViewer.PDFView);
  733. PDFFormTool.ClearAllToolState();
  734. newPDFViewer.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  735. newPDFViewer.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  736. newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler; ;
  737. }
  738. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  739. {
  740. if (e is WidgetSignArgs)
  741. {
  742. var x = 1;
  743. }
  744. }
  745. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  746. {
  747. if (e == null || e.IsAnnotCreateReset)
  748. { }
  749. else
  750. {
  751. switch (e.GetAnnotTypes())
  752. {
  753. case AnnotArgsType.WidgetViewForm:
  754. WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
  755. switch (formArgs.WidgeType)
  756. {
  757. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  758. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  759. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  760. ExpandRightPropertyPanel(fromPropertyControl, Visibility.Visible);
  761. if ((bool)ViewSettingBtn.IsChecked)
  762. {
  763. ViewSettingBtn.IsChecked = false;
  764. }
  765. FormBarBtn.IsChecked = true;
  766. break;
  767. default:
  768. break;
  769. }
  770. fromPropertyControl.SetPropertyForType(formArgs, e);
  771. break;
  772. }
  773. }
  774. }
  775. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  776. {
  777. if (e != null && e.Count > 0)
  778. {
  779. AnnotEditEvent editEvent = e[e.Count - 1];
  780. if (editEvent.EditAction == ActionType.Add)
  781. {
  782. pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  783. }
  784. }
  785. }
  786. private void PDFFormTool_Loaded(object sender, RoutedEventArgs e)
  787. {
  788. LoadPDFFormTool = true;
  789. }
  790. private void PDFFormTool_Unloaded(object sender, RoutedEventArgs e)
  791. {
  792. LoadPDFFormTool = false;
  793. }
  794. }
  795. }