MeasureSettingPanel.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using Compdfkit_Tools.PDFControl;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer.PdfViewer;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. namespace Compdfkit_Tools.Measure
  12. {
  13. /// <summary>
  14. /// MeasureSettingPanel.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class MeasureSettingPanel : UserControl
  17. {
  18. public event EventHandler CancelEvent;
  19. public event EventHandler DoneEvent;
  20. public PDFViewControl PdfViewControl { get; set; }
  21. public bool ReturnToInfoPanel { get; set; }
  22. public List<AnnotHandlerEventArgs> UpdateArgsList { get; set; } = new List<AnnotHandlerEventArgs>();
  23. public MeasureSettingPanel()
  24. {
  25. InitializeComponent();
  26. }
  27. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  28. {
  29. if (UpdateArgsList != null && UpdateArgsList.Count > 0)
  30. {
  31. ReturnToInfoPanel = false;
  32. }
  33. UpdateArgsList?.Clear();
  34. CancelEvent?.Invoke(this, e);
  35. ReturnToInfoPanel = false;
  36. }
  37. public void ShowAreaAndLength(Visibility visibility)
  38. {
  39. if (visibility == Visibility.Visible)
  40. {
  41. SettingPanel.Height = 350;
  42. }
  43. else
  44. {
  45. SettingPanel.Height = 290;
  46. }
  47. AreaAndLength.Visibility = visibility;
  48. }
  49. private void ButtonDone_Click(object sender, RoutedEventArgs e)
  50. {
  51. if (string.IsNullOrEmpty(RulerBaseText.Text) || string.IsNullOrEmpty(RulerTranslateText.Text))
  52. {
  53. MessageBox.Show("Scale is not greater than zero");
  54. return;
  55. }
  56. if (double.TryParse(RulerBaseText.Text, out double ruleBase))
  57. {
  58. if (ruleBase <= 0)
  59. {
  60. MessageBox.Show("Scale is not greater than zero");
  61. return;
  62. }
  63. }
  64. if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  65. {
  66. if (ruletranBase <= 0)
  67. {
  68. MessageBox.Show("Scale is not greater than zero");
  69. return;
  70. }
  71. }
  72. if (UpdateArgsList != null && UpdateArgsList.Count == 0)
  73. {
  74. SaveMeasureSetting();
  75. }
  76. if (UpdateArgsList != null && UpdateArgsList.Count > 0)
  77. {
  78. List<AnnotArgsType> allowTypeList = new List<AnnotArgsType>()
  79. {
  80. AnnotArgsType.LineMeasure,
  81. AnnotArgsType.PolyLineMeasure,
  82. AnnotArgsType.PolygonMeasure
  83. };
  84. foreach (AnnotHandlerEventArgs args in UpdateArgsList)
  85. {
  86. if (allowTypeList.Contains(args.EventType))
  87. {
  88. CPDFAnnotation pdfAnnot = args.GetPDFAnnot();
  89. switch (pdfAnnot.Type)
  90. {
  91. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  92. {
  93. CPDFLineAnnotation lineAnnot = (CPDFLineAnnotation)pdfAnnot;
  94. if (lineAnnot.IsMersured())
  95. {
  96. CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
  97. CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
  98. if (PrecisionBox.SelectedValue != null)
  99. {
  100. ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  101. if (double.TryParse(checkItem.Content.ToString(), out double precision))
  102. {
  103. measureInfo.Precision = GetMeasureSavePrecision(precision);
  104. }
  105. }
  106. if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
  107. {
  108. measureInfo.RulerBase = (float)ruleBasedata;
  109. }
  110. if (RulerBaseUnitCombo.SelectedItem != null)
  111. {
  112. ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  113. measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
  114. }
  115. if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
  116. {
  117. measureInfo.RulerTranslate = (float)ruletranBasedata;
  118. }
  119. if (RulerTranslateCombo.SelectedItem != null)
  120. {
  121. ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  122. measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
  123. }
  124. lineMeasure.SetMeasureInfo(measureInfo);
  125. lineMeasure.SetMeasureScale(
  126. measureInfo.RulerBase,
  127. measureInfo.RulerBaseUnit,
  128. measureInfo.RulerTranslate,
  129. measureInfo.RulerTranslateUnit);
  130. lineMeasure.UpdateAnnotMeasure();
  131. lineAnnot.UpdateAp();
  132. args.Draw();
  133. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  134. {
  135. CPDFViewer viewer = PdfViewControl.PDFView;
  136. if (viewer != null && viewer.UndoManager != null)
  137. {
  138. viewer.UndoManager.CanSave = true;
  139. }
  140. }
  141. }
  142. }
  143. break;
  144. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  145. {
  146. CPDFPolylineAnnotation polylineAnnot = (CPDFPolylineAnnotation)pdfAnnot;
  147. if (polylineAnnot.IsMersured())
  148. {
  149. CPDFPerimeterMeasure polylineMeasure = polylineAnnot.GetPerimeterMeasure();
  150. CPDFMeasureInfo measureInfo = polylineMeasure.MeasureInfo;
  151. if (PrecisionBox.SelectedValue != null)
  152. {
  153. ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  154. if (double.TryParse(checkItem.Content.ToString(), out double precision))
  155. {
  156. measureInfo.Precision = GetMeasureSavePrecision(precision);
  157. }
  158. }
  159. if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
  160. {
  161. measureInfo.RulerBase = (float)ruleBasedata;
  162. }
  163. if (RulerBaseUnitCombo.SelectedItem != null)
  164. {
  165. ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  166. measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
  167. }
  168. if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
  169. {
  170. measureInfo.RulerTranslate = (float)ruletranBasedata;
  171. }
  172. if (RulerTranslateCombo.SelectedItem != null)
  173. {
  174. ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  175. measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
  176. }
  177. polylineMeasure.SetMeasureInfo(measureInfo);
  178. polylineMeasure.SetMeasureScale(
  179. measureInfo.RulerBase,
  180. measureInfo.RulerBaseUnit,
  181. measureInfo.RulerTranslate,
  182. measureInfo.RulerTranslateUnit);
  183. polylineMeasure.UpdateAnnotMeasure();
  184. polylineAnnot.UpdateAp();
  185. args.Draw();
  186. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  187. {
  188. CPDFViewer viewer = PdfViewControl.PDFView;
  189. if (viewer != null && viewer.UndoManager != null)
  190. {
  191. viewer.UndoManager.CanSave = true;
  192. }
  193. }
  194. }
  195. }
  196. break;
  197. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  198. {
  199. CPDFPolygonAnnotation areaAnnot = (CPDFPolygonAnnotation)pdfAnnot;
  200. if (areaAnnot.IsMersured())
  201. {
  202. CPDFAreaMeasure areaMeasure = areaAnnot.GetAreaMeasure();
  203. CPDFMeasureInfo measureInfo = areaMeasure.MeasureInfo;
  204. if (PrecisionBox.SelectedValue != null)
  205. {
  206. ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  207. if (double.TryParse(checkItem.Content.ToString(), out double precision))
  208. {
  209. measureInfo.Precision = GetMeasureSavePrecision(precision);
  210. }
  211. }
  212. if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
  213. {
  214. measureInfo.RulerBase = (float)ruleBasedata;
  215. }
  216. if (RulerBaseUnitCombo.SelectedItem != null)
  217. {
  218. ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  219. measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
  220. }
  221. if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
  222. {
  223. measureInfo.RulerTranslate = (float)ruletranBasedata;
  224. }
  225. if (RulerTranslateCombo.SelectedItem != null)
  226. {
  227. ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  228. measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
  229. }
  230. measureInfo.CaptionType = CPDFCaptionType.CPDF_CAPTION_NONE;
  231. if ((bool)AreaCheckBox.IsChecked)
  232. {
  233. measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_AREA;
  234. }
  235. if ((bool)LengthCheckBox.IsChecked)
  236. {
  237. measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_LENGTH;
  238. }
  239. areaMeasure.SetMeasureInfo(measureInfo);
  240. areaMeasure.SetMeasureScale(
  241. measureInfo.RulerBase,
  242. measureInfo.RulerBaseUnit,
  243. measureInfo.RulerTranslate,
  244. measureInfo.RulerTranslateUnit);
  245. areaMeasure.UpdateAnnotMeasure();
  246. areaAnnot.UpdateAp();
  247. args.Draw();
  248. if(PdfViewControl!=null && PdfViewControl.PDFView!=null)
  249. {
  250. CPDFViewer viewer = PdfViewControl.PDFView;
  251. if(viewer!=null && viewer.UndoManager!=null)
  252. {
  253. viewer.UndoManager.CanSave = true;
  254. }
  255. }
  256. }
  257. }
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. }
  264. ReturnToInfoPanel = false;
  265. }
  266. UpdateArgsList?.Clear();
  267. DoneEvent?.Invoke(this, e);
  268. ReturnToInfoPanel = false;
  269. }
  270. private int GetMeasureSavePrecision(double Precision)
  271. {
  272. if (Precision == 1)
  273. {
  274. return CPDFMeasure.PRECISION_VALUE_ZERO;
  275. }
  276. if (Precision == 0.1)
  277. {
  278. return CPDFMeasure.PRECISION_VALUE_ONE;
  279. }
  280. if (Precision == 0.01)
  281. {
  282. return CPDFMeasure.PRECISION_VALUE_TWO;
  283. }
  284. if (Precision == 0.001)
  285. {
  286. return CPDFMeasure.PRECISION_VALUE_THREE;
  287. }
  288. if (Precision == 0.0001)
  289. {
  290. return CPDFMeasure.PRECISION_VALUE_FOUR;
  291. }
  292. return 0;
  293. }
  294. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  295. {
  296. List<Key> allowKeys = new List<Key>()
  297. {
  298. Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
  299. Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
  300. Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right,
  301. Key.OemPeriod,Key.Decimal
  302. };
  303. if (allowKeys.Contains(e.Key) == false)
  304. {
  305. e.Handled = true;
  306. }
  307. }
  308. private void TextBox_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  309. {
  310. try
  311. {
  312. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  313. {
  314. string checkText = Clipboard.GetText();
  315. if (int.TryParse(checkText, out int value))
  316. {
  317. e.CanExecute = true;
  318. }
  319. e.Handled = true;
  320. }
  321. }
  322. catch (Exception ex)
  323. {
  324. }
  325. }
  326. public void BindMeasureSetting()
  327. {
  328. if (UpdateArgsList != null && UpdateArgsList.Count>0)
  329. {
  330. return;
  331. }
  332. RulerBaseText.Text = MeasureSetting.RulerBase.ToString();
  333. RulerTranslateText.Text = MeasureSetting.RulerTranslate.ToString();
  334. RulerTranslateCombo.SelectedIndex = -1;
  335. RulerBaseUnitCombo.SelectedIndex = -1;
  336. PrecisionBox.SelectedIndex = -1;
  337. if (MeasureSetting.RulerBaseUnit == "in")
  338. {
  339. RulerBaseUnitCombo.SelectedIndex = 0;
  340. }
  341. if (MeasureSetting.RulerBaseUnit == "cm")
  342. {
  343. RulerBaseUnitCombo.SelectedIndex = 1;
  344. }
  345. if (MeasureSetting.RulerBaseUnit == "mm")
  346. {
  347. RulerBaseUnitCombo.SelectedIndex = 2;
  348. }
  349. for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
  350. {
  351. ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
  352. if (checkItem != null && checkItem.Content.ToString() == MeasureSetting.RulerTranslateUnit.ToString())
  353. {
  354. RulerTranslateCombo.SelectedIndex = i;
  355. }
  356. }
  357. for (int i = 0; i < PrecisionBox.Items.Count; i++)
  358. {
  359. ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  360. if (checkItem != null && checkItem.Content.ToString() == ((decimal)MeasureSetting.Precision).ToString())
  361. {
  362. PrecisionBox.SelectedIndex = i;
  363. }
  364. }
  365. }
  366. public void BindMeasureSetting(MeasureEventArgs measureEventArgs)
  367. {
  368. if (measureEventArgs != null)
  369. {
  370. RulerBaseText.Text = measureEventArgs.RulerBase.ToString();
  371. RulerTranslateText.Text = measureEventArgs.RulerTranslate.ToString();
  372. RulerTranslateCombo.SelectedIndex = -1;
  373. RulerBaseUnitCombo.SelectedIndex = -1;
  374. PrecisionBox.SelectedIndex = -1;
  375. if (measureEventArgs.RulerBaseUnit == "in")
  376. {
  377. RulerBaseUnitCombo.SelectedIndex = 0;
  378. }
  379. if (measureEventArgs.RulerBaseUnit == "cm")
  380. {
  381. RulerBaseUnitCombo.SelectedIndex = 1;
  382. }
  383. if (measureEventArgs.RulerBaseUnit == "mm")
  384. {
  385. RulerBaseUnitCombo.SelectedIndex = 2;
  386. }
  387. for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
  388. {
  389. ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
  390. if (checkItem != null && checkItem.Content.ToString() == measureEventArgs.RulerTranslateUnit.ToString())
  391. {
  392. RulerTranslateCombo.SelectedIndex = i;
  393. }
  394. }
  395. for (int i = 0; i < PrecisionBox.Items.Count; i++)
  396. {
  397. ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  398. if (checkItem != null && checkItem.Content.ToString() == ((decimal)measureEventArgs.Precision).ToString())
  399. {
  400. PrecisionBox.SelectedIndex = i;
  401. }
  402. }
  403. }
  404. }
  405. private void SaveMeasureSetting()
  406. {
  407. if (double.TryParse(RulerBaseText.Text, out double ruleBase))
  408. {
  409. MeasureSetting.RulerBase = ruleBase;
  410. }
  411. if (RulerBaseUnitCombo.SelectedItem != null)
  412. {
  413. ComboBoxItem checkItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  414. MeasureSetting.RulerBaseUnit = checkItem.Content.ToString();
  415. }
  416. if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  417. {
  418. MeasureSetting.RulerTranslate = ruletranBase;
  419. }
  420. if (RulerTranslateCombo.SelectedItem != null)
  421. {
  422. ComboBoxItem checkItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  423. MeasureSetting.RulerTranslateUnit = checkItem.Content.ToString();
  424. }
  425. if (PrecisionBox.SelectedValue != null)
  426. {
  427. ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  428. if (double.TryParse(checkItem.Content.ToString(), out double precision))
  429. {
  430. MeasureSetting.Precision = precision;
  431. }
  432. }
  433. MeasureSetting.IsShowArea = (bool)AreaCheckBox.IsChecked;
  434. MeasureSetting.IsShowLength = (bool)LengthCheckBox.IsChecked;
  435. }
  436. public void ChangedCheckBoxIsChecked(bool Area, bool Lenght)
  437. {
  438. AreaCheckBox.IsChecked = Area;
  439. LengthCheckBox.IsChecked = Lenght;
  440. }
  441. }
  442. }