CPDFMeasureBarControl.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.Tool;
  5. using Compdfkit_Tools.Helper;
  6. using Compdfkit_Tools.Measure;
  7. using Compdfkit_Tools.Measure.Property;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Runtime.CompilerServices;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Controls.Primitives;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Ink;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. using static Compdfkit_Tools.Helper.PanelState;
  28. namespace Compdfkit_Tools.PDFControl
  29. {
  30. /// <summary>
  31. /// CPDFMeasureBarControl.xaml 的交互逻辑
  32. /// </summary>
  33. public partial class CPDFMeasureBarControl : UserControl
  34. {
  35. #region Data
  36. private bool isFirstLoad = true;
  37. private static string line = LanguageHelper.ToolBarManager.GetString("Button_Line");
  38. private static string multiline = LanguageHelper.ToolBarManager.GetString("Button_Multiline");
  39. private static string polygonal = LanguageHelper.ToolBarManager.GetString("Button_Polygonal");
  40. private static string rectangles = LanguageHelper.ToolBarManager.GetString("Button_Rectangle");
  41. readonly Dictionary<string, string> buttonDict = new Dictionary<string, string>
  42. {
  43. {line,"M18.2782 5.75646L14.7427 2.22093L13.5052 3.45837L15.9802 5.93333L14.9195 6.99399L12.4446 4.51903L10.6768 6.28679L13.1518 8.76175L12.0911 9.82241L9.61615 7.34745L7.84838 9.11522L10.3233 11.5902L9.26268 12.6508L6.78772 10.1759L5.01996 11.9436L7.49492 14.4186L6.43426 15.4793L3.9593 13.0043L2.72186 14.2417L6.25739 17.7773L18.2782 5.75646ZM15.8033 1.16027L14.7427 0.0996094L13.682 1.16027L1.6612 13.1811L0.600539 14.2417L1.6612 15.3024L5.19673 18.8379L6.25739 19.8986L7.31805 18.8379L19.3389 6.81712L20.3995 5.75646L19.3389 4.6958L15.8033 1.16027Z"},
  44. {multiline, "M1 3C1 1.89543 1.89543 1 3 1C3.83934 1 4.55793 1.51704 4.85462 2.25H16.1454C16.4421 1.51704 17.1607 1 18 1C19.1046 1 20 1.89543 20 3C20 3.83934 19.483 4.55793 18.75 4.85462V17V17.75H18H14.2011L15.0167 18.5765L14.0138 19.5928L11.9986 17.5505L11.4972 17.0423L11.9986 16.5342L14.0138 14.4918L15.0167 15.5082L14.2847 16.25H17.25V4.85462C16.7487 4.65168 16.3483 4.25135 16.1454 3.75H4.85462C4.65168 4.25135 4.25135 4.65168 3.75 4.85462V16.25H6.75V15H8.25V19H6.75V17.75H3H2.25V17V4.85462C1.51704 4.55793 1 3.83934 1 3Z"},
  45. {polygonal,"M1.87111 7.6963L10.5 1.42705L19.1289 7.6963L15.8329 17.8402H5.16705L1.87111 7.6963Z"},
  46. {rectangles,"M20.25 2.25H0.75V17.75H20.25V2.25ZM18.75 10.75V16.25H2.25V10.75H18.75ZM18.75 9.25V3.75H2.25V9.25H18.75ZM6.5 5.5H3.5V7.5H6.5V5.5ZM3.5 12.5H6.5V14.5H3.5V12.5Z"}
  47. };
  48. private PDFViewControl pdfViewer;
  49. private MeasurePropertyControl measurePropertyControl = null;
  50. private MeasureControl measureControl = null;
  51. private enum MeasureType
  52. {
  53. UnKnown = -1,
  54. Line,
  55. Multiline,
  56. Polygonal,
  57. Rectangles
  58. }
  59. private PanelState panelState = PanelState.GetInstance();
  60. private MeasureType StringToType(string type)
  61. {
  62. if (type == line)
  63. return MeasureType.Line;
  64. if (type == multiline)
  65. return MeasureType.Multiline;
  66. if (type == polygonal)
  67. return MeasureType.Polygonal;
  68. if (type == rectangles)
  69. return MeasureType.Rectangles;
  70. return MeasureType.UnKnown;
  71. }
  72. #endregion
  73. #region Create Default UI
  74. public CPDFMeasureBarControl()
  75. {
  76. InitializeComponent();
  77. }
  78. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  79. {
  80. if (isFirstLoad)
  81. {
  82. foreach (KeyValuePair<string, string> data in buttonDict)
  83. {
  84. string Path = data.Value;
  85. string name = data.Key;
  86. Geometry annotationGeometry = Geometry.Parse(Path);
  87. Path path = new Path
  88. {
  89. Width = 20,
  90. Height = 20,
  91. Data = annotationGeometry,
  92. Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D)),
  93. };
  94. if (name == LanguageHelper.ToolBarManager.GetString("Button_Polygonal"))
  95. {
  96. path.Fill = null;
  97. path.Stroke = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D));
  98. path.StrokeThickness = 1.5;
  99. }
  100. else if (name == LanguageHelper.ToolBarManager.GetString("Button_Rectangle"))
  101. {
  102. path.Fill = null;
  103. path.Data = new RectangleGeometry(new Rect(3.25, 2.75, 14.5, 14.5));
  104. path.Stroke = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D));
  105. path.StrokeThickness = 1.5;
  106. }
  107. CreateButtonForPath(path, name);
  108. }
  109. isFirstLoad = false;
  110. }
  111. }
  112. private void CreateButtonForPath(Path path, String name)
  113. {
  114. StackPanel stackPanel = new StackPanel();
  115. TextBlock textBlock = new TextBlock();
  116. if (path != null)
  117. {
  118. stackPanel.Orientation = Orientation.Horizontal;
  119. stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
  120. stackPanel.Children.Add(path);
  121. }
  122. if (!string.IsNullOrEmpty(name))
  123. {
  124. textBlock.Text = name;
  125. textBlock.VerticalAlignment = VerticalAlignment.Center;
  126. textBlock.Margin = new Thickness(8, 0, 0, 0);
  127. textBlock.FontSize = 12;
  128. stackPanel.Children.Add(textBlock);
  129. }
  130. Style style = (Style)FindResource("RoundMarginToggleButtonStyle");
  131. ToggleButton button = new ToggleButton();
  132. button.BorderThickness = new Thickness(0);
  133. button.Padding = new Thickness(10, 5, 10, 5);
  134. button.Tag = name;
  135. button.ToolTip = name;
  136. button.Style = style;
  137. button.Content = stackPanel;
  138. button.Click += MeasureBtn_Click;
  139. MeasureGrid.Children.Add(button);
  140. }
  141. #endregion
  142. #region Even Process
  143. public void InitWithPDFViewer(PDFViewControl pdfViewer, MeasurePropertyControl FromProperty, MeasureControl parentControl)
  144. {
  145. this.pdfViewer = pdfViewer;
  146. measurePropertyControl = FromProperty;
  147. measureControl = parentControl;
  148. }
  149. public void ClearAllToolState()
  150. {
  151. foreach (UIElement child in MeasureGrid.Children)
  152. {
  153. if (child is ToggleButton toggle)
  154. {
  155. toggle.IsChecked = false;
  156. }
  157. }
  158. }
  159. public bool ToolChecked()
  160. {
  161. foreach (UIElement child in MeasureGrid.Children)
  162. {
  163. if (child is ToggleButton toggle)
  164. {
  165. if (toggle.IsChecked==true)
  166. {
  167. return true;
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. private void ClearToolState(UIElement sender)
  174. {
  175. foreach (UIElement child in MeasureGrid.Children)
  176. {
  177. if (child is ToggleButton toggle && (child as ToggleButton) != (sender as ToggleButton))
  178. {
  179. toggle.IsChecked = false;
  180. }
  181. }
  182. }
  183. private void MeasureBtn_Click(object sender, RoutedEventArgs e)
  184. {
  185. AnnotParam annotParam = null;
  186. ClearToolState(sender as ToggleButton);
  187. if ((bool)(sender as ToggleButton).IsChecked)
  188. {
  189. switch (StringToType((sender as ToggleButton).Tag.ToString()))
  190. {
  191. case MeasureType.UnKnown:
  192. break;
  193. case MeasureType.Line:
  194. annotParam = CreateLine();
  195. break;
  196. case MeasureType.Multiline:
  197. annotParam = CreateMultiline();
  198. break;
  199. case MeasureType.Polygonal:
  200. annotParam = CreatePolygonal();
  201. break;
  202. case MeasureType.Rectangles:
  203. annotParam = CreateRectangles();
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. else
  210. {
  211. pdfViewer.SetToolType(CPDFToolManager.ToolType.Pan);
  212. measureControl.SetInfoPanelVisble(false, false);
  213. }
  214. if (annotParam != null)
  215. {
  216. panelState.RightPanel = RightPanelState.PropertyPanel;
  217. }
  218. measurePropertyControl.SetPropertyForMeasureCreate(annotParam, null, pdfViewer);
  219. }
  220. #endregion
  221. #region Create Form
  222. private AnnotParam CreateLine()
  223. {
  224. pdfViewer.SetToolType(CPDFToolManager.ToolType.CreateAnnot);
  225. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINE);
  226. LineMeasureParam lineMeasureParam = new LineMeasureParam();
  227. lineMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINE;
  228. lineMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  229. lineMeasureParam.LineWidth = 2;
  230. lineMeasureParam.Transparency = 255;
  231. lineMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  232. lineMeasureParam.FontName = "Arial";
  233. lineMeasureParam.FontSize = 14;
  234. lineMeasureParam.HeadLineType = C_LINE_TYPE.LINETYPE_ARROW;
  235. lineMeasureParam.TailLineType = C_LINE_TYPE.LINETYPE_ARROW;
  236. lineMeasureParam.measureInfo = new CPDFMeasureInfo
  237. {
  238. Unit = CPDFMeasure.CPDF_CM,
  239. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  240. RulerBase = 1,
  241. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  242. RulerTranslate = 1,
  243. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  244. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH,
  245. };
  246. pdfViewer.SetAnnotParam(lineMeasureParam);
  247. //pdfViewer?.ClearSelectAnnots();
  248. //pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  249. //pdfViewer?.SetToolParam(lineMeasureArgs);
  250. measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_DISTANCE_MEASURE);
  251. measureControl.SetInfoPanelVisble(true, false);
  252. var measureSetting = pdfViewer.PDFViewTool.GetMeasureSetting();
  253. measureControl.SetMeasureScale(CPDFMeasureType.CPDF_DISTANCE_MEASURE,
  254. string.Format("{0} {1} = {2} {3}",
  255. measureSetting.RulerBase,
  256. measureSetting.RulerBaseUnit,
  257. measureSetting.RulerTranslate,
  258. measureSetting.RulerTranslateUnit));
  259. return lineMeasureParam;
  260. }
  261. private AnnotParam CreateMultiline()
  262. {
  263. pdfViewer.SetToolType(CPDFToolManager.ToolType.CreateAnnot);
  264. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE);
  265. PolyLineMeasureParam polyLineMeasureParam = new PolyLineMeasureParam();
  266. polyLineMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE;
  267. polyLineMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  268. polyLineMeasureParam.LineWidth = 2;
  269. polyLineMeasureParam.Transparency = 255;
  270. polyLineMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  271. polyLineMeasureParam.FontName = "Arial";
  272. polyLineMeasureParam.FontSize = 14;
  273. polyLineMeasureParam.measureInfo = new CPDFMeasureInfo
  274. {
  275. Unit = CPDFMeasure.CPDF_CM,
  276. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  277. RulerBase = 1,
  278. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  279. RulerTranslate = 1,
  280. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  281. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH,
  282. };
  283. pdfViewer.SetAnnotParam(polyLineMeasureParam);
  284. //PolyLineMeasureArgs polyLineMeasureArgs = new PolyLineMeasureArgs();
  285. //polyLineMeasureArgs.LineColor = Colors.Red;
  286. //polyLineMeasureArgs.LineWidth = 2;
  287. //polyLineMeasureArgs.Transparency = 1;
  288. //polyLineMeasureArgs.FontColor = Colors.Red;
  289. //polyLineMeasureArgs.FontName = "Arial";
  290. //polyLineMeasureArgs.FontSize = 14;
  291. // pdfViewer?.ClearSelectAnnots();
  292. // pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  293. // pdfViewer?.SetToolParam(polyLineMeasureArgs);
  294. measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_PERIMETER_MEASURE);
  295. measureControl.SetInfoPanelVisble(true, false);
  296. var measureSetting = pdfViewer.PDFViewTool.GetMeasureSetting();
  297. measureControl.SetMeasureScale(CPDFMeasureType.CPDF_PERIMETER_MEASURE,
  298. string.Format("{0} {1} = {2} {3}",
  299. measureSetting.RulerBase,
  300. measureSetting.RulerBaseUnit,
  301. measureSetting.RulerTranslate,
  302. measureSetting.RulerTranslateUnit));
  303. return polyLineMeasureParam;
  304. }
  305. private AnnotParam CreatePolygonal()
  306. {
  307. pdfViewer.SetToolType(CPDFToolManager.ToolType.CreateAnnot);
  308. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON);
  309. PolygonMeasureParam polygonMeasureParam = new PolygonMeasureParam();
  310. polygonMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON;
  311. polygonMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  312. polygonMeasureParam.LineWidth = 2;
  313. polygonMeasureParam.Transparency = 255;
  314. polygonMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  315. polygonMeasureParam.FontName = "Arial";
  316. polygonMeasureParam.FontSize = 14;
  317. polygonMeasureParam.measureInfo = new CPDFMeasureInfo
  318. {
  319. Unit = CPDFMeasure.CPDF_CM,
  320. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  321. RulerBase = 1,
  322. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  323. RulerTranslate = 1,
  324. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  325. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH | CPDFCaptionType.CPDF_CAPTION_AREA,
  326. };
  327. pdfViewer.SetAnnotParam(polygonMeasureParam);
  328. // PolygonMeasureArgs polygonMeasureArgs = new PolygonMeasureArgs();
  329. // polygonMeasureArgs.LineColor = Colors.Red;
  330. // polygonMeasureArgs.LineWidth = 2;
  331. // polygonMeasureArgs.Transparency = 1;
  332. // polygonMeasureArgs.FontColor = Colors.Red;
  333. // polygonMeasureArgs.FillColor = Colors.Transparent;
  334. // polygonMeasureArgs.FontName = "Arial";
  335. // polygonMeasureArgs.FontSize = 14;
  336. // pdfViewer?.ClearSelectAnnots();
  337. // pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  338. // pdfViewer?.SetToolParam(polygonMeasureArgs);
  339. measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_AREA_MEASURE);
  340. measureControl.SetInfoPanelVisble(true, false);
  341. var measureSetting = pdfViewer.PDFViewTool.GetMeasureSetting();
  342. measureControl.SetMeasureScale(CPDFMeasureType.CPDF_AREA_MEASURE,
  343. string.Format("{0} {1} = {2} {3}",
  344. measureSetting.RulerBase,
  345. measureSetting.RulerBaseUnit,
  346. measureSetting.RulerTranslate,
  347. measureSetting.RulerTranslateUnit));
  348. // return polygonMeasureArgs;
  349. return polygonMeasureParam;
  350. }
  351. private AnnotParam CreateRectangles()
  352. {
  353. pdfViewer.SetToolType(CPDFToolManager.ToolType.CreateAnnot);
  354. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON);
  355. PolygonMeasureParam polygonMeasureParam = new PolygonMeasureParam();
  356. polygonMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON;
  357. polygonMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  358. polygonMeasureParam.LineWidth = 2;
  359. polygonMeasureParam.Transparency = 255;
  360. polygonMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  361. polygonMeasureParam.FontName = "Arial";
  362. polygonMeasureParam.FontSize = 14;
  363. polygonMeasureParam.measureInfo = new CPDFMeasureInfo
  364. {
  365. Unit = CPDFMeasure.CPDF_CM,
  366. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  367. RulerBase = 1,
  368. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  369. RulerTranslate = 1,
  370. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  371. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH | CPDFCaptionType.CPDF_CAPTION_AREA,
  372. };
  373. pdfViewer.SetAnnotParam(polygonMeasureParam);
  374. // PolygonMeasureArgs rectPolygonMeasureArgs = new PolygonMeasureArgs();
  375. // rectPolygonMeasureArgs.LineColor = Colors.Red;
  376. // rectPolygonMeasureArgs.IsOnlyDrawRect = true;
  377. // rectPolygonMeasureArgs.LineWidth = 2;
  378. // rectPolygonMeasureArgs.Transparency = 1;
  379. // rectPolygonMeasureArgs.FontColor = Colors.Red;
  380. // rectPolygonMeasureArgs.FillColor = Colors.Transparent;
  381. // rectPolygonMeasureArgs.FontName = "Arial";
  382. // rectPolygonMeasureArgs.FontSize = 14;
  383. // pdfViewer?.ClearSelectAnnots();
  384. // pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  385. // pdfViewer?.SetToolParam(rectPolygonMeasureArgs);
  386. // measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_AREA_MEASURE);
  387. // measureControl.SetInfoPanelVisble(true, false);
  388. // measureControl.SetMeasureScale(CPDFMeasureType.CPDF_AREA_MEASURE,
  389. // string.Format("{0} {1} = {2} {3}",
  390. // MeasureSetting.RulerBase,
  391. // MeasureSetting.RulerBaseUnit,
  392. // MeasureSetting.RulerTranslate,
  393. // MeasureSetting.RulerTranslateUnit));
  394. // return rectPolygonMeasureArgs;
  395. return polygonMeasureParam;
  396. }
  397. #endregion
  398. }
  399. }