MainWindow.xaml.cs 30 KB

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