MainWindow.xaml.cs 32 KB

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