MainWindow.xaml.cs 32 KB

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