MainWindow.xaml.cs 33 KB

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