MeasureControl.xaml.cs 21 KB

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