RegularViewerControl.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. public void SetPropertyContainer(UIElement uiElement)
  55. {
  56. PropertyContainer.Child = uiElement;
  57. }
  58. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  59. {
  60. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  61. {
  62. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  63. }
  64. else if (e.PropertyName == nameof(PanelState.RightPanel))
  65. {
  66. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  67. {
  68. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  69. }
  70. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  71. {
  72. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  73. }
  74. else
  75. {
  76. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  77. }
  78. }
  79. }
  80. public void ExpandLeftPanel(bool isExpand)
  81. {
  82. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  83. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  84. if (isExpand)
  85. {
  86. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  87. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  88. }
  89. else
  90. {
  91. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  92. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  93. }
  94. }
  95. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  96. {
  97. PropertyContainer.Width = 260;
  98. PropertyContainer.Child = propertytPanel;
  99. PropertyContainer.Visibility = visible;
  100. }
  101. #region Init PDFViewer
  102. private void InitialControl()
  103. {
  104. PDFGrid.Child = PdfViewControl;
  105. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  106. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  107. }
  108. private void PdfViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
  109. {
  110. ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
  111. if (ContextMenu == null)
  112. {
  113. ContextMenu = new ContextMenu();
  114. }
  115. switch (e.hitTestType)
  116. {
  117. case MouseHitTestType.Annot:
  118. case MouseHitTestType.SelectRect:
  119. break;
  120. case MouseHitTestType.Text:
  121. CreateSelectTextContextMenu(sender, ref ContextMenu);
  122. break;
  123. case MouseHitTestType.ImageSelect:
  124. CreateSelectImageContextMenu(sender, ref ContextMenu);
  125. break;
  126. default:
  127. PdfViewControl.CreateViewerMenu(sender, ref ContextMenu);
  128. break;
  129. }
  130. PdfViewControl.SetRightMenu(ContextMenu);
  131. }
  132. private void CreateSelectImageContextMenu(object sender, ref ContextMenu menu)
  133. {
  134. if (menu == null)
  135. {
  136. menu = new ContextMenu();
  137. }
  138. MenuItem copyImage = new MenuItem();
  139. copyImage.Header = "Copy Image";
  140. copyImage.Click += CopyImage_Click;
  141. menu.Items.Add(copyImage);
  142. MenuItem extractImage = new MenuItem();
  143. extractImage.Header = "Extract Image";
  144. extractImage.Click += ExtractImage_Click;
  145. menu.Items.Add(extractImage);
  146. }
  147. private void CreateSelectTextContextMenu(object sender, ref ContextMenu menu)
  148. {
  149. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  150. }
  151. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  152. {
  153. PdfViewControl = pdfViewer;
  154. PDFGrid.Child = PdfViewControl;
  155. FloatPageTool.InitWithPDFViewer(pdfViewer);
  156. InitialControl();
  157. DataContext = this;
  158. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  159. {
  160. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  161. PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler;
  162. }
  163. }
  164. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  165. {
  166. this.BotaContainer.Child = botaControl;
  167. }
  168. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  169. {
  170. this.displaySettingsControl = displaySettingsControl;
  171. }
  172. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  173. {
  174. this.signatureStatusBarControl = signatureStatusBarControl;
  175. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  176. if (signatureStatusBarControl.Status != SignatureStatus.None)
  177. {
  178. SignatureStatusBorder.Visibility = Visibility.Visible;
  179. }
  180. else
  181. {
  182. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  183. }
  184. }
  185. #endregion
  186. public void ClearViewerControl()
  187. {
  188. PDFGrid.Child = null;
  189. BotaContainer.Child = null;
  190. PropertyContainer.Child = null;
  191. SignatureStatusBorder.Child = null;
  192. displaySettingsControl = null;
  193. }
  194. #region PropertyChanged
  195. /// <summary>
  196. /// Undo Redo Event Noitfy
  197. /// </summary>
  198. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  199. {
  200. OnPropertyChanged(e.PropertyName);
  201. if (e.PropertyName == "CanSave")
  202. {
  203. OnCanSaveChanged?.Invoke(this, CanSave);
  204. }
  205. }
  206. protected void OnPropertyChanged([CallerMemberName] string name = null)
  207. {
  208. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  209. }
  210. #endregion
  211. #region Context Menu
  212. private void ExtractImage_Click(object sender, RoutedEventArgs e)
  213. {
  214. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  215. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  216. {
  217. var image = PdfViewControl.FocusPDFViewTool.GetSelectImage();
  218. if(image == null)
  219. {
  220. return;
  221. }
  222. CPDFPage page = PdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
  223. string savePath = Path.Combine(folderDialog.SelectedPath, Guid.NewGuid() + ".jpg");
  224. string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
  225. page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
  226. Bitmap bitmap = new Bitmap(tempPath);
  227. bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  228. Process.Start("explorer", "/select,\"" + savePath + "\"");
  229. }
  230. }
  231. private void CopyImage_Click(object sender, RoutedEventArgs e)
  232. {
  233. var image = PdfViewControl.FocusPDFViewTool.GetSelectImage();
  234. if(image == null)
  235. {
  236. return;
  237. }
  238. CPDFPage page = PdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
  239. string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
  240. page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
  241. Bitmap bitmap = new Bitmap(tempPath);
  242. BitmapImage imageData;
  243. using (MemoryStream ms = new MemoryStream())
  244. {
  245. bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  246. imageData = new BitmapImage();
  247. imageData.BeginInit();
  248. imageData.StreamSource = ms;
  249. imageData.CacheOption = BitmapCacheOption.OnLoad;
  250. imageData.EndInit();
  251. imageData.Freeze();
  252. Clipboard.SetImage(imageData);
  253. bitmap.Dispose();
  254. File.Delete(tempPath);
  255. }
  256. }
  257. #endregion
  258. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  259. {
  260. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  261. }
  262. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  263. {
  264. }
  265. }
  266. }