CPDFPageEditBarControl.xaml.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Media;
  9. using System.Windows.Shapes;
  10. using ComPDFKit.Controls.Helper;
  11. namespace ComPDFKit.Controls.PDFControl
  12. {
  13. public partial class CPDFPageEditBarControl : UserControl, INotifyPropertyChanged
  14. {
  15. #region Data
  16. private string insert = LanguageHelper.ToolBarManager.GetString("Button_Insert");
  17. private string replace = LanguageHelper.ToolBarManager.GetString("Button_Replace");
  18. private string extract = LanguageHelper.ToolBarManager.GetString("Button_Extract");
  19. private string reverse = LanguageHelper.ToolBarManager.GetString("Button_Reverse");
  20. private string copy = LanguageHelper.ToolBarManager.GetString("Button_Copy");
  21. private string rotate = LanguageHelper.ToolBarManager.GetString("Button_Rotate");
  22. private string delete = LanguageHelper.ToolBarManager.GetString("Button_Delete");
  23. Dictionary<string, string> ButtonDict = new Dictionary<string, string>
  24. {
  25. {"Insert","M0.25 0.25H11.3107L15.75 4.68934V10H14.25V5.311L10.689 1.75H1.75V16.25H8V17.75H0.25V0.25ZM12.75 17H11.25V14.75H9V13.25H11.25V11H12.75V13.25H15V14.75H12.75V17Z"},
  26. {"Replace", "M0 0H11.0607L15.5 4.43934V9.75H14V5.061L10.439 1.5H1.5V16H7.75V17.5H0V0ZM12.1366 11.7474L13.1726 10.75L16.75 13.75H8.75V12.3857H12.8974L12.1366 11.7474ZM12.3274 18.25L13.3634 17.2526L12.6026 16.6143H16.75V15.25H8.75L12.3274 18.25Z"},
  27. {"Extract","M0.25 0.25H11.3107L15.75 4.68934V10H14.25V5.311L10.689 1.75H1.75V16.25H8V17.75H0.25V0.25ZM14.0174 15.25L13.1768 16.091L14.2374 17.1516L16.8891 14.5L14.2374 11.8483L13.1768 12.909L14.0174 13.75H9V15.25H14.0174Z"},
  28. {"Separator", "90" },
  29. // {"Reverse","M9.90193 0.75H0.25V17.25H13.75V4.95882L9.90193 0.75ZM9.24 2.25L12.25 5.542V15.75H1.75V2.25H9.24ZM18.624 5.83397L15.25 0.772918V17.25H16.75V5.729L17.376 6.66603L18.624 5.83397Z"},
  30. {"Copy","M3.75 0.75H13.3866L17.25 4.69386V16.25H14.25V19.25H0.75V3.75H3.75V0.75ZM3.75 16.25H12.75V17.75H2.25V5.25H3.75V16.25ZM15.75 5.306L12.756 2.25H5.25V14.75H15.75V5.306ZM13.3428 9.77832C13.165 11.1685 12.0986 12.1333 10.3911 12.1333C8.37891 12.1333 7.14746 10.7749 7.14746 8.52148C7.14746 6.30615 8.37256 4.9541 10.3848 4.9541C12.1177 4.9541 13.165 5.96338 13.3428 7.34717H12.0161C11.8574 6.67432 11.2988 6.17285 10.3848 6.17285C9.24219 6.17285 8.54395 7.04883 8.54395 8.52148C8.54395 10.0195 9.24854 10.9146 10.3848 10.9146C11.248 10.9146 11.8384 10.521 12.0161 9.77832H13.3428Z"},
  31. {"Rotate","M9 8.75V0.75H0.5V8.75V9.5V17.25H17V8.75H9ZM7.5 8.75V2.25H2V8.75H7.5ZM15.5 10.25V15.75H1.999V10.25H15.5ZM17.4623 4.46967L18.523 5.53033L15.8713 8.18198L13.2197 5.53033L14.2803 4.46967L15.1944 5.38355C15.1061 4.03216 14.1724 2.82648 12.7916 2.4565C12.0174 2.24905 11.214 2.33465 10.5138 2.68044L10.3255 2.78117L9.57546 1.48213C10.6588 0.856654 11.9471 0.677284 13.1798 1.00761C15.1834 1.54445 16.5429 3.28512 16.6886 5.24287L17.4623 4.46967Z"},
  32. {"Delete", "M12 1.75V0.25H6V1.75H12ZM18 4.75V3.25H15.75H2.25H0V4.75H2.25V17.75H15.75V4.75H18ZM3.75 16.25V4.75H14.25V16.25H3.75ZM7.75 7.5V13.5H6.25V7.5H7.75ZM11.75 7.5V13.5H10.25V7.5H11.75Z"},
  33. };
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. protected void OnPropertyChanged([CallerMemberName] string name = null)
  36. {
  37. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  38. }
  39. public event EventHandler<string> PageEditEvent;
  40. #endregion
  41. #region Create Dafault UI
  42. public CPDFPageEditBarControl()
  43. {
  44. InitializeComponent();
  45. CreateButton();
  46. }
  47. private void CreateButton()
  48. {
  49. foreach (KeyValuePair<string, string> data in ButtonDict)
  50. {
  51. string Path = data.Value;
  52. string name = data.Key;
  53. if (string.Equals(name, "Separator"))
  54. {
  55. CreateSeparator(Convert.ToDouble(Path));
  56. }
  57. else
  58. {
  59. Geometry annotationGeometry = Geometry.Parse(Path);
  60. Path path = new Path
  61. {
  62. Width = 20,
  63. Height = 20,
  64. Data = annotationGeometry,
  65. Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D))
  66. };
  67. CreateButtonForPath(path, name);
  68. }
  69. }
  70. }
  71. private void CreateButtonForPath(Path path, String name)
  72. {
  73. StackPanel stackPanel = new StackPanel();
  74. TextBlock textBlock = new TextBlock();
  75. if (path != null)
  76. {
  77. stackPanel.Orientation = Orientation.Horizontal;
  78. stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
  79. stackPanel.Children.Add(path);
  80. }
  81. if (!string.IsNullOrEmpty(name))
  82. {
  83. textBlock.Text = GetToolString(name);
  84. textBlock.VerticalAlignment = VerticalAlignment.Center;
  85. textBlock.Margin = new Thickness(8, 0, 0, 0);
  86. textBlock.FontSize = 12;
  87. stackPanel.Children.Add(textBlock);
  88. }
  89. Style style = (Style)FindResource("RoundMarginToggleButtonStyle");
  90. ToggleButton button = new ToggleButton();
  91. button.BorderThickness = new Thickness(0);
  92. button.Padding = new Thickness(10, 5, 10, 5);
  93. button.Tag = name;
  94. button.ToolTip = GetToolString(name);
  95. button.Style = style;
  96. button.Content = stackPanel;
  97. button.Click -= PageEditBtn_Click;
  98. button.Click += PageEditBtn_Click;
  99. FormGrid.Children.Add(button);
  100. }
  101. private string GetToolString(string name)
  102. {
  103. string tooltip = string.Empty;
  104. switch (name)
  105. {
  106. case "Insert":
  107. tooltip = insert;
  108. break;
  109. case "Replace":
  110. tooltip = replace;
  111. break;
  112. case "Extract":
  113. tooltip = extract;
  114. break;
  115. case "Reverse":
  116. tooltip = reverse;
  117. break;
  118. case "Copy":
  119. tooltip = copy;
  120. break;
  121. case "Rotate":
  122. tooltip = rotate;
  123. break;
  124. case "Delete":
  125. tooltip = delete;
  126. break;
  127. default:
  128. break;
  129. }
  130. return tooltip;
  131. }
  132. private void PageEditBtn_Click(object sender, RoutedEventArgs e)
  133. {
  134. var button = sender as ToggleButton;
  135. if (button != null)
  136. {
  137. PageEditEvent?.Invoke(sender, button.Tag.ToString());
  138. }
  139. ClearToolState();
  140. }
  141. private void CreateSeparator(double rotate)
  142. {
  143. RotateTransform rotateTransform = new RotateTransform();
  144. rotateTransform.Angle = rotate;
  145. Separator separator = new Separator();
  146. separator.Height = 12;
  147. separator.Width = 20;
  148. separator.HorizontalAlignment = HorizontalAlignment.Center;
  149. separator.VerticalAlignment = VerticalAlignment.Center;
  150. separator.LayoutTransform = rotateTransform;
  151. FormGrid.Children.Add(separator);
  152. }
  153. private void ClearToolState()
  154. {
  155. foreach (UIElement child in FormGrid.Children)
  156. {
  157. if (child is ToggleButton toggle)
  158. {
  159. toggle.IsChecked = false;
  160. }
  161. }
  162. }
  163. #endregion
  164. }
  165. }