CPDFShapeUI.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool;
  3. using Compdfkit_Tools.Common;
  4. using Compdfkit_Tools.Data;
  5. using Compdfkit_Tools.PDFControl;
  6. using ComPDFKitViewer;
  7. using ComPDFKitViewer.Annot;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO.Ports;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Media;
  14. using Compdfkit_Tools.Helper;
  15. using ComPDFKit.PDFAnnotation;
  16. namespace Compdfkit_Tools.PDFControlUI
  17. {
  18. public partial class CPDFShapeUI : UserControl
  19. {
  20. private CPDFAnnotationType currentAnnotationType;
  21. private AnnotParam annotParam;
  22. private CPDFAnnotation annotCore;
  23. private PDFViewControl viewControl;
  24. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  25. public CPDFShapeUI()
  26. {
  27. InitializeComponent();
  28. BorderColorPickerControl.ColorChanged -= BorderColorPickerControl_ColorChanged;
  29. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  30. CPDFThicknessControl.ThicknessChanged -= CPDFThicknessControl_ThicknessChanged;
  31. CPDFLineStyleControl.LineStyleChanged -= CPDFLineStyleControl_LineStyleChanged;
  32. BorderColorPickerControl.ColorChanged += BorderColorPickerControl_ColorChanged;
  33. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  34. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  35. CPDFLineStyleControl.LineStyleChanged += CPDFLineStyleControl_LineStyleChanged;
  36. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  37. }
  38. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  39. {
  40. if (annotParam == null)
  41. {
  42. PropertyChanged?.Invoke(this, GetShapeData());
  43. }
  44. else
  45. {
  46. SolidColorBrush borderBrush=BorderColorPickerControl.Brush as SolidColorBrush;
  47. if(annotCore!=null && annotCore.IsValid() && borderBrush!=null)
  48. {
  49. if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  50. {
  51. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  52. squareAnnot?.SetLineColor(new byte[3]
  53. {
  54. borderBrush.Color.R,
  55. borderBrush.Color.G,
  56. borderBrush.Color.B
  57. });
  58. }
  59. if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  60. {
  61. CPDFCircleAnnotation circleAnnot= annotCore as CPDFCircleAnnotation;
  62. circleAnnot?.SetLineColor(new byte[3]
  63. {
  64. borderBrush.Color.R,
  65. borderBrush.Color.G,
  66. borderBrush.Color.B
  67. });
  68. }
  69. if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  70. {
  71. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  72. lineAnnot?.SetLineColor(new byte[3]
  73. {
  74. borderBrush.Color.R,
  75. borderBrush.Color.G,
  76. borderBrush.Color.B
  77. });
  78. }
  79. if (viewControl != null && viewControl.PDFViewTool != null)
  80. {
  81. viewControl.UpdateAnnotFrame();
  82. }
  83. }
  84. }
  85. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  86. }
  87. private void FillColorPickerControl_ColorChanged(object sender, EventArgs e)
  88. {
  89. if (annotParam == null)
  90. {
  91. PropertyChanged?.Invoke(this, GetShapeData());
  92. }
  93. else
  94. {
  95. SolidColorBrush fillBrush = FillColorPickerControl.Brush as SolidColorBrush;
  96. if (annotCore != null && annotCore.IsValid() && fillBrush != null)
  97. {
  98. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  99. {
  100. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  101. squareAnnot?.SetBgColor(new byte[3]
  102. {
  103. fillBrush.Color.R,
  104. fillBrush.Color.G,
  105. fillBrush.Color.B
  106. });
  107. }
  108. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  109. {
  110. CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
  111. circleAnnot?.SetBgColor(new byte[3]
  112. {
  113. fillBrush.Color.R,
  114. fillBrush.Color.G,
  115. fillBrush.Color.B
  116. });
  117. }
  118. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  119. {
  120. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  121. lineAnnot?.SetBgColor(new byte[3]
  122. {
  123. fillBrush.Color.R,
  124. fillBrush.Color.G,
  125. fillBrush.Color.B
  126. });
  127. }
  128. if (viewControl != null && viewControl.PDFViewTool != null)
  129. {
  130. viewControl.UpdateAnnotFrame();
  131. }
  132. }
  133. }
  134. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  135. }
  136. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  137. {
  138. if (annotParam == null)
  139. {
  140. PropertyChanged?.Invoke(this, GetShapeData());
  141. }
  142. else
  143. {
  144. if (annotCore != null && annotCore.IsValid())
  145. {
  146. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  147. {
  148. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  149. squareAnnot?.SetLineWidth(CPDFThicknessControl.Thickness);
  150. }
  151. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  152. {
  153. CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
  154. circleAnnot?.SetLineWidth(CPDFThicknessControl.Thickness);
  155. }
  156. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  157. {
  158. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  159. lineAnnot?.SetLineWidth(CPDFThicknessControl.Thickness);
  160. }
  161. if (viewControl != null && viewControl.PDFViewTool != null)
  162. {
  163. viewControl.UpdateAnnotFrame();
  164. }
  165. }
  166. }
  167. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  168. }
  169. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  170. {
  171. if (annotParam == null)
  172. {
  173. PropertyChanged?.Invoke(this, GetShapeData());
  174. }
  175. else
  176. {
  177. double opacity = CPDFOpacityControl.OpacityValue / 100.0;
  178. if (opacity > 0 && opacity <= 1)
  179. {
  180. opacity = opacity * 255;
  181. }
  182. if (annotCore != null && annotCore.IsValid())
  183. {
  184. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  185. {
  186. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  187. squareAnnot?.SetTransparency((byte)opacity);
  188. }
  189. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  190. {
  191. CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
  192. circleAnnot?.SetTransparency((byte)opacity);
  193. }
  194. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  195. {
  196. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  197. lineAnnot?.SetTransparency((byte)opacity);
  198. }
  199. if (viewControl != null && viewControl.PDFViewTool != null)
  200. {
  201. viewControl.UpdateAnnotFrame();
  202. }
  203. }
  204. }
  205. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  206. }
  207. private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
  208. {
  209. if (annotParam == null)
  210. {
  211. PropertyChanged?.Invoke(this, GetShapeData());
  212. }
  213. else
  214. {
  215. if (annotCore != null && annotCore.IsValid())
  216. {
  217. float[] dashArray = null;
  218. C_BORDER_STYLE borderStyle;
  219. if (CPDFLineStyleControl.DashStyle == DashStyles.Solid || CPDFLineStyleControl.DashStyle == null)
  220. {
  221. dashArray = new float[0];
  222. borderStyle = C_BORDER_STYLE.BS_SOLID;
  223. }
  224. else
  225. {
  226. List<float> floatArray = new List<float>();
  227. foreach (double num in CPDFLineStyleControl.DashStyle.Dashes)
  228. {
  229. floatArray.Add((float)num);
  230. }
  231. dashArray = floatArray.ToArray();
  232. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  233. }
  234. if (dashArray != null)
  235. {
  236. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  237. {
  238. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  239. squareAnnot?.SetBorderStyle(borderStyle, dashArray);
  240. }
  241. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  242. {
  243. CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
  244. circleAnnot?.SetBorderStyle(borderStyle, dashArray);
  245. }
  246. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  247. {
  248. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  249. lineAnnot?.SetBorderStyle(borderStyle, dashArray);
  250. }
  251. if (viewControl != null && viewControl.PDFViewTool != null)
  252. {
  253. viewControl.UpdateAnnotFrame();
  254. }
  255. }
  256. }
  257. }
  258. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  259. }
  260. private void CPDFArrowControl_ArrowChanged(object sender, EventArgs e)
  261. {
  262. if (annotParam == null)
  263. {
  264. PropertyChanged?.Invoke(PropertyChanged, GetShapeData());
  265. }
  266. else
  267. {
  268. if (annotCore != null && annotCore.IsValid())
  269. {
  270. if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  271. {
  272. CPDFLineAnnotation lineAnnot= annotCore as CPDFLineAnnotation;
  273. if(lineAnnot!=null)
  274. {
  275. lineAnnot.SetLineType(CPDFArrowControl.LineType.HeadLineType, CPDFArrowControl.LineType.TailLineType);
  276. if (viewControl != null && viewControl.PDFViewTool != null)
  277. {
  278. viewControl.UpdateAnnotFrame();
  279. }
  280. }
  281. }
  282. }
  283. }
  284. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  285. }
  286. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  287. {
  288. if (annotParam == null)
  289. {
  290. PropertyChanged?.Invoke(this, GetShapeData());
  291. }
  292. else
  293. {
  294. if (annotCore != null && annotCore.IsValid())
  295. {
  296. annotCore.SetContent(NoteTextBox.Text);
  297. if (viewControl != null && viewControl.PDFViewTool != null)
  298. {
  299. viewControl.UpdateAnnotFrame();
  300. }
  301. }
  302. }
  303. }
  304. public CPDFAnnotationData GetShapeData()
  305. {
  306. if (currentAnnotationType == CPDFAnnotationType.Circle || currentAnnotationType == CPDFAnnotationType.Square)
  307. {
  308. CPDFShapeData pdfShapeData = new CPDFShapeData();
  309. pdfShapeData.AnnotationType = currentAnnotationType;
  310. pdfShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  311. pdfShapeData.FillColor = ((SolidColorBrush)FillColorPickerControl.Brush).Color;
  312. pdfShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  313. pdfShapeData.Thickness = CPDFThicknessControl.Thickness;
  314. pdfShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
  315. pdfShapeData.Note = NoteTextBox.Text;
  316. return pdfShapeData;
  317. }
  318. else
  319. {
  320. CPDFLineShapeData pdfLineShapeData = new CPDFLineShapeData();
  321. pdfLineShapeData.AnnotationType = currentAnnotationType;
  322. pdfLineShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  323. pdfLineShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  324. pdfLineShapeData.LineType = CPDFArrowControl.LineType;
  325. pdfLineShapeData.Thickness = CPDFThicknessControl.Thickness;
  326. pdfLineShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
  327. pdfLineShapeData.LineType = CPDFArrowControl.LineType;
  328. pdfLineShapeData.Note = NoteTextBox.Text;
  329. return pdfLineShapeData;
  330. }
  331. }
  332. public void SetPresentAnnotAttrib(AnnotParam param,CPDFAnnotation annot,PDFViewControl view)
  333. {
  334. this.annotParam = null;
  335. this.annotCore = null;
  336. this.viewControl = null;
  337. if(param != null)
  338. {
  339. if(param is SquareParam)
  340. {
  341. SquareParam squareParam= (SquareParam)param;
  342. CPDFThicknessControl.Thickness = (int)squareParam.LineWidth;
  343. if (squareParam.LineColor != null)
  344. {
  345. BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  346. squareParam.LineColor[0],
  347. squareParam.LineColor[1],
  348. squareParam.LineColor[2]));
  349. BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
  350. squareParam.LineColor[0],
  351. squareParam.LineColor[1],
  352. squareParam.LineColor[2]));
  353. }
  354. if (squareParam.BgColor!=null)
  355. {
  356. FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
  357. squareParam.BgColor[0],
  358. squareParam.BgColor[1],
  359. squareParam.BgColor[2]));
  360. }
  361. if (squareParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  362. {
  363. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  364. }
  365. else
  366. {
  367. CPDFLineStyleControl.DashStyle = DashStyles.Dash;
  368. }
  369. }
  370. if(param is CircleParam)
  371. {
  372. CircleParam circleParam= (CircleParam)param;
  373. CPDFThicknessControl.Thickness = (int)circleParam.LineWidth;
  374. if (circleParam.LineColor != null)
  375. {
  376. BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  377. circleParam.LineColor[0],
  378. circleParam.LineColor[1],
  379. circleParam.LineColor[2]));
  380. BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
  381. circleParam.LineColor[0],
  382. circleParam.LineColor[1],
  383. circleParam.LineColor[2]));
  384. }
  385. if (circleParam.BgColor!=null)
  386. {
  387. FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
  388. circleParam.BgColor[0],
  389. circleParam.BgColor[1],
  390. circleParam.BgColor[2]));
  391. }
  392. if (circleParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  393. {
  394. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  395. }
  396. else
  397. {
  398. CPDFLineStyleControl.DashStyle = DashStyles.Dash;
  399. }
  400. }
  401. if(param is LineParam)
  402. {
  403. LineParam lineParam= (LineParam)param;
  404. CPDFThicknessControl.Thickness = (int)lineParam.LineWidth;
  405. if (lineParam.LineColor != null)
  406. {
  407. BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  408. lineParam.LineColor[0],
  409. lineParam.LineColor[1],
  410. lineParam.LineColor[2]));
  411. BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
  412. lineParam.LineColor[0],
  413. lineParam.LineColor[1],
  414. lineParam.LineColor[2]));
  415. }
  416. if (lineParam.BgColor != null)
  417. {
  418. FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
  419. lineParam.BgColor[0],
  420. lineParam.BgColor[1],
  421. lineParam.BgColor[2]));
  422. }
  423. if(lineParam.BorderStyle==C_BORDER_STYLE.BS_SOLID)
  424. {
  425. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  426. }
  427. else
  428. {
  429. CPDFLineStyleControl.DashStyle = DashStyles.Dash;
  430. }
  431. LineType lineType = new LineType()
  432. {
  433. HeadLineType = lineParam.HeadLineType,
  434. TailLineType = lineParam.TailLineType
  435. };
  436. CPDFArrowControl.LineType = lineType;
  437. }
  438. CPDFOpacityControl.OpacityValue = (int)(param.Transparency/255 * 100);
  439. NoteTextBox.Text = param.Content;
  440. }
  441. this.annotParam = param;
  442. this.annotCore = annot;
  443. this.viewControl = view;
  444. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  445. }
  446. public void InitWhenRectAndRound()
  447. {
  448. FillColorStackPanel.Visibility = Visibility.Visible;
  449. ArrowStackPanel.Visibility = Visibility.Collapsed;
  450. FillColorPickerControl.ColorChanged -= FillColorPickerControl_ColorChanged;
  451. FillColorPickerControl.ColorChanged += FillColorPickerControl_ColorChanged;
  452. CPDFArrowControl.ArrowChanged -= CPDFArrowControl_ArrowChanged;
  453. }
  454. public void InitWhenArrowAndLine()
  455. {
  456. FillColorStackPanel.Visibility = Visibility.Collapsed;
  457. ArrowStackPanel.Visibility = Visibility.Visible;
  458. CPDFArrowControl.ArrowChanged -= CPDFArrowControl_ArrowChanged;
  459. CPDFArrowControl.ArrowChanged += CPDFArrowControl_ArrowChanged;
  460. FillColorPickerControl.ColorChanged -= FillColorPickerControl_ColorChanged;
  461. LineType lineType;
  462. if (currentAnnotationType == CPDFAnnotationType.Arrow)
  463. {
  464. lineType = new LineType()
  465. {
  466. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  467. TailLineType = C_LINE_TYPE.LINETYPE_ARROW
  468. };
  469. }
  470. else
  471. {
  472. lineType = new LineType()
  473. {
  474. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  475. TailLineType = C_LINE_TYPE.LINETYPE_NONE
  476. };
  477. }
  478. CPDFArrowControl.LineType = lineType;
  479. }
  480. public void InitWithAnnotationType(CPDFAnnotationType annotationType)
  481. {
  482. currentAnnotationType = annotationType;
  483. switch (annotationType)
  484. {
  485. case CPDFAnnotationType.Square:
  486. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Square");
  487. InitWhenRectAndRound();
  488. break;
  489. case CPDFAnnotationType.Circle:
  490. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Circle");
  491. InitWhenRectAndRound();
  492. break;
  493. case CPDFAnnotationType.Arrow:
  494. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Arrow");
  495. InitWhenArrowAndLine();
  496. break;
  497. case CPDFAnnotationType.Line:
  498. TitleTextBlock.Text = LanguageHelper.PropertyPanelManager.GetString("Title_Line");
  499. InitWhenArrowAndLine();
  500. break;
  501. default:
  502. throw new ArgumentException("Not Excepted Argument");
  503. }
  504. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  505. }
  506. }
  507. }