CPDFPageEditBarControl.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using ComPDFKitViewer.PdfViewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace Compdfkit_Tools.PDFControl
  20. {
  21. public partial class CPDFPageEditBarControl : UserControl, INotifyPropertyChanged
  22. {
  23. #region Data
  24. Dictionary<string, string> ButtonDict = new Dictionary<string, string>
  25. {
  26. {"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"},
  27. {"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"},
  28. {"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"},
  29. {"Separator", "90" },
  30. // {"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"},
  31. {"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"},
  32. {"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"},
  33. {"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"},
  34. };
  35. public event PropertyChangedEventHandler PropertyChanged;
  36. protected void OnPropertyChanged([CallerMemberName] string name = null)
  37. {
  38. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  39. }
  40. private CPDFViewer pdfViewer;
  41. public event EventHandler<string> PageEditEvent;
  42. #endregion
  43. #region Create Dafault UI
  44. public CPDFPageEditBarControl()
  45. {
  46. InitializeComponent();
  47. CreateButton();
  48. }
  49. private void CreateButton()
  50. {
  51. foreach (KeyValuePair<string, string> data in ButtonDict)
  52. {
  53. string Path = data.Value;
  54. string name = data.Key;
  55. if (string.Equals(name, "Separator"))
  56. {
  57. CreateSeparator(Convert.ToDouble(Path));
  58. }
  59. else
  60. {
  61. Geometry annotationGeometry = Geometry.Parse(Path);
  62. Path path = new Path
  63. {
  64. Width = 20,
  65. Height = 20,
  66. Data = annotationGeometry,
  67. Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D))
  68. };
  69. CreateButtonForPath(path, name);
  70. }
  71. }
  72. }
  73. private void CreateButtonForPath(Path path, String name)
  74. {
  75. StackPanel stackPanel = new StackPanel();
  76. TextBlock textBlock = new TextBlock();
  77. if (path != null)
  78. {
  79. stackPanel.Orientation = Orientation.Horizontal;
  80. stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
  81. stackPanel.Children.Add(path);
  82. }
  83. if (!string.IsNullOrEmpty(name))
  84. {
  85. textBlock.Text = name;
  86. textBlock.VerticalAlignment = VerticalAlignment.Center;
  87. textBlock.Margin = new Thickness(8, 0, 0, 0);
  88. textBlock.FontSize = 12;
  89. stackPanel.Children.Add(textBlock);
  90. }
  91. Style style = (Style)FindResource("RoundMarginToggleButtonStyle");
  92. ToggleButton button = new ToggleButton();
  93. button.BorderThickness = new Thickness(0);
  94. button.Padding = new Thickness(10, 5, 10, 5);
  95. button.Tag = name;
  96. button.ToolTip = name;
  97. button.Style = style;
  98. button.Content = stackPanel;
  99. button.Click += PageEditBtn_Click;
  100. FormGrid.Children.Add(button);
  101. }
  102. private void PageEditBtn_Click(object sender, RoutedEventArgs e)
  103. {
  104. var button = sender as ToggleButton;
  105. if (button != null)
  106. {
  107. PageEditEvent?.Invoke(sender, button.Tag.ToString());
  108. }
  109. ClearToolState();
  110. }
  111. private void CreateSeparator(double rotate)
  112. {
  113. RotateTransform rotateTransform = new RotateTransform();
  114. rotateTransform.Angle = rotate;
  115. Separator separator = new Separator();
  116. separator.Height = 12;
  117. separator.Width = 20;
  118. separator.HorizontalAlignment = HorizontalAlignment.Center;
  119. separator.VerticalAlignment = VerticalAlignment.Center;
  120. separator.LayoutTransform = rotateTransform;
  121. FormGrid.Children.Add(separator);
  122. }
  123. private void ClearToolState()
  124. {
  125. foreach (UIElement child in FormGrid.Children)
  126. {
  127. if (child is ToggleButton toggle)
  128. {
  129. toggle.IsChecked = false;
  130. }
  131. }
  132. }
  133. #endregion
  134. }
  135. }