CPDFShapeUI.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  76. pdfViewer?.UpDataAnnotFrame();
  77. }
  78. }
  79. }
  80. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  81. }
  82. private void FillColorPickerControl_ColorChanged(object sender, EventArgs e)
  83. {
  84. if (annotParam == null)
  85. {
  86. PropertyChanged?.Invoke(this, GetShapeData());
  87. }
  88. else
  89. {
  90. SolidColorBrush fillBrush = FillColorPickerControl.Brush as SolidColorBrush;
  91. if (annotCore != null && annotCore.IsValid() && fillBrush != null)
  92. {
  93. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  94. {
  95. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  96. squareAnnot?.SetBgColor(new byte[3]
  97. {
  98. fillBrush.Color.R,
  99. fillBrush.Color.G,
  100. fillBrush.Color.B
  101. });
  102. }
  103. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  104. {
  105. CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
  106. circleAnnot?.SetBgColor(new byte[3]
  107. {
  108. fillBrush.Color.R,
  109. fillBrush.Color.G,
  110. fillBrush.Color.B
  111. });
  112. }
  113. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  114. {
  115. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  116. lineAnnot?.SetBgColor(new byte[3]
  117. {
  118. fillBrush.Color.R,
  119. fillBrush.Color.G,
  120. fillBrush.Color.B
  121. });
  122. }
  123. if (viewControl != null && viewControl.PDFViewTool != null)
  124. {
  125. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  126. pdfViewer?.UpDataAnnotFrame();
  127. }
  128. }
  129. }
  130. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  131. }
  132. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  133. {
  134. if (annotParam == null)
  135. {
  136. PropertyChanged?.Invoke(this, GetShapeData());
  137. }
  138. else
  139. {
  140. if (annotCore != null && annotCore.IsValid())
  141. {
  142. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  143. {
  144. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  145. squareAnnot?.SetBorderWidth(CPDFThicknessControl.Thickness);
  146. }
  147. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  148. {
  149. CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
  150. circleAnnot?.SetBorderWidth(CPDFThicknessControl.Thickness);
  151. }
  152. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  153. {
  154. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  155. lineAnnot?.SetBorderWidth(CPDFThicknessControl.Thickness);
  156. }
  157. if (viewControl != null && viewControl.PDFViewTool != null)
  158. {
  159. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  160. pdfViewer?.UpDataAnnotFrame();
  161. }
  162. }
  163. }
  164. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  165. }
  166. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  167. {
  168. if (annotParam == null)
  169. {
  170. PropertyChanged?.Invoke(this, GetShapeData());
  171. }
  172. else
  173. {
  174. double opacity = CPDFOpacityControl.OpacityValue / 100.0;
  175. if (opacity > 0 && opacity <= 1)
  176. {
  177. opacity = opacity * 255;
  178. }
  179. if (annotCore != null && annotCore.IsValid())
  180. {
  181. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)
  182. {
  183. CPDFSquareAnnotation squareAnnot = annotCore as CPDFSquareAnnotation;
  184. squareAnnot?.SetTransparency((byte)opacity);
  185. }
  186. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE)
  187. {
  188. CPDFCircleAnnotation circleAnnot = annotCore as CPDFCircleAnnotation;
  189. circleAnnot?.SetTransparency((byte)opacity);
  190. }
  191. if (annotCore.Type == C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  192. {
  193. CPDFLineAnnotation lineAnnot = annotCore as CPDFLineAnnotation;
  194. lineAnnot?.SetTransparency((byte)opacity);
  195. }
  196. if (viewControl != null && viewControl.PDFViewTool != null)
  197. {
  198. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  199. pdfViewer?.UpDataAnnotFrame();
  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. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  252. pdfViewer?.UpDataAnnotFrame();
  253. }
  254. }
  255. }
  256. }
  257. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  258. }
  259. private void CPDFArrowControl_ArrowChanged(object sender, EventArgs e)
  260. {
  261. if (annotParam == null)
  262. {
  263. PropertyChanged?.Invoke(PropertyChanged, GetShapeData());
  264. }
  265. else
  266. {
  267. if (annotCore != null && annotCore.IsValid())
  268. {
  269. if(annotCore.Type==C_ANNOTATION_TYPE.C_ANNOTATION_LINE)
  270. {
  271. CPDFLineAnnotation lineAnnot= annotCore as CPDFLineAnnotation;
  272. if(lineAnnot!=null)
  273. {
  274. lineAnnot.SetLineType(CPDFArrowControl.LineType.HeadLineType, CPDFArrowControl.LineType.TailLineType);
  275. if (viewControl != null && viewControl.PDFViewTool != null)
  276. {
  277. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  278. pdfViewer?.UpDataAnnotFrame();
  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. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  300. pdfViewer?.UpDataAnnotFrame();
  301. }
  302. }
  303. }
  304. }
  305. public CPDFAnnotationData GetShapeData()
  306. {
  307. if (currentAnnotationType == CPDFAnnotationType.Circle || currentAnnotationType == CPDFAnnotationType.Square)
  308. {
  309. CPDFShapeData pdfShapeData = new CPDFShapeData();
  310. pdfShapeData.AnnotationType = currentAnnotationType;
  311. pdfShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  312. pdfShapeData.FillColor = ((SolidColorBrush)FillColorPickerControl.Brush).Color;
  313. pdfShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  314. pdfShapeData.Thickness = CPDFThicknessControl.Thickness;
  315. pdfShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
  316. pdfShapeData.Note = NoteTextBox.Text;
  317. return pdfShapeData;
  318. }
  319. else
  320. {
  321. CPDFLineShapeData pdfLineShapeData = new CPDFLineShapeData();
  322. pdfLineShapeData.AnnotationType = currentAnnotationType;
  323. pdfLineShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  324. pdfLineShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  325. pdfLineShapeData.LineType = CPDFArrowControl.LineType;
  326. pdfLineShapeData.Thickness = CPDFThicknessControl.Thickness;
  327. pdfLineShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
  328. pdfLineShapeData.LineType = CPDFArrowControl.LineType;
  329. pdfLineShapeData.Note = NoteTextBox.Text;
  330. return pdfLineShapeData;
  331. }
  332. }
  333. public void SetPresentAnnotAttrib(AnnotParam param,CPDFAnnotation annot,PDFViewControl view)
  334. {
  335. this.annotParam = null;
  336. this.annotCore = null;
  337. this.viewControl = null;
  338. if(param != null)
  339. {
  340. if(param is SquareParam)
  341. {
  342. SquareParam squareParam= (SquareParam)param;
  343. CPDFThicknessControl.Thickness = (int)squareParam.LineWidth;
  344. if (squareParam.LineColor != null)
  345. {
  346. BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  347. squareParam.LineColor[0],
  348. squareParam.LineColor[1],
  349. squareParam.LineColor[2]));
  350. BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
  351. squareParam.LineColor[0],
  352. squareParam.LineColor[1],
  353. squareParam.LineColor[2]));
  354. }
  355. if (squareParam.BgColor!=null)
  356. {
  357. FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
  358. squareParam.BgColor[0],
  359. squareParam.BgColor[1],
  360. squareParam.BgColor[2]));
  361. }
  362. if (squareParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  363. {
  364. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  365. }
  366. else
  367. {
  368. CPDFLineStyleControl.DashStyle = DashStyles.Dash;
  369. }
  370. }
  371. if(param is CircleParam)
  372. {
  373. CircleParam circleParam= (CircleParam)param;
  374. CPDFThicknessControl.Thickness = (int)circleParam.LineWidth;
  375. if (circleParam.LineColor != null)
  376. {
  377. BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  378. circleParam.LineColor[0],
  379. circleParam.LineColor[1],
  380. circleParam.LineColor[2]));
  381. BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
  382. circleParam.LineColor[0],
  383. circleParam.LineColor[1],
  384. circleParam.LineColor[2]));
  385. }
  386. if (circleParam.BgColor!=null)
  387. {
  388. FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
  389. circleParam.BgColor[0],
  390. circleParam.BgColor[1],
  391. circleParam.BgColor[2]));
  392. }
  393. if (circleParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  394. {
  395. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  396. }
  397. else
  398. {
  399. CPDFLineStyleControl.DashStyle = DashStyles.Dash;
  400. }
  401. }
  402. if(param is LineParam)
  403. {
  404. LineParam lineParam= (LineParam)param;
  405. CPDFThicknessControl.Thickness = (int)lineParam.LineWidth;
  406. if (lineParam.LineColor != null)
  407. {
  408. BorderColorPickerControl.Brush = new SolidColorBrush(Color.FromRgb(
  409. lineParam.LineColor[0],
  410. lineParam.LineColor[1],
  411. lineParam.LineColor[2]));
  412. BorderColorPickerControl.SetCheckedForColor(Color.FromRgb(
  413. lineParam.LineColor[0],
  414. lineParam.LineColor[1],
  415. lineParam.LineColor[2]));
  416. }
  417. if (lineParam.BgColor != null)
  418. {
  419. FillColorPickerControl.SetCheckedForColor(Color.FromRgb(
  420. lineParam.BgColor[0],
  421. lineParam.BgColor[1],
  422. lineParam.BgColor[2]));
  423. }
  424. if(lineParam.BorderStyle==C_BORDER_STYLE.BS_SOLID)
  425. {
  426. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  427. }
  428. else
  429. {
  430. CPDFLineStyleControl.DashStyle = DashStyles.Dash;
  431. }
  432. LineType lineType = new LineType()
  433. {
  434. HeadLineType = lineParam.HeadLineType,
  435. TailLineType = lineParam.TailLineType
  436. };
  437. CPDFArrowControl.LineType = lineType;
  438. }
  439. CPDFOpacityControl.OpacityValue = (int)(param.Transparency/255 * 100);
  440. NoteTextBox.Text = param.Content;
  441. }
  442. this.annotParam = param;
  443. this.annotCore = annot;
  444. this.viewControl = view;
  445. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  446. }
  447. public void InitWhenRectAndRound()
  448. {
  449. FillColorStackPanel.Visibility = Visibility.Visible;
  450. ArrowStackPanel.Visibility = Visibility.Collapsed;
  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. FillColorPickerControl.ColorChanged -= FillColorPickerControl_ColorChanged;
  460. LineType lineType;
  461. if (currentAnnotationType == CPDFAnnotationType.Arrow)
  462. {
  463. lineType = new LineType()
  464. {
  465. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  466. TailLineType = C_LINE_TYPE.LINETYPE_ARROW
  467. };
  468. }
  469. else
  470. {
  471. lineType = new LineType()
  472. {
  473. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  474. TailLineType = C_LINE_TYPE.LINETYPE_NONE
  475. };
  476. }
  477. CPDFArrowControl.LineType = lineType;
  478. }
  479. public void InitWithAnnotationType(CPDFAnnotationType annotationType)
  480. {
  481. currentAnnotationType = annotationType;
  482. switch (annotationType)
  483. {
  484. case CPDFAnnotationType.Square:
  485. TitleTextBlock.Text = "Rectangle";
  486. InitWhenRectAndRound();
  487. break;
  488. case CPDFAnnotationType.Circle:
  489. TitleTextBlock.Text = "Circle";
  490. InitWhenRectAndRound();
  491. break;
  492. case CPDFAnnotationType.Arrow:
  493. TitleTextBlock.Text = "Arrow";
  494. InitWhenArrowAndLine();
  495. break;
  496. case CPDFAnnotationType.Line:
  497. TitleTextBlock.Text = "Line";
  498. InitWhenArrowAndLine();
  499. break;
  500. default:
  501. throw new ArgumentException("Not Excepted Argument");
  502. }
  503. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  504. }
  505. }
  506. }