MainWindow.xaml.cs 34 KB

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