RegularViewerControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.Controls.Helper;
  11. using ComPDFKit.Controls.PDFControl;
  12. using ComPDFKit.PDFPage;
  13. using ComPDFKit.Tool;
  14. using ContextMenu = System.Windows.Controls.ContextMenu;
  15. using MenuItem = System.Windows.Controls.MenuItem;
  16. using UserControl = System.Windows.Controls.UserControl;
  17. using System.Collections.Generic;
  18. using ComPDFKit.Tool.DrawTool;
  19. namespace ComPDFKit.Controls.PDFView
  20. {
  21. public partial class RegularViewerControl : UserControl, INotifyPropertyChanged
  22. {
  23. public PDFViewControl PdfViewControl;
  24. public CPDFAnnotationControl PDFAnnotationControl = new CPDFAnnotationControl();
  25. private SignatureStatusBarControl signatureStatusBarControl;
  26. private CPDFDisplaySettingsControl displaySettingsControl = null;
  27. private PanelState panelState = PanelState.GetInstance();
  28. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  29. public event PropertyChangedEventHandler PropertyChanged;
  30. public event EventHandler<bool> OnCanSaveChanged;
  31. private bool CanSave
  32. {
  33. get
  34. {
  35. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  36. {
  37. if (PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  38. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  39. {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. }
  46. public RegularViewerControl()
  47. {
  48. InitializeComponent();
  49. panelState.PropertyChanged -= PanelState_PropertyChanged;
  50. panelState.PropertyChanged += PanelState_PropertyChanged;
  51. }
  52. public void SetPropertyContainer(UIElement uiElement)
  53. {
  54. PropertyContainer.Child = uiElement;
  55. }
  56. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  57. {
  58. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  59. {
  60. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  61. }
  62. else if (e.PropertyName == nameof(PanelState.RightPanel))
  63. {
  64. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  65. {
  66. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  67. }
  68. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  69. {
  70. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  71. }
  72. else
  73. {
  74. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  75. }
  76. }
  77. }
  78. public void ExpandLeftPanel(bool isExpand)
  79. {
  80. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  81. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  82. if (isExpand)
  83. {
  84. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  85. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  86. }
  87. else
  88. {
  89. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  90. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  91. }
  92. }
  93. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  94. {
  95. PropertyContainer.Width = 260;
  96. PropertyContainer.Child = propertytPanel;
  97. PropertyContainer.Visibility = visible;
  98. }
  99. #region Init PDFViewer
  100. private void InitialControl()
  101. {
  102. PDFGrid.Child = PdfViewControl;
  103. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  104. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  105. }
  106. private void PdfViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
  107. {
  108. ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
  109. if (ContextMenu == null)
  110. {
  111. ContextMenu = new ContextMenu();
  112. }
  113. switch (e.hitTestType)
  114. {
  115. case MouseHitTestType.Annot:
  116. case MouseHitTestType.SelectRect:
  117. break;
  118. case MouseHitTestType.Text:
  119. CreateSelectTextContextMenu(sender, ref ContextMenu);
  120. break;
  121. case MouseHitTestType.ImageSelect:
  122. CreateSelectImageContextMenu(sender, ref ContextMenu);
  123. break;
  124. default:
  125. PdfViewControl.CreateViewerMenu(sender, ref ContextMenu);
  126. break;
  127. }
  128. PdfViewControl.SetRightMenu(ContextMenu);
  129. }
  130. private void CreateSelectImageContextMenu(object sender, ref ContextMenu menu)
  131. {
  132. if (menu == null)
  133. {
  134. menu = new ContextMenu();
  135. }
  136. MenuItem copyImage = new MenuItem();
  137. copyImage.Header = "Copy Image";
  138. copyImage.Click += CopyImage_Click;
  139. menu.Items.Add(copyImage);
  140. MenuItem extractImage = new MenuItem();
  141. extractImage.Header = "Extract Image";
  142. extractImage.Click += ExtractImage_Click;
  143. menu.Items.Add(extractImage);
  144. }
  145. private void CreateSelectTextContextMenu(object sender, ref ContextMenu menu)
  146. {
  147. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  148. }
  149. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  150. {
  151. PdfViewControl = pdfViewer;
  152. PDFGrid.Child = PdfViewControl;
  153. FloatPageTool.InitWithPDFViewer(pdfViewer);
  154. InitialControl();
  155. DataContext = this;
  156. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  157. {
  158. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  159. PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler;
  160. }
  161. }
  162. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  163. {
  164. this.BotaContainer.Child = botaControl;
  165. }
  166. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  167. {
  168. this.displaySettingsControl = displaySettingsControl;
  169. }
  170. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  171. {
  172. this.signatureStatusBarControl = signatureStatusBarControl;
  173. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  174. if (signatureStatusBarControl.Status != SignatureStatus.None)
  175. {
  176. SignatureStatusBorder.Visibility = Visibility.Visible;
  177. }
  178. else
  179. {
  180. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  181. }
  182. }
  183. #endregion
  184. public void ClearViewerControl()
  185. {
  186. PDFGrid.Child = null;
  187. BotaContainer.Child = null;
  188. PropertyContainer.Child = null;
  189. SignatureStatusBorder.Child = null;
  190. displaySettingsControl = null;
  191. }
  192. #region PropertyChanged
  193. /// <summary>
  194. /// Undo Redo Event Noitfy
  195. /// </summary>
  196. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  197. {
  198. OnPropertyChanged(e.PropertyName);
  199. if (e.PropertyName == "CanSave")
  200. {
  201. OnCanSaveChanged?.Invoke(this, CanSave);
  202. }
  203. }
  204. protected void OnPropertyChanged([CallerMemberName] string name = null)
  205. {
  206. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  207. }
  208. #endregion
  209. #region Context Menu
  210. private void ExtractImage_Click(object sender, RoutedEventArgs e)
  211. {
  212. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  213. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  214. {
  215. PageImageItem image = null;
  216. Dictionary<int, List<PageImageItem>> pageImageDict = PdfViewControl.FocusPDFViewTool.GetSelectImageItems();
  217. if (pageImageDict != null && pageImageDict.Count > 0)
  218. {
  219. foreach (int pageIndex in pageImageDict.Keys)
  220. {
  221. List<PageImageItem> imageItemList = pageImageDict[pageIndex];
  222. image = imageItemList[0];
  223. break;
  224. }
  225. }
  226. if (image == null)
  227. {
  228. return;
  229. }
  230. CPDFPage page = PdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
  231. string savePath = Path.Combine(folderDialog.SelectedPath, Guid.NewGuid() + ".jpg");
  232. string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
  233. page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
  234. Bitmap bitmap = new Bitmap(tempPath);
  235. bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  236. Process.Start("explorer", "/select,\"" + savePath + "\"");
  237. }
  238. }
  239. private void CopyImage_Click(object sender, RoutedEventArgs e)
  240. {
  241. PageImageItem image = null;
  242. Dictionary<int, List<PageImageItem>> pageImageDict = PdfViewControl.FocusPDFViewTool.GetSelectImageItems();
  243. if (pageImageDict != null && pageImageDict.Count > 0)
  244. {
  245. foreach (int pageIndex in pageImageDict.Keys)
  246. {
  247. List<PageImageItem> imageItemList = pageImageDict[pageIndex];
  248. image = imageItemList[0];
  249. break;
  250. }
  251. }
  252. if (image == null)
  253. {
  254. return;
  255. }
  256. CPDFPage page = PdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
  257. string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
  258. page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
  259. Bitmap bitmap = new Bitmap(tempPath);
  260. BitmapImage imageData;
  261. using (MemoryStream ms = new MemoryStream())
  262. {
  263. bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  264. imageData = new BitmapImage();
  265. imageData.BeginInit();
  266. imageData.StreamSource = ms;
  267. imageData.CacheOption = BitmapCacheOption.OnLoad;
  268. imageData.EndInit();
  269. imageData.Freeze();
  270. Clipboard.SetImage(imageData);
  271. bitmap.Dispose();
  272. File.Delete(tempPath);
  273. }
  274. }
  275. #endregion
  276. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  277. {
  278. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  279. }
  280. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  281. {
  282. }
  283. }
  284. }