MeasureControl.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using Compdfkit_Tools.Helper;
  4. using Compdfkit_Tools.PDFControl;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.AnnotEvent;
  7. using ComPDFKitViewer.PdfViewer;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace Compdfkit_Tools.Measure
  25. {
  26. /// <summary>
  27. /// MeasureControl.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class MeasureControl : UserControl
  30. {
  31. public MeasurePropertyControl measurePropertyControl = new MeasurePropertyControl();
  32. private CPDFDisplaySettingsControl displaySettingsControl;
  33. private PDFViewControl PdfViewControl = new PDFViewControl();
  34. private PanelState panelState = PanelState.GetInstance();
  35. public event EventHandler ExpandEvent;
  36. public MeasureControl()
  37. {
  38. InitializeComponent();
  39. MeasureSetting.MeasureChanged += MeasureSetting_MeasureChanged;
  40. }
  41. private void MeasureSetting_MeasureChanged(object sender, MeasureEventArgs e)
  42. {
  43. InfoPanel.SetMeasureType(e.Type);
  44. InfoPanel.SetMeasureInfo(e);
  45. }
  46. #region Init PDFViewer
  47. public void InitWithPDFViewer(PDFViewControl pdfViewControl, CPDFViewer pdfViewer)
  48. {
  49. PdfViewControl = pdfViewControl;
  50. PdfViewControl.PDFView = pdfViewer;
  51. PDFMeasureTool.InitWithPDFViewer(pdfViewer, measurePropertyControl, this);
  52. FloatPageTool.InitWithPDFViewer(pdfViewer);
  53. PDFGrid.Child = PdfViewControl;
  54. panelState.PropertyChanged -= PanelState_PropertyChanged;
  55. panelState.PropertyChanged += PanelState_PropertyChanged;
  56. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  57. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  58. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  59. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  60. SetInfoPanelVisble(false, false);
  61. }
  62. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  63. {
  64. switch (e.CommandType)
  65. {
  66. case CommandType.Context:
  67. if (e.CommandTarget == TargetType.Annot && e.PressOnAnnot)
  68. {
  69. e.Handle = true;
  70. e.PopupMenu = new ContextMenu();
  71. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  72. MenuItem menuItem = new MenuItem();
  73. menuItem.Header = "Measurement Settings";
  74. menuItem.Click += (item, param) =>
  75. {
  76. SettingPanel.UpdateArgsList = e.AnnotEventArgsList;
  77. SettingPanel.BindMeasureSetting();
  78. SetInfoPanelVisble(false, true);
  79. };
  80. MenuItem propertyItem = new MenuItem();
  81. propertyItem.Header = "Properties";
  82. propertyItem.Click += (item, param) =>
  83. {
  84. ExpandEvent?.Invoke(this, new EventArgs());
  85. };
  86. e.PopupMenu.Items.Add(menuItem);
  87. e.PopupMenu.Items.Add(propertyItem);
  88. }
  89. break;
  90. case CommandType.Delete:
  91. e.DoCommand();
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  98. {
  99. if (e == null || e.IsAnnotCreateReset)
  100. {
  101. if (e == null)
  102. {
  103. measurePropertyControl?.ClearMeasurePanel();
  104. if(PDFMeasureTool.ToolChecked()==false)
  105. {
  106. SetInfoPanelVisble(false, false);
  107. }
  108. }
  109. return;
  110. }
  111. else
  112. {
  113. switch (e.GetAnnotTypes())
  114. {
  115. case AnnotArgsType.LineMeasure:
  116. LineMeasureArgs LineArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.LineMeasure).First() as LineMeasureArgs;
  117. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  118. measurePropertyControl.SetPropertyForMeasureCreate(LineArgs, e);
  119. SetInfoPanelVisble(true, false);
  120. SetMeasureInfoPanel(LineArgs.GetPDFAnnot(), LineArgs);
  121. break;
  122. case AnnotArgsType.PolygonMeasure:
  123. PolygonMeasureArgs polygonArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.PolygonMeasure).First() as PolygonMeasureArgs;
  124. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  125. measurePropertyControl.SetPropertyForMeasureCreate(polygonArgs, e);
  126. SetInfoPanelVisble(true, false);
  127. SetMeasureInfoPanel(polygonArgs.GetPDFAnnot(),polygonArgs);
  128. break;
  129. case AnnotArgsType.PolyLineMeasure:
  130. PolyLineMeasureArgs polyLineArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.PolyLineMeasure).First() as PolyLineMeasureArgs;
  131. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  132. measurePropertyControl.SetPropertyForMeasureCreate(polyLineArgs, e);
  133. SetInfoPanelVisble(true, false);
  134. SetMeasureInfoPanel(polyLineArgs.GetPDFAnnot(),polyLineArgs);
  135. break;
  136. }
  137. }
  138. }
  139. private void SetMeasureInfoPanel(CPDFAnnotation rawAnnot,AnnotHandlerEventArgs annotArgs=null)
  140. {
  141. if (rawAnnot == null)
  142. {
  143. return;
  144. }
  145. try
  146. {
  147. if (rawAnnot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  148. {
  149. CPDFLineAnnotation lineAnnot = rawAnnot as CPDFLineAnnotation;
  150. if (lineAnnot.IsMersured() && lineAnnot.Points != null && lineAnnot.Points.Count() == 2)
  151. {
  152. CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
  153. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  154. Vector standVector = new Vector(1, 0);
  155. Point startPoint = new Point(lineAnnot.Points[0].x, lineAnnot.Points[0].y);
  156. Point endPoint = new Point(lineAnnot.Points[1].x, lineAnnot.Points[1].y);
  157. Vector movevector = endPoint - startPoint;
  158. double inch = movevector.Length / 72D;
  159. double rate = measureInfo.RulerTranslate / measureInfo.RulerBase;
  160. double ratio = GetMeasureRatio(measureInfo.RulerBaseUnit);
  161. double showLenght = inch * rate / ratio;
  162. MeasureEventArgs measureEvent = new MeasureEventArgs();
  163. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(movevector, standVector));
  164. measureEvent.RulerTranslateUnit = measureInfo.RulerTranslateUnit;
  165. measureEvent.RulerTranslate = measureInfo.RulerTranslate;
  166. measureEvent.RulerBase = measureInfo.RulerBase;
  167. measureEvent.RulerBaseUnit = measureInfo.RulerBaseUnit;
  168. measureEvent.MousePos = new Point(
  169. (int)Math.Abs(movevector.X),
  170. (int)Math.Abs(movevector.Y));
  171. measureEvent.Type = CPDFMeasureType.CPDF_DISTANCE_MEASURE;
  172. NumberFormatInfo formatInfo = new NumberFormatInfo();
  173. formatInfo.NumberDecimalDigits = Math.Abs(measureInfo.Precision).ToString().Length - 1;
  174. measureEvent.Distance = showLenght.ToString("N", formatInfo) + measureInfo.RulerTranslateUnit;
  175. measureEvent.Precision = GetMeasureShowPrecision(measureInfo.Precision);
  176. MeasureSetting.InvokeMeasureChangeEvent(this, measureEvent);
  177. if(annotArgs!=null)
  178. {
  179. SettingPanel.UpdateArgsList =new List<AnnotHandlerEventArgs> { annotArgs};
  180. }
  181. }
  182. }
  183. if (rawAnnot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE)
  184. {
  185. CPDFPolylineAnnotation polylineAnnot = rawAnnot as CPDFPolylineAnnotation;
  186. if (polylineAnnot.IsMersured() && polylineAnnot.Points != null && polylineAnnot.Points.Count() >= 2)
  187. {
  188. double totalInch = 0;
  189. for (int i = 0; i < polylineAnnot.Points.Count - 1; i++)
  190. {
  191. Point endLinePoint = new Point(
  192. polylineAnnot.Points[i + 1].x,
  193. polylineAnnot.Points[i + 1].y
  194. );
  195. Point startLinePoint = new Point(
  196. polylineAnnot.Points[i].x,
  197. polylineAnnot.Points[i].y
  198. );
  199. Vector subVector = endLinePoint - startLinePoint;
  200. totalInch += subVector.Length;
  201. }
  202. totalInch = totalInch / 72D;
  203. CPDFPerimeterMeasure lineMeasure = polylineAnnot.GetPerimeterMeasure();
  204. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  205. double rate = measureInfo.RulerTranslate / measureInfo.RulerBase;
  206. double ratio = GetMeasureRatio(measureInfo.RulerBaseUnit);
  207. double showLenght = totalInch * rate / ratio;
  208. MeasureEventArgs measureEvent = new MeasureEventArgs();
  209. measureEvent.Angle = 0;
  210. //if (polylineAnnot.Points.Count() >= 3)
  211. //{
  212. // int count = polylineAnnot.Points.Count();
  213. // Point endLinePoint = new Point(
  214. // polylineAnnot.Points[count - 1].x,
  215. // polylineAnnot.Points[count - 1].y
  216. // );
  217. // Point cornerLinePoint = new Point(
  218. // polylineAnnot.Points[count - 2].x,
  219. // polylineAnnot.Points[count - 2].y
  220. // );
  221. // Point startLinePoint = new Point(
  222. // polylineAnnot.Points[count - 3].x,
  223. // polylineAnnot.Points[count - 3].y
  224. // );
  225. // Vector startVector = startLinePoint - cornerLinePoint;
  226. // Vector endvector = endLinePoint - cornerLinePoint;
  227. // measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, startVector));
  228. //}
  229. measureEvent.RulerTranslateUnit = measureInfo.RulerTranslateUnit;
  230. measureEvent.RulerTranslate = measureInfo.RulerTranslate;
  231. measureEvent.RulerBase = measureInfo.RulerBase;
  232. measureEvent.RulerBaseUnit = measureInfo.RulerBaseUnit;
  233. measureEvent.Precision = GetMeasureShowPrecision(measureInfo.Precision);
  234. measureEvent.Type = CPDFMeasureType.CPDF_PERIMETER_MEASURE;
  235. NumberFormatInfo formatInfo = new NumberFormatInfo();
  236. formatInfo.NumberDecimalDigits = Math.Abs(measureInfo.Precision).ToString().Length - 1;
  237. measureEvent.Distance = showLenght.ToString("N", formatInfo) + measureInfo.RulerTranslateUnit;
  238. MeasureSetting.InvokeMeasureChangeEvent(this, measureEvent);
  239. if (annotArgs != null)
  240. {
  241. SettingPanel.UpdateArgsList = new List<AnnotHandlerEventArgs> { annotArgs };
  242. }
  243. }
  244. }
  245. if(rawAnnot.Type== C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON)
  246. {
  247. CPDFPolygonAnnotation Annot = rawAnnot as CPDFPolygonAnnotation;
  248. CPDFCaptionType CaptionType = Annot.GetAreaMeasure().MeasureInfo.CaptionType;
  249. bool IsArea = false;
  250. bool IsLength = false;
  251. if ((CaptionType& CPDFCaptionType.CPDF_CAPTION_AREA)== CPDFCaptionType.CPDF_CAPTION_AREA)
  252. {
  253. IsArea = true;
  254. }
  255. if ((CaptionType & CPDFCaptionType.CPDF_CAPTION_LENGTH) == CPDFCaptionType.CPDF_CAPTION_LENGTH)
  256. {
  257. IsLength = true;
  258. }
  259. SettingPanel.ChangedCheckBoxIsChecked(IsArea, IsLength);
  260. double Area = Annot.GetMeasureValue(CPDFCaptionType.CPDF_CAPTION_AREA);
  261. double inch = MeasureSetting.GetMeasurePDFArea(Area);
  262. double Length = Annot.GetMeasureValue(CPDFCaptionType.CPDF_CAPTION_LENGTH);
  263. double currentInch = MeasureSetting.GetMeasurePDFLength(Length);
  264. MeasureEventArgs measureEvent = new MeasureEventArgs();
  265. measureEvent.RulerTranslateUnit = MeasureSetting.RulerTranslateUnit;
  266. measureEvent.RulerTranslate = MeasureSetting.RulerTranslate;
  267. measureEvent.RulerBase = MeasureSetting.RulerBase;
  268. measureEvent.RulerBaseUnit = MeasureSetting.RulerBaseUnit;
  269. measureEvent.Precision = MeasureSetting.Precision;
  270. measureEvent.Type = CPDFMeasureType.CPDF_AREA_MEASURE;
  271. measureEvent.Distance = string.Format(
  272. "{0}{1}",
  273. MeasureSetting.GetPrecisionData(currentInch),
  274. MeasureSetting.RulerTranslateUnit);
  275. measureEvent.Area = string.Format(
  276. "{0} sq {1}",
  277. MeasureSetting.GetPrecisionData(inch),
  278. MeasureSetting.RulerTranslateUnit);
  279. MeasureSetting.InvokeMeasureChangeEvent(this, measureEvent);
  280. if (annotArgs != null)
  281. {
  282. SettingPanel.UpdateArgsList = new List<AnnotHandlerEventArgs> { annotArgs };
  283. }
  284. }
  285. }
  286. catch (Exception e)
  287. {
  288. }
  289. }
  290. private double GetMeasureRatio(string baseUnit)
  291. {
  292. if (baseUnit == CPDFMeasure.CPDF_PT)
  293. {
  294. return 1 / 72;
  295. }
  296. if (baseUnit == CPDFMeasure.CPDF_IN)
  297. {
  298. return 1;
  299. }
  300. if (baseUnit == CPDFMeasure.CPDF_MM)
  301. {
  302. return 1 / 25.4;
  303. }
  304. if (baseUnit == CPDFMeasure.CPDF_CM)
  305. {
  306. return 1 / 2.54;
  307. }
  308. if (baseUnit == CPDFMeasure.CPDF_M)
  309. {
  310. return 1 / 0.0254;
  311. }
  312. if (baseUnit == CPDFMeasure.CPDFO_KM)
  313. {
  314. return 1 / 0.0254 / 1000;
  315. }
  316. if (baseUnit == CPDFMeasure.CPDF_FT)
  317. {
  318. return 12;
  319. }
  320. if (baseUnit == CPDFMeasure.CPDF_YD)
  321. {
  322. return 36;
  323. }
  324. if (baseUnit == CPDFMeasure.CPDF_MI)
  325. {
  326. return 63360;
  327. }
  328. return 0;
  329. }
  330. private double GetMeasureShowPrecision(int precision)
  331. {
  332. if (precision == CPDFMeasure.PRECISION_VALUE_ZERO)
  333. {
  334. return 1;
  335. }
  336. if (CPDFMeasure.PRECISION_VALUE_ONE == precision)
  337. {
  338. return 0.1;
  339. }
  340. if (CPDFMeasure.PRECISION_VALUE_TWO == precision)
  341. {
  342. return 0.01;
  343. }
  344. if (CPDFMeasure.PRECISION_VALUE_THREE == precision)
  345. {
  346. return 0.001;
  347. }
  348. if (CPDFMeasure.PRECISION_VALUE_FOUR == precision)
  349. {
  350. return 0.0001;
  351. }
  352. return 0;
  353. }
  354. public void SetSettingsControl(CPDFDisplaySettingsControl cPDFDisplaySettingsControl)
  355. {
  356. displaySettingsControl = cPDFDisplaySettingsControl;
  357. }
  358. public void ClearAllToolState()
  359. {
  360. PDFMeasureTool.ClearAllToolState();
  361. }
  362. public void ClearViewerControl()
  363. {
  364. PDFGrid.Child = null;
  365. BotaContainer.Child = null;
  366. PropertyContainer.Child = null;
  367. displaySettingsControl = null;
  368. }
  369. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  370. {
  371. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  372. {
  373. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  374. }
  375. else if (e.PropertyName == nameof(PanelState.RightPanel))
  376. {
  377. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  378. {
  379. ExpandRightPropertyPanel(measurePropertyControl, Visibility.Visible);
  380. }
  381. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  382. {
  383. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  384. }
  385. else
  386. {
  387. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  388. }
  389. }
  390. }
  391. #endregion
  392. #region Expand and collapse Panel
  393. public void ExpandRightPropertyPanel(Visibility visible)
  394. {
  395. ExpandRightPropertyPanel(measurePropertyControl, visible);
  396. }
  397. public void ExpandNullRightPropertyPanel(Visibility visible)
  398. {
  399. ExpandRightPropertyPanel(null, visible);
  400. }
  401. public void ExpandViewSettings(Visibility visible)
  402. {
  403. SetViewSettings(displaySettingsControl, visible);
  404. }
  405. private void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  406. {
  407. PropertyContainer.Width = 260;
  408. PropertyContainer.Child = propertytPanel;
  409. PropertyContainer.Visibility = visible;
  410. }
  411. private void SetViewSettings(CPDFDisplaySettingsControl displaySettingsControl, Visibility visibility)
  412. {
  413. PropertyContainer.Child = displaySettingsControl;
  414. PropertyContainer.Visibility = visibility;
  415. }
  416. public void ExpandLeftPanel(bool isExpand)
  417. {
  418. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  419. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  420. if (isExpand)
  421. {
  422. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  423. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  424. }
  425. else
  426. {
  427. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  428. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  429. }
  430. }
  431. #endregion
  432. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  433. {
  434. }
  435. private void MeasureInfoPanel_SettingClick(object sender, EventArgs e)
  436. {
  437. SettingPanel.ReturnToInfoPanel = true;
  438. SetInfoPanelVisble(false, true);
  439. SettingPanel.BindMeasureSetting();
  440. }
  441. private void SettingPanel_CancelEvent(object sender, EventArgs e)
  442. {
  443. SetInfoPanelVisble(SettingPanel.ReturnToInfoPanel, false);
  444. }
  445. private void SettingPanel_DoneEvent(object sender, EventArgs e)
  446. {
  447. SetInfoPanelVisble(SettingPanel.ReturnToInfoPanel, false);
  448. }
  449. public void SetInfoPanelVisble(bool measureInfo, bool measureSetting)
  450. {
  451. if (measureInfo)
  452. {
  453. InfoPanel.ClearMeasureInfo();
  454. }
  455. InfoPanel.Visibility = measureInfo ? Visibility.Visible : Visibility.Collapsed;
  456. SettingPanel.Visibility = measureSetting ? Visibility.Visible : Visibility.Collapsed;
  457. }
  458. public void SetMeasureInfoType(CPDFMeasureType measureType)
  459. {
  460. InfoPanel?.SetMeasureType(measureType);
  461. }
  462. public void SetMeasureScale(CPDFMeasureType measureType, string scale)
  463. {
  464. InfoPanel?.SetMeasureScale(measureType,scale);
  465. }
  466. }
  467. }