CPDFMeasureBarControl.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.Tool;
  4. using ComPDFKit.Controls.Helper;
  5. using ComPDFKit.Controls.Measure;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Media;
  12. using System.Windows.Shapes;
  13. using static ComPDFKit.Controls.Helper.PanelState;
  14. namespace ComPDFKit.Controls.PDFControl
  15. {
  16. public partial class CPDFMeasureBarControl : UserControl
  17. {
  18. #region Data
  19. private bool isFirstLoad = true;
  20. private static string line = LanguageHelper.ToolBarManager.GetString("Button_Line");
  21. private static string multiline = LanguageHelper.ToolBarManager.GetString("Button_Multiline");
  22. private static string polygonal = LanguageHelper.ToolBarManager.GetString("Button_Polygonal");
  23. private static string rectangles = LanguageHelper.ToolBarManager.GetString("Button_Rectangle");
  24. readonly Dictionary<string, string> buttonDict = new Dictionary<string, string>
  25. {
  26. {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"},
  27. {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"},
  28. {polygonal,"M1.87111 7.6963L10.5 1.42705L19.1289 7.6963L15.8329 17.8402H5.16705L1.87111 7.6963Z"},
  29. {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"}
  30. };
  31. private PDFViewControl pdfViewer;
  32. private MeasurePropertyControl measurePropertyControl = null;
  33. private MeasureControl measureControl = null;
  34. private enum MeasureType
  35. {
  36. UnKnown = -1,
  37. Line,
  38. Multiline,
  39. Polygonal,
  40. Rectangles
  41. }
  42. private PanelState panelState = PanelState.GetInstance();
  43. private MeasureType StringToType(string type)
  44. {
  45. if (type == line)
  46. return MeasureType.Line;
  47. if (type == multiline)
  48. return MeasureType.Multiline;
  49. if (type == polygonal)
  50. return MeasureType.Polygonal;
  51. if (type == rectangles)
  52. return MeasureType.Rectangles;
  53. return MeasureType.UnKnown;
  54. }
  55. #endregion
  56. #region Create Default UI
  57. public CPDFMeasureBarControl()
  58. {
  59. InitializeComponent();
  60. }
  61. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  62. {
  63. if (isFirstLoad)
  64. {
  65. foreach (KeyValuePair<string, string> data in buttonDict)
  66. {
  67. string Path = data.Value;
  68. string name = data.Key;
  69. Geometry annotationGeometry = Geometry.Parse(Path);
  70. Path path = new Path
  71. {
  72. Width = 20,
  73. Height = 20,
  74. Data = annotationGeometry,
  75. Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D)),
  76. };
  77. if (name == LanguageHelper.ToolBarManager.GetString("Button_Polygonal"))
  78. {
  79. path.Fill = null;
  80. path.Stroke = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D));
  81. path.StrokeThickness = 1.5;
  82. }
  83. else if (name == LanguageHelper.ToolBarManager.GetString("Button_Rectangle"))
  84. {
  85. path.Fill = null;
  86. path.Data = new RectangleGeometry(new Rect(3.25, 2.75, 14.5, 14.5));
  87. path.Stroke = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D));
  88. path.StrokeThickness = 1.5;
  89. }
  90. CreateButtonForPath(path, name);
  91. }
  92. isFirstLoad = false;
  93. }
  94. }
  95. private void CreateButtonForPath(Path path, String name)
  96. {
  97. StackPanel stackPanel = new StackPanel();
  98. TextBlock textBlock = new TextBlock();
  99. if (path != null)
  100. {
  101. stackPanel.Orientation = Orientation.Horizontal;
  102. stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
  103. stackPanel.Children.Add(path);
  104. }
  105. if (!string.IsNullOrEmpty(name))
  106. {
  107. textBlock.Text = name;
  108. textBlock.VerticalAlignment = VerticalAlignment.Center;
  109. textBlock.Margin = new Thickness(8, 0, 0, 0);
  110. textBlock.FontSize = 12;
  111. stackPanel.Children.Add(textBlock);
  112. }
  113. Style style = (Style)FindResource("RoundMarginToggleButtonStyle");
  114. ToggleButton button = new ToggleButton();
  115. button.BorderThickness = new Thickness(0);
  116. button.Padding = new Thickness(10, 5, 10, 5);
  117. button.Tag = name;
  118. button.ToolTip = name;
  119. button.Style = style;
  120. button.Content = stackPanel;
  121. button.Click += MeasureBtn_Click;
  122. MeasureGrid.Children.Add(button);
  123. }
  124. #endregion
  125. #region Even Process
  126. public void InitWithPDFViewer(PDFViewControl pdfViewer, MeasurePropertyControl FromProperty, MeasureControl parentControl)
  127. {
  128. this.pdfViewer = pdfViewer;
  129. measurePropertyControl = FromProperty;
  130. measureControl = parentControl;
  131. }
  132. public void ClearAllToolState()
  133. {
  134. foreach (UIElement child in MeasureGrid.Children)
  135. {
  136. if (child is ToggleButton toggle)
  137. {
  138. toggle.IsChecked = false;
  139. }
  140. }
  141. }
  142. public bool ToolChecked()
  143. {
  144. foreach (UIElement child in MeasureGrid.Children)
  145. {
  146. if (child is ToggleButton toggle)
  147. {
  148. if (toggle.IsChecked==true)
  149. {
  150. return true;
  151. }
  152. }
  153. }
  154. return false;
  155. }
  156. private void ClearToolState(UIElement sender)
  157. {
  158. foreach (UIElement child in MeasureGrid.Children)
  159. {
  160. if (child is ToggleButton toggle && (child as ToggleButton) != (sender as ToggleButton))
  161. {
  162. toggle.IsChecked = false;
  163. }
  164. }
  165. }
  166. private void MeasureBtn_Click(object sender, RoutedEventArgs e)
  167. {
  168. AnnotParam annotParam = null;
  169. ClearToolState(sender as ToggleButton);
  170. if ((bool)(sender as ToggleButton).IsChecked)
  171. {
  172. switch (StringToType((sender as ToggleButton).Tag.ToString()))
  173. {
  174. case MeasureType.UnKnown:
  175. break;
  176. case MeasureType.Line:
  177. annotParam = CreateLine();
  178. break;
  179. case MeasureType.Multiline:
  180. annotParam = CreateMultiline();
  181. break;
  182. case MeasureType.Polygonal:
  183. annotParam = CreatePolygonal();
  184. break;
  185. case MeasureType.Rectangles:
  186. annotParam = CreateRectangles();
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. else
  193. {
  194. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_NONE);
  195. pdfViewer.SetToolType(ToolType.Pan);
  196. measureControl.SetInfoPanelVisble(false, false);
  197. }
  198. if (annotParam != null)
  199. {
  200. panelState.RightPanel = RightPanelState.PropertyPanel;
  201. }
  202. measurePropertyControl.SetPropertyForMeasureCreate(annotParam, null, pdfViewer);
  203. }
  204. #endregion
  205. #region Create Form
  206. private AnnotParam CreateLine()
  207. {
  208. pdfViewer.SetToolType(ToolType.CreateAnnot);
  209. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINE);
  210. LineMeasureParam lineMeasureParam = new LineMeasureParam();
  211. lineMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINE;
  212. lineMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  213. lineMeasureParam.LineWidth = 2;
  214. lineMeasureParam.Transparency = 255;
  215. lineMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  216. lineMeasureParam.FontName = "Arial";
  217. lineMeasureParam.FontSize = 14;
  218. lineMeasureParam.HeadLineType = C_LINE_TYPE.LINETYPE_ARROW;
  219. lineMeasureParam.TailLineType = C_LINE_TYPE.LINETYPE_ARROW;
  220. lineMeasureParam.measureInfo = new CPDFMeasureInfo
  221. {
  222. Unit = CPDFMeasure.CPDF_CM,
  223. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  224. RulerBase = 1,
  225. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  226. RulerTranslate = 1,
  227. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  228. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH,
  229. };
  230. pdfViewer.SetAnnotParam(lineMeasureParam);
  231. //pdfViewer?.ClearSelectAnnots();
  232. //pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  233. //pdfViewer?.SetToolParam(lineMeasureArgs);
  234. measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_DISTANCE_MEASURE);
  235. measureControl.SetInfoPanelVisble(true, false);
  236. var measureSetting = pdfViewer.PDFViewTool.GetMeasureSetting();
  237. measureControl.SetMeasureScale(CPDFMeasureType.CPDF_DISTANCE_MEASURE,
  238. string.Format("{0} {1} = {2} {3}",
  239. measureSetting.RulerBase,
  240. measureSetting.RulerBaseUnit,
  241. measureSetting.RulerTranslate,
  242. measureSetting.RulerTranslateUnit));
  243. return lineMeasureParam;
  244. }
  245. private AnnotParam CreateMultiline()
  246. {
  247. pdfViewer.SetToolType(ToolType.CreateAnnot);
  248. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE);
  249. PolyLineMeasureParam polyLineMeasureParam = new PolyLineMeasureParam();
  250. polyLineMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE;
  251. polyLineMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  252. polyLineMeasureParam.LineWidth = 2;
  253. polyLineMeasureParam.Transparency = 255;
  254. polyLineMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  255. polyLineMeasureParam.FontName = "Arial";
  256. polyLineMeasureParam.FontSize = 14;
  257. polyLineMeasureParam.measureInfo = new CPDFMeasureInfo
  258. {
  259. Unit = CPDFMeasure.CPDF_CM,
  260. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  261. RulerBase = 1,
  262. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  263. RulerTranslate = 1,
  264. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  265. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH,
  266. };
  267. pdfViewer.SetAnnotParam(polyLineMeasureParam);
  268. //PolyLineMeasureArgs polyLineMeasureArgs = new PolyLineMeasureArgs();
  269. //polyLineMeasureArgs.LineColor = Colors.Red;
  270. //polyLineMeasureArgs.LineWidth = 2;
  271. //polyLineMeasureArgs.Transparency = 1;
  272. //polyLineMeasureArgs.FontColor = Colors.Red;
  273. //polyLineMeasureArgs.FontName = "Arial";
  274. //polyLineMeasureArgs.FontSize = 14;
  275. // pdfViewer?.ClearSelectAnnots();
  276. // pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  277. // pdfViewer?.SetToolParam(polyLineMeasureArgs);
  278. measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_PERIMETER_MEASURE);
  279. measureControl.SetInfoPanelVisble(true, false);
  280. var measureSetting = pdfViewer.PDFViewTool.GetMeasureSetting();
  281. measureControl.SetMeasureScale(CPDFMeasureType.CPDF_PERIMETER_MEASURE,
  282. string.Format("{0} {1} = {2} {3}",
  283. measureSetting.RulerBase,
  284. measureSetting.RulerBaseUnit,
  285. measureSetting.RulerTranslate,
  286. measureSetting.RulerTranslateUnit));
  287. return polyLineMeasureParam;
  288. }
  289. private AnnotParam CreatePolygonal()
  290. {
  291. pdfViewer.PDFViewTool.GetDefaultSettingParam().IsCreateSquarePolygonMeasure = false;
  292. pdfViewer.SetToolType(ToolType.CreateAnnot);
  293. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON);
  294. PolygonMeasureParam polygonMeasureParam = new PolygonMeasureParam();
  295. polygonMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON;
  296. polygonMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  297. polygonMeasureParam.LineWidth = 2;
  298. polygonMeasureParam.Transparency = 255;
  299. polygonMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  300. polygonMeasureParam.FontName = "Arial";
  301. polygonMeasureParam.FontSize = 14;
  302. polygonMeasureParam.measureInfo = new CPDFMeasureInfo
  303. {
  304. Unit = CPDFMeasure.CPDF_CM,
  305. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  306. RulerBase = 1,
  307. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  308. RulerTranslate = 1,
  309. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  310. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH | CPDFCaptionType.CPDF_CAPTION_AREA,
  311. };
  312. pdfViewer.SetAnnotParam(polygonMeasureParam);
  313. // PolygonMeasureArgs polygonMeasureArgs = new PolygonMeasureArgs();
  314. // polygonMeasureArgs.LineColor = Colors.Red;
  315. // polygonMeasureArgs.LineWidth = 2;
  316. // polygonMeasureArgs.Transparency = 1;
  317. // polygonMeasureArgs.FontColor = Colors.Red;
  318. // polygonMeasureArgs.FillColor = Colors.Transparent;
  319. // polygonMeasureArgs.FontName = "Arial";
  320. // polygonMeasureArgs.FontSize = 14;
  321. // pdfViewer?.ClearSelectAnnots();
  322. // pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  323. // pdfViewer?.SetToolParam(polygonMeasureArgs);
  324. measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_AREA_MEASURE);
  325. measureControl.SetInfoPanelVisble(true, false);
  326. var measureSetting = pdfViewer.PDFViewTool.GetMeasureSetting();
  327. measureControl.SetMeasureScale(CPDFMeasureType.CPDF_AREA_MEASURE,
  328. string.Format("{0} {1} = {2} {3}",
  329. measureSetting.RulerBase,
  330. measureSetting.RulerBaseUnit,
  331. measureSetting.RulerTranslate,
  332. measureSetting.RulerTranslateUnit));
  333. // return polygonMeasureArgs;
  334. return polygonMeasureParam;
  335. }
  336. private AnnotParam CreateRectangles()
  337. {
  338. pdfViewer.PDFViewTool.GetDefaultSettingParam().IsCreateSquarePolygonMeasure = true;
  339. pdfViewer.SetToolType(ToolType.CreateAnnot);
  340. pdfViewer.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON);
  341. PolygonMeasureParam polygonMeasureParam = new PolygonMeasureParam();
  342. polygonMeasureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON;
  343. polygonMeasureParam.LineColor = new byte[] { 255, 0, 0, };
  344. polygonMeasureParam.LineWidth = 2;
  345. polygonMeasureParam.Transparency = 255;
  346. polygonMeasureParam.FontColor = new byte[] { 255, 0, 0, };
  347. polygonMeasureParam.FontName = "Arial";
  348. polygonMeasureParam.FontSize = 14;
  349. polygonMeasureParam.measureInfo = new CPDFMeasureInfo
  350. {
  351. Unit = CPDFMeasure.CPDF_CM,
  352. Precision = CPDFMeasure.PRECISION_VALUE_TWO,
  353. RulerBase = 1,
  354. RulerBaseUnit = CPDFMeasure.CPDF_CM,
  355. RulerTranslate = 1,
  356. RulerTranslateUnit = CPDFMeasure.CPDF_CM,
  357. CaptionType = CPDFCaptionType.CPDF_CAPTION_LENGTH | CPDFCaptionType.CPDF_CAPTION_AREA,
  358. };
  359. pdfViewer.SetAnnotParam(polygonMeasureParam);
  360. measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_AREA_MEASURE);
  361. // PolygonMeasureArgs rectPolygonMeasureArgs = new PolygonMeasureArgs();
  362. // rectPolygonMeasureArgs.LineColor = Colors.Red;
  363. // rectPolygonMeasureArgs.IsOnlyDrawRect = true;
  364. // rectPolygonMeasureArgs.LineWidth = 2;
  365. // rectPolygonMeasureArgs.Transparency = 1;
  366. // rectPolygonMeasureArgs.FontColor = Colors.Red;
  367. // rectPolygonMeasureArgs.FillColor = Colors.Transparent;
  368. // rectPolygonMeasureArgs.FontName = "Arial";
  369. // rectPolygonMeasureArgs.FontSize = 14;
  370. // pdfViewer?.ClearSelectAnnots();
  371. // pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  372. // pdfViewer?.SetToolParam(rectPolygonMeasureArgs);
  373. // measureControl.SetMeasureInfoType(CPDFMeasureType.CPDF_AREA_MEASURE);
  374. // measureControl.SetInfoPanelVisble(true, false);
  375. // measureControl.SetMeasureScale(CPDFMeasureType.CPDF_AREA_MEASURE,
  376. // string.Format("{0} {1} = {2} {3}",
  377. // MeasureSetting.RulerBase,
  378. // MeasureSetting.RulerBaseUnit,
  379. // MeasureSetting.RulerTranslate,
  380. // MeasureSetting.RulerTranslateUnit));
  381. // return rectPolygonMeasureArgs;
  382. return polygonMeasureParam;
  383. }
  384. #endregion
  385. }
  386. }