MainWindow.xaml.cs 37 KB

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