CPDFShapeUI.xaml.cs 22 KB

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