RegularViewerControl.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Runtime.CompilerServices;
  7. using System.Windows;
  8. using System.Windows.Input;
  9. using System.Windows.Media.Imaging;
  10. using Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  11. using Compdfkit_Tools.Common;
  12. using Compdfkit_Tools.Helper;
  13. using Compdfkit_Tools.PDFControl;
  14. using ComPDFKit.PDFPage;
  15. using ComPDFKit.PDFPage.Edit;
  16. using ComPDFKitViewer;
  17. using ComPDFKit.Tool;
  18. using ContextMenu = System.Windows.Controls.ContextMenu;
  19. using MenuItem = System.Windows.Controls.MenuItem;
  20. using UserControl = System.Windows.Controls.UserControl;
  21. namespace Compdfkit_Tools.PDFView
  22. {
  23. public partial class RegularViewerControl : UserControl, INotifyPropertyChanged
  24. {
  25. public PDFViewControl PdfViewControl;
  26. public CPDFAnnotationControl PDFAnnotationControl = new CPDFAnnotationControl();
  27. private SignatureStatusBarControl signatureStatusBarControl;
  28. private CPDFDisplaySettingsControl displaySettingsControl = null;
  29. private PanelState panelState = PanelState.GetInstance();
  30. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. public event EventHandler<bool> OnCanSaveChanged;
  33. private bool CanSave
  34. {
  35. get
  36. {
  37. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  38. {
  39. if (PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  40. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  41. {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. }
  48. public RegularViewerControl()
  49. {
  50. InitializeComponent();
  51. panelState.PropertyChanged -= PanelState_PropertyChanged;
  52. panelState.PropertyChanged += PanelState_PropertyChanged;
  53. }
  54. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  55. {
  56. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  57. {
  58. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  59. }
  60. else if (e.PropertyName == nameof(PanelState.RightPanel))
  61. {
  62. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  63. {
  64. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  65. }
  66. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  67. {
  68. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  69. }
  70. else
  71. {
  72. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  73. }
  74. }
  75. }
  76. public void ExpandLeftPanel(bool isExpand)
  77. {
  78. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  79. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  80. if (isExpand)
  81. {
  82. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  83. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  84. }
  85. else
  86. {
  87. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  88. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  89. }
  90. }
  91. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  92. {
  93. PropertyContainer.Width = 260;
  94. PropertyContainer.Child = propertytPanel;
  95. PropertyContainer.Visibility = visible;
  96. }
  97. #region Init PDFViewer
  98. private void InitialControl()
  99. {
  100. PdfViewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Pan);
  101. //PdfViewControl.PDFView?.SetShowLink(true);
  102. PDFGrid.Child = PdfViewControl;
  103. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  104. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  105. //PdfViewControl.PDFView.SetFormFieldHighlight(true);
  106. }
  107. private void PdfViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
  108. {
  109. ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
  110. if (ContextMenu == null)
  111. {
  112. ContextMenu = new ContextMenu();
  113. }
  114. switch (e.hitTestType)
  115. {
  116. case MouseHitTestType.Annot:
  117. case MouseHitTestType.SelectRect:
  118. break;
  119. case MouseHitTestType.Text:
  120. CreateSelectTextContextMenu(sender, ref ContextMenu);
  121. break;
  122. case MouseHitTestType.ImageSelect:
  123. CreateSelectImageContextMenu(sender, ref ContextMenu);
  124. break;
  125. default:
  126. PdfViewControl.CreateViewerMenu(sender, ref ContextMenu);
  127. break;
  128. }
  129. PdfViewControl.SetRightMenu(ContextMenu);
  130. }
  131. private void CreateSelectImageContextMenu(object sender, ref ContextMenu menu)
  132. {
  133. if (menu == null)
  134. {
  135. menu = new ContextMenu();
  136. }
  137. MenuItem copyImage = new MenuItem();
  138. copyImage.Header = "Copy Image";
  139. copyImage.Click += CopyImage_Click;
  140. menu.Items.Add(copyImage);
  141. MenuItem extractImage = new MenuItem();
  142. extractImage.Header = "Extract Image";
  143. extractImage.Click += ExtractImage_Click;
  144. menu.Items.Add(extractImage);
  145. }
  146. private void CreateSelectTextContextMenu(object sender, ref ContextMenu menu)
  147. {
  148. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  149. }
  150. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  151. {
  152. PdfViewControl = pdfViewer;
  153. PDFGrid.Child = PdfViewControl;
  154. FloatPageTool.InitWithPDFViewer(pdfViewer);
  155. InitialControl();
  156. DataContext = this;
  157. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  158. {
  159. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  160. //PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  161. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  162. //PdfViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  163. }
  164. }
  165. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  166. //{
  167. // if ((e is WidgetSignArgs args))
  168. // {
  169. // var signatureWidget = args.Sign;
  170. // if(signatureWidget != null)
  171. // {
  172. // CPDFSignature sig = signatureWidget.GetSignature(PdfViewControl.PDFView.Document);
  173. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  174. // {
  175. // return;
  176. // }
  177. // }
  178. // if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  179. // {
  180. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  181. // CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  182. // signatureProperty.SetFormProperty(args, PdfViewControl.PDFView);
  183. // PropertyContainer.Child = signatureProperty;
  184. // }
  185. // }
  186. //}
  187. public void CancelWidgetClickHandler()
  188. {
  189. //if (PdfViewControl != null && PdfViewControl.PDFView != null)
  190. //{
  191. // PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  192. //}
  193. }
  194. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  195. {
  196. this.BotaContainer.Child = botaControl;
  197. }
  198. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  199. {
  200. this.displaySettingsControl = displaySettingsControl;
  201. }
  202. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  203. {
  204. this.signatureStatusBarControl = signatureStatusBarControl;
  205. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  206. if (signatureStatusBarControl.Status != SignatureStatus.None)
  207. {
  208. SignatureStatusBorder.Visibility = Visibility.Visible;
  209. }
  210. else
  211. {
  212. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  213. }
  214. }
  215. #endregion
  216. public void ClearViewerControl()
  217. {
  218. PDFGrid.Child = null;
  219. BotaContainer.Child = null;
  220. PropertyContainer.Child = null;
  221. SignatureStatusBorder.Child = null;
  222. displaySettingsControl = null;
  223. }
  224. #region PropertyChanged
  225. /// <summary>
  226. /// Undo Redo Event Noitfy
  227. /// </summary>
  228. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  229. {
  230. OnPropertyChanged(e.PropertyName);
  231. if (e.PropertyName == "CanSave")
  232. {
  233. OnCanSaveChanged?.Invoke(this, CanSave);
  234. }
  235. }
  236. protected void OnPropertyChanged([CallerMemberName] string name = null)
  237. {
  238. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  239. }
  240. #endregion
  241. #region Context Menu
  242. private void ExtractImage_Click(object sender, RoutedEventArgs e)
  243. {
  244. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  245. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  246. {
  247. var image = PdfViewControl.FocusPDFViewTool.GetSelectImage();
  248. if(image == null)
  249. {
  250. return;
  251. }
  252. CPDFPage page = PdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
  253. string savePath = Path.Combine(folderDialog.SelectedPath, Guid.NewGuid() + ".jpg");
  254. string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
  255. page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
  256. Bitmap bitmap = new Bitmap(tempPath);
  257. bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  258. Process.Start("explorer", "/select,\"" + savePath + "\"");
  259. }
  260. }
  261. private void CopyImage_Click(object sender, RoutedEventArgs e)
  262. {
  263. var image = PdfViewControl.FocusPDFViewTool.GetSelectImage();
  264. if(image == null)
  265. {
  266. return;
  267. }
  268. CPDFPage page = PdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
  269. string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
  270. page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
  271. Bitmap bitmap = new Bitmap(tempPath);
  272. BitmapImage imageData;
  273. using (MemoryStream ms = new MemoryStream())
  274. {
  275. bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  276. imageData = new BitmapImage();
  277. imageData.BeginInit();
  278. imageData.StreamSource = ms;
  279. imageData.CacheOption = BitmapCacheOption.OnLoad;
  280. imageData.EndInit();
  281. imageData.Freeze();
  282. Clipboard.SetImage(imageData);
  283. bitmap.Dispose();
  284. File.Delete(tempPath);
  285. }
  286. }
  287. //private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  288. //{
  289. // if (e != null && e.CommandType == CommandType.Context)
  290. // {
  291. // if (e.PressOnSelectedText)
  292. // {
  293. // e.Handle = true;
  294. // e.PopupMenu = new ContextMenu();
  295. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  296. // }
  297. // else if (e.CommandTarget == TargetType.ImageSelection)
  298. // {
  299. // if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
  300. // {
  301. // e.Handle = true;
  302. // e.PopupMenu = new ContextMenu();
  303. // MenuItem imageCopyMenu = new MenuItem();
  304. // imageCopyMenu = new MenuItem();
  305. // imageCopyMenu.Header = "Copy Images";
  306. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  307. // imageCopyMenu.CommandParameter = e;
  308. // e.PopupMenu.Items.Add(imageCopyMenu);
  309. // MenuItem imageExtraMenu = new MenuItem();
  310. // imageExtraMenu = new MenuItem();
  311. // imageExtraMenu.Header = "Extract Images";
  312. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  313. // imageExtraMenu.CommandParameter = e;
  314. // e.PopupMenu.Items.Add(imageExtraMenu);
  315. // }
  316. // }
  317. // else
  318. // {
  319. // e.Handle = true;
  320. // e.PopupMenu = new ContextMenu();
  321. // //if (PdfViewControl.CheckHasForm())
  322. // MenuItem fitWidthMenu = new MenuItem();
  323. // fitWidthMenu.Header = "Automatically Resize";
  324. // fitWidthMenu.Click += (o, p) =>
  325. // {
  326. // if (PdfViewControl != null)
  327. // {
  328. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  329. // }
  330. // };
  331. // e.PopupMenu.Items.Add(fitWidthMenu);
  332. // MenuItem fitSizeMenu = new MenuItem();
  333. // fitSizeMenu.Header = "Actual Size";
  334. // fitSizeMenu.Click += (o, p) =>
  335. // {
  336. // if (PdfViewControl != null)
  337. // {
  338. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  339. // }
  340. // };
  341. // e.PopupMenu.Items.Add(fitSizeMenu);
  342. // MenuItem zoomInMenu = new MenuItem();
  343. // zoomInMenu.Header = "Zoom In";
  344. // zoomInMenu.Click += (o, p) =>
  345. // {
  346. // if (PdfViewControl != null)
  347. // {
  348. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  349. // PdfViewControl.PDFView?.Zoom(newZoom);
  350. // }
  351. // };
  352. // e.PopupMenu.Items.Add(zoomInMenu);
  353. // MenuItem zoomOutMenu = new MenuItem();
  354. // zoomOutMenu.Header = "Zoom Out";
  355. // zoomOutMenu.Click += (o, p) =>
  356. // {
  357. // if (PdfViewControl != null)
  358. // {
  359. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  360. // PdfViewControl.PDFView?.Zoom(newZoom);
  361. // }
  362. // };
  363. // e.PopupMenu.Items.Add(zoomOutMenu);
  364. // e.PopupMenu.Items.Add(new Separator());
  365. // MenuItem singleView = new MenuItem();
  366. // singleView.Header = "Single Page";
  367. // singleView.Click += (o, p) =>
  368. // {
  369. // if (PdfViewControl != null)
  370. // {
  371. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  372. // }
  373. // };
  374. // e.PopupMenu.Items.Add(singleView);
  375. // MenuItem singleContinuousView = new MenuItem();
  376. // singleContinuousView.Header = "Single Page Continuous";
  377. // singleContinuousView.Click += (o, p) =>
  378. // {
  379. // if (PdfViewControl != null)
  380. // {
  381. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  382. // }
  383. // };
  384. // e.PopupMenu.Items.Add(singleContinuousView);
  385. // MenuItem doubleView = new MenuItem();
  386. // doubleView.Header = "Two Pages";
  387. // doubleView.Click += (o, p) =>
  388. // {
  389. // if (PdfViewControl != null)
  390. // {
  391. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  392. // }
  393. // };
  394. // e.PopupMenu.Items.Add(doubleView);
  395. // MenuItem doubleContinuousView = new MenuItem();
  396. // doubleContinuousView.Header = "Two Pages Continuous";
  397. // doubleContinuousView.Click += (o, p) =>
  398. // {
  399. // if (PdfViewControl != null)
  400. // {
  401. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  402. // }
  403. // };
  404. // e.PopupMenu.Items.Add(doubleContinuousView);
  405. // {
  406. // MenuItem resetForms = new MenuItem();
  407. // resetForms.Header = "Reset Forms";
  408. // resetForms.Click += (o, p) =>
  409. // {
  410. // if (PdfViewControl != null)
  411. // {
  412. // PdfViewControl.PDFView?.ResetForm(null);
  413. // }
  414. // };
  415. // e.PopupMenu.Items.Add(new Separator());
  416. // e.PopupMenu.Items.Add(resetForms);
  417. // }
  418. // }
  419. // }
  420. // if (e != null && e.CommandType == CommandType.Copy)
  421. // {
  422. // e.DoCommand();
  423. // }
  424. //}
  425. #endregion
  426. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  427. {
  428. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  429. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  430. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  431. }
  432. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  433. {
  434. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  435. PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler;
  436. }
  437. }
  438. }