CPDFShapeUI.xaml.cs 22 KB

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