CPDFAnnotationControl.xaml.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. using ComPDFKit.Controls.Data;
  2. using ComPDFKit.Controls.PDFControlUI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using ComPDFKit.Controls.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  9. using System.IO;
  10. using System.Windows.Ink;
  11. using ComPDFKit.Controls.Annotation.PDFAnnotationUI;
  12. using Microsoft.Win32;
  13. using System.Windows.Media;
  14. using ComPDFKit.Controls.Properties;
  15. using ComPDFKit.PDFAnnotation;
  16. using ComPDFKit.Tool;
  17. using ComPDFKit.Tool.Help;
  18. using ComPDFKit.Tool.SettingParam;
  19. using ComPDFKit.Import;
  20. using ComPDFKit.PDFDocument;
  21. using ComPDFKitViewer.BaseObject;
  22. using ComPDFKitViewer.Helper;
  23. using ComPDFKit.Viewer.Helper;
  24. namespace ComPDFKit.Controls.PDFControl
  25. {
  26. public partial class CPDFAnnotationControl : UserControl
  27. {
  28. private bool isTempPanel = false;
  29. private CPDFAnnotationType currentAnnotationType = CPDFAnnotationType.Unknown;
  30. private PDFViewControl pdfViewerControl;
  31. private UIElement annotationPanel = null;
  32. private UIElement tempAnnotationPanel = null;
  33. private CPDFMarkupUI pdfHighlightUI;
  34. private CPDFMarkupUI pdfUnderlineUI;
  35. private CPDFMarkupUI pdfSquigglyUI;
  36. private CPDFMarkupUI pdfStrikeoutUI;
  37. private CPDFShapeUI pdfSquareUI;
  38. private CPDFShapeUI pdfCircleUI;
  39. private CPDFShapeUI pdfLineUI;
  40. private CPDFShapeUI pdfArrowUI;
  41. private CPDFCloudUI pdfCloudUI;
  42. private CPDFFreehandUI pdfFreehandUI;
  43. private CPDFFreeTextUI pdfFreeTextUI;
  44. private CPDFNoteUI pdfNoteUI;
  45. private CPDFStampUI pdfStampUI;
  46. private CPDFSignatureUI pdfSignatureUI;
  47. private CPDFLinkUI pdfLinkUI;
  48. private bool disableClean;
  49. public event EventHandler ClearAnnotationBar;
  50. public CPDFAnnotationControl()
  51. {
  52. InitializeComponent();
  53. }
  54. public void SetPDFViewer(PDFViewControl pdfViewer)
  55. {
  56. if (this.pdfViewerControl != null)
  57. {
  58. UnLoadPDFViewHandler();
  59. }
  60. this.pdfViewerControl = pdfViewer;
  61. LoadPDFViewHandler();
  62. }
  63. public void LoadPDFViewHandler()
  64. {
  65. if (pdfViewerControl != null)
  66. {
  67. pdfViewerControl.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  68. pdfViewerControl.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  69. pdfViewerControl.MouseLeftButtonUpHandler -= PdfViewerControl_MouseLeftButtonUpHandler;
  70. pdfViewerControl.MouseLeftButtonUpHandler += PdfViewerControl_MouseLeftButtonUpHandler;
  71. pdfViewerControl.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
  72. pdfViewerControl.MouseRightButtonDownHandler += PDFViewControl_MouseRightButtonDownHandler;
  73. }
  74. }
  75. public void UnLoadPDFViewHandler()
  76. {
  77. if (pdfViewerControl != null)
  78. {
  79. pdfViewerControl.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  80. pdfViewerControl.MouseLeftButtonUpHandler -= PdfViewerControl_MouseLeftButtonUpHandler;
  81. pdfViewerControl.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
  82. }
  83. }
  84. private void PDFViewControl_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  85. {
  86. //throw new NotImplementedException();
  87. }
  88. private void PdfViewerControl_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  89. {
  90. if (e.IsCreate)
  91. {
  92. if (currentAnnotationType == CPDFAnnotationType.Image || currentAnnotationType == CPDFAnnotationType.Stamp || currentAnnotationType == CPDFAnnotationType.Signature)
  93. {
  94. pdfViewerControl.SetToolType(ToolType.Pan);
  95. pdfViewerControl.SetIsVisibleCustomMouse(false);
  96. pdfViewerControl.SetIsShowStampMouse(false);
  97. }
  98. }
  99. }
  100. private void AnnotationControl_Loaded(object sender, RoutedEventArgs e)
  101. {
  102. LoadPDFViewHandler();
  103. }
  104. private void AnnotationControl_Unloaded(object sender, RoutedEventArgs e)
  105. {
  106. UnLoadPDFViewHandler();
  107. }
  108. private UIElement GetAnnotationPanel()
  109. {
  110. return AnnotationPanelContainer.Child;
  111. }
  112. private void SetAnnotationPanel(UIElement newChild)
  113. {
  114. AnnotationPanelContainer.Child = newChild;
  115. }
  116. private void ExpandPanel()
  117. {
  118. AnnotationPanelContainer.Visibility = Visibility.Visible;
  119. }
  120. private void ChangeAnnotationData()
  121. {
  122. switch (currentAnnotationType)
  123. {
  124. case CPDFAnnotationType.Highlight:
  125. case CPDFAnnotationType.Underline:
  126. case CPDFAnnotationType.Squiggly:
  127. case CPDFAnnotationType.Strikeout:
  128. (annotationPanel as CPDFMarkupUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  129. (annotationPanel as CPDFMarkupUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  130. SetAnnotationProperty((annotationPanel as CPDFMarkupUI).GetMarkupData());
  131. break;
  132. case CPDFAnnotationType.Square:
  133. case CPDFAnnotationType.Circle:
  134. case CPDFAnnotationType.Line:
  135. case CPDFAnnotationType.Arrow:
  136. (annotationPanel as CPDFShapeUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  137. (annotationPanel as CPDFShapeUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  138. SetAnnotationProperty((annotationPanel as CPDFShapeUI).GetShapeData());
  139. break;
  140. case CPDFAnnotationType.Polygon:
  141. (annotationPanel as CPDFCloudUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  142. (annotationPanel as CPDFCloudUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  143. SetAnnotationProperty((annotationPanel as CPDFCloudUI).GetPolygonData());
  144. break;
  145. case CPDFAnnotationType.Note:
  146. (annotationPanel as CPDFNoteUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  147. (annotationPanel as CPDFNoteUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  148. SetAnnotationProperty((annotationPanel as CPDFNoteUI).GetNoteData());
  149. break;
  150. case CPDFAnnotationType.Freehand:
  151. (annotationPanel as CPDFFreehandUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  152. (annotationPanel as CPDFFreehandUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  153. SetAnnotationProperty((annotationPanel as CPDFFreehandUI).GetFreehandData());
  154. break;
  155. case CPDFAnnotationType.FreeText:
  156. (annotationPanel as CPDFFreeTextUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  157. (annotationPanel as CPDFFreeTextUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  158. SetAnnotationProperty((annotationPanel as CPDFFreeTextUI).GetFreeTextData());
  159. break;
  160. case CPDFAnnotationType.Stamp:
  161. (annotationPanel as CPDFStampUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  162. (annotationPanel as CPDFStampUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  163. break;
  164. case CPDFAnnotationType.Signature:
  165. (annotationPanel as CPDFSignatureUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  166. (annotationPanel as CPDFSignatureUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. private void SetAnnotationProperty(CPDFAnnotationData pdfAnnotationData = null)
  173. {
  174. if (pdfAnnotationData == null)
  175. {
  176. pdfViewerControl.SetToolType(ToolType.Pan);
  177. pdfViewerControl.SetIsShowStampMouse(false);
  178. pdfViewerControl.SetIsVisibleCustomMouse(false);
  179. return;
  180. }
  181. CPDFAnnotationType annotationType = pdfAnnotationData.AnnotationType;
  182. AnnotParam annotHandlerEventArgs = null;
  183. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  184. switch (annotationType)
  185. {
  186. case CPDFAnnotationType.Highlight:
  187. {
  188. CPDFMarkupData highlightData = pdfAnnotationData as CPDFMarkupData;
  189. annotHandlerEventArgs = new HighlightParam();
  190. byte[] Color = new byte[] { highlightData.Color.R, highlightData.Color.G, highlightData.Color.B };
  191. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT;
  192. (annotHandlerEventArgs as HighlightParam).HighlightColor = Color;
  193. (annotHandlerEventArgs as HighlightParam).Transparency = Convert.ToByte(highlightData.Opacity * 255);
  194. (annotHandlerEventArgs as HighlightParam).Content = highlightData.Note;
  195. (annotHandlerEventArgs as HighlightParam).Author = CPDFMarkupData.Author;
  196. (annotHandlerEventArgs as HighlightParam).Locked = highlightData.IsLocked;
  197. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT);
  198. }
  199. break;
  200. case CPDFAnnotationType.Underline:
  201. {
  202. CPDFMarkupData underlineData = pdfAnnotationData as CPDFMarkupData;
  203. annotHandlerEventArgs = new UnderlineParam();
  204. byte[] Color = new byte[] { underlineData.Color.R, underlineData.Color.G, underlineData.Color.B };
  205. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE;
  206. (annotHandlerEventArgs as UnderlineParam).UnderlineColor = Color;
  207. (annotHandlerEventArgs as UnderlineParam).Transparency = Convert.ToByte(underlineData.Opacity * 255);
  208. (annotHandlerEventArgs as UnderlineParam).Author = CPDFMarkupData.Author;
  209. (annotHandlerEventArgs as UnderlineParam).Content = underlineData.Note;
  210. (annotHandlerEventArgs as UnderlineParam).Locked = underlineData.IsLocked;
  211. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE);
  212. }
  213. break;
  214. case CPDFAnnotationType.Strikeout:
  215. {
  216. CPDFMarkupData strikeoutData = pdfAnnotationData as CPDFMarkupData;
  217. annotHandlerEventArgs = new StrikeoutParam();
  218. byte[] Color = new byte[] { strikeoutData.Color.R, strikeoutData.Color.G, strikeoutData.Color.B };
  219. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT;
  220. (annotHandlerEventArgs as StrikeoutParam).StrikeoutColor = Color;
  221. (annotHandlerEventArgs as StrikeoutParam).Transparency = Convert.ToByte(strikeoutData.Opacity * 255);
  222. (annotHandlerEventArgs as StrikeoutParam).Locked = strikeoutData.IsLocked;
  223. (annotHandlerEventArgs as StrikeoutParam).Author = CPDFMarkupData.Author;
  224. (annotHandlerEventArgs as StrikeoutParam).Content = strikeoutData.Note;
  225. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT);
  226. }
  227. break;
  228. case CPDFAnnotationType.Squiggly:
  229. {
  230. CPDFMarkupData squigglyData = pdfAnnotationData as CPDFMarkupData;
  231. annotHandlerEventArgs = new SquigglyParam();
  232. byte[] Color = new byte[] { squigglyData.Color.R, squigglyData.Color.G, squigglyData.Color.B };
  233. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY;
  234. (annotHandlerEventArgs as SquigglyParam).SquigglyColor = Color;
  235. (annotHandlerEventArgs as SquigglyParam).Transparency = Convert.ToByte(squigglyData.Opacity * 255);
  236. (annotHandlerEventArgs as SquigglyParam).Locked = squigglyData.IsLocked;
  237. (annotHandlerEventArgs as SquigglyParam).Author = CPDFMarkupData.Author;
  238. (annotHandlerEventArgs as SquigglyParam).Content = squigglyData.Note;
  239. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY);
  240. }
  241. break;
  242. case CPDFAnnotationType.Square:
  243. {
  244. CPDFShapeData squareData = pdfAnnotationData as CPDFShapeData;
  245. annotHandlerEventArgs = new SquareParam()
  246. {
  247. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE
  248. };
  249. byte[] LineColor = new byte[] { squareData.BorderColor.R, squareData.BorderColor.G, squareData.BorderColor.B };
  250. (annotHandlerEventArgs as SquareParam).LineColor = LineColor;
  251. if (squareData.FillColor != Colors.Transparent)
  252. {
  253. byte[] FillColor = new byte[] { squareData.FillColor.R, squareData.FillColor.G, squareData.FillColor.B };
  254. (annotHandlerEventArgs as SquareParam).BgColor = FillColor;
  255. (annotHandlerEventArgs as SquareParam).HasBgColor = true;
  256. }
  257. (annotHandlerEventArgs as SquareParam).LineWidth = squareData.Thickness;
  258. (annotHandlerEventArgs as SquareParam).Transparency = Convert.ToByte(squareData.Opacity * 255);
  259. ParamConverter.ParseDashStyle(squareData.DashStyle, out float[] LineDash, out C_BORDER_STYLE BorderStyle);
  260. (annotHandlerEventArgs as SquareParam).LineDash = LineDash;
  261. (annotHandlerEventArgs as SquareParam).BorderStyle = BorderStyle;
  262. (annotHandlerEventArgs as SquareParam).Author = CPDFMarkupData.Author;
  263. (annotHandlerEventArgs as SquareParam).Content = squareData.Note;
  264. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE);
  265. }
  266. break;
  267. case CPDFAnnotationType.Circle:
  268. {
  269. CPDFShapeData circleData = pdfAnnotationData as CPDFShapeData;
  270. byte[] LineColor = new byte[] { circleData.BorderColor.R, circleData.BorderColor.G, circleData.BorderColor.B };
  271. annotHandlerEventArgs = new CircleParam
  272. {
  273. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE
  274. };
  275. if (annotHandlerEventArgs is CircleParam circleParam)
  276. {
  277. circleParam.LineColor = LineColor;
  278. if (circleData.FillColor != Colors.Transparent)
  279. {
  280. byte[] BgColor = new byte[] { circleData.FillColor.R, circleData.FillColor.G, circleData.FillColor.B };
  281. circleParam.BgColor = BgColor;
  282. circleParam.HasBgColor = true;
  283. }
  284. circleParam.LineWidth = circleData.Thickness;
  285. circleParam.Transparency = Convert.ToByte(circleData.Opacity * 255);
  286. ParamConverter.ParseDashStyle(circleData.DashStyle, out float[] LineDash, out C_BORDER_STYLE BorderStyle);
  287. circleParam.LineDash = LineDash;
  288. circleParam.BorderStyle = BorderStyle;
  289. circleParam.Author = CPDFMarkupData.Author;
  290. circleParam.Content = circleData.Note;
  291. }
  292. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE);
  293. }
  294. break;
  295. case CPDFAnnotationType.Polygon:
  296. {
  297. CPDFPolygonData polygonData = pdfAnnotationData as CPDFPolygonData;
  298. annotHandlerEventArgs = new PolygonMeasureParam()
  299. {
  300. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  301. };
  302. if (annotHandlerEventArgs is PolygonMeasureParam polygonMeasureParam)
  303. {
  304. polygonMeasureParam.LineColor = new byte[] { polygonData.BorderColor.R, polygonData.BorderColor.G, polygonData.BorderColor.B };
  305. polygonMeasureParam.HasFillColor = true;
  306. polygonMeasureParam.FillColor = new byte[] { polygonData.FillColor.R, polygonData.FillColor.G, polygonData.FillColor.B };
  307. polygonMeasureParam.LineWidth = polygonData.Thickness;
  308. polygonMeasureParam.IsMeasure = polygonData.IsMeasured;
  309. polygonMeasureParam.Transparency = 255;
  310. polygonMeasureParam.IsMeasure = false;
  311. }
  312. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON);
  313. }
  314. break;
  315. case CPDFAnnotationType.Arrow:
  316. case CPDFAnnotationType.Line:
  317. {
  318. CPDFLineShapeData lineData = pdfAnnotationData as CPDFLineShapeData;
  319. annotHandlerEventArgs = new LineParam()
  320. {
  321. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINE
  322. };
  323. if (annotHandlerEventArgs is LineParam lineParam)
  324. {
  325. byte[] LineColor = new byte[] { lineData.BorderColor.R, lineData.BorderColor.G, lineData.BorderColor.B };
  326. lineParam.LineColor = LineColor;
  327. lineParam.LineWidth = lineData.Thickness;
  328. lineParam.Transparency = Convert.ToByte(lineData.Opacity * 255);
  329. ParamConverter.ParseDashStyle(lineData.DashStyle, out float[] LineDash, out C_BORDER_STYLE BorderStyle);
  330. lineParam.LineDash = LineDash;
  331. lineParam.BorderStyle = BorderStyle;
  332. lineParam.HeadLineType = lineData.LineType.HeadLineType;
  333. lineParam.TailLineType = lineData.LineType.TailLineType;
  334. lineParam.Author = CPDFMarkupData.Author;
  335. lineParam.Content = lineData.Note;
  336. }
  337. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINE);
  338. }
  339. break;
  340. case CPDFAnnotationType.Note:
  341. {
  342. CPDFNoteData noteData = pdfAnnotationData as CPDFNoteData;
  343. annotHandlerEventArgs = new StickyNoteParam();
  344. if (annotHandlerEventArgs is StickyNoteParam stickyNoteParam)
  345. {
  346. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_TEXT;
  347. byte[] StickyNoteColor = new byte[] { noteData.BorderColor.R, noteData.BorderColor.G, noteData.BorderColor.B };
  348. stickyNoteParam.StickyNoteColor = StickyNoteColor;
  349. stickyNoteParam.Content = noteData.Note;
  350. stickyNoteParam.Transparency = 255;
  351. stickyNoteParam.Author = CPDFMarkupData.Author;
  352. }
  353. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT);
  354. }
  355. break;
  356. case CPDFAnnotationType.Freehand:
  357. {
  358. CPDFFreehandData freehandData = pdfAnnotationData as CPDFFreehandData;
  359. annotHandlerEventArgs = new InkParam()
  360. {
  361. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_INK
  362. };
  363. if (annotHandlerEventArgs is InkParam inkParam)
  364. {
  365. byte[] LineColor = new byte[] { freehandData.BorderColor.R, freehandData.BorderColor.G, freehandData.BorderColor.B };
  366. inkParam.InkColor = LineColor;
  367. inkParam.Thickness = freehandData.Thickness;
  368. inkParam.Transparency = Convert.ToByte(freehandData.Opacity * 255);
  369. inkParam.Content = freehandData.Note;
  370. inkParam.Author = CPDFMarkupData.Author;
  371. }
  372. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  373. }
  374. break;
  375. case CPDFAnnotationType.FreeText:
  376. {
  377. CPDFFreeTextData freeTextData = pdfAnnotationData as CPDFFreeTextData;
  378. annotHandlerEventArgs = new FreeTextParam()
  379. {
  380. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT
  381. };
  382. if (annotHandlerEventArgs is FreeTextParam freeTextParam)
  383. {
  384. freeTextParam.Transparency = Convert.ToByte(freeTextData.Opacity * 255);
  385. freeTextParam.FontName = freeTextData.FontFamily.ToString();
  386. byte[] FontColor = new byte[] { freeTextData.BorderColor.R, freeTextData.BorderColor.G, freeTextData.BorderColor.B };
  387. freeTextParam.FontColor = FontColor;
  388. freeTextParam.FontSize = freeTextData.FontSize;
  389. string postScriptName = string.Empty;
  390. CPDFFont.GetPostScriptName(pdfFreeTextUI.CPDFFontControl.FontFamilyValue, pdfFreeTextUI.CPDFFontControl.FontStyleValue, ref postScriptName);
  391. freeTextParam.FontName = postScriptName;
  392. switch (freeTextData.TextAlignment)
  393. {
  394. case TextAlignment.Left:
  395. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_LEFT;
  396. break;
  397. case TextAlignment.Right:
  398. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT;
  399. break;
  400. case TextAlignment.Center:
  401. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_CENTER;
  402. break;
  403. default:
  404. break;
  405. }
  406. freeTextParam.Content = freeTextData.Note;
  407. freeTextParam.Author = CPDFMarkupData.Author;
  408. }
  409. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT);
  410. }
  411. break;
  412. case CPDFAnnotationType.Stamp:
  413. {
  414. StampParam stampParam = new StampParam()
  415. {
  416. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP
  417. };
  418. if(pdfAnnotationData is CPDFStampData stampData)
  419. {
  420. SetStamp(ref stampParam, stampData);
  421. annotHandlerEventArgs = stampParam;
  422. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  423. byte[] imageData = null;
  424. int imageWidth = 0;
  425. int imageHeight = 0;
  426. PDFHelp.ImageStreamToByte(stampParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  427. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  428. {
  429. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  430. pdfViewerControl.SetStampMouseImage(imageData, imageWidth, imageHeight);
  431. }
  432. }
  433. pdfViewerControl.SetIsVisibleCustomMouse(true);
  434. pdfViewerControl.SetIsShowStampMouse(true);
  435. }
  436. break;
  437. case CPDFAnnotationType.Signature:
  438. {
  439. AnnotParam signatureParam = new AnnotParam();
  440. CPDFSignatureData SignatureData = pdfAnnotationData as CPDFSignatureData;
  441. SetSignature(ref signatureParam, SignatureData);
  442. switch (signatureParam.CurrentType)
  443. {
  444. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  445. {
  446. StampParam stampParam = (StampParam)signatureParam;
  447. stampParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  448. byte[] imageData = null;
  449. int imageWidth = 0;
  450. int imageHeight = 0;
  451. PDFHelp.ImageStreamToByte(stampParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  452. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  453. {
  454. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  455. pdfViewerControl.SetStampMouseImage(imageData, imageWidth, imageHeight);
  456. pdfViewerControl.SetIsVisibleCustomMouse(true);
  457. pdfViewerControl.SetIsShowStampMouse(true);
  458. annotHandlerEventArgs = signatureParam;
  459. }
  460. }
  461. break;
  462. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  463. {
  464. WriteableBitmap writeableBitmap = CreateInkImage(signatureParam as InkParam);
  465. byte[] imageArray = new byte[writeableBitmap.PixelWidth * writeableBitmap.PixelHeight * 4];
  466. writeableBitmap.CopyPixels(imageArray, writeableBitmap.PixelWidth * 4, 0);
  467. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
  468. pdfViewerControl.SetStampMouseImage(imageArray, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
  469. pdfViewerControl.SetIsVisibleCustomMouse(true);
  470. pdfViewerControl.SetIsShowStampMouse(true);
  471. annotHandlerEventArgs = signatureParam;
  472. }
  473. break;
  474. default:
  475. return;
  476. }
  477. }
  478. break;
  479. case CPDFAnnotationType.Link:
  480. if (annotHandlerEventArgs != null)
  481. {
  482. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINK;
  483. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINK);
  484. }
  485. break;
  486. case CPDFAnnotationType.Unknown:
  487. pdfViewerControl.SetToolType(ToolType.Pan);
  488. return;
  489. default:
  490. break;
  491. }
  492. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  493. if (annotationType != CPDFAnnotationType.Stamp && annotationType != CPDFAnnotationType.Signature && annotationType != CPDFAnnotationType.Image)
  494. {
  495. pdfViewerControl.SetIsShowStampMouse(false);
  496. }
  497. pdfViewerControl.SetAnnotParam(annotHandlerEventArgs);
  498. }
  499. private WriteableBitmap CreateInkImage(InkParam inkParam)
  500. {
  501. if (inkParam == null)
  502. {
  503. return null;
  504. }
  505. if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
  506. {
  507. GeometryGroup PaintGeomtry = new GeometryGroup();
  508. int minLeft = -1;
  509. int minTop = -1;
  510. int maxLeft = -1;
  511. int maxTop = -1;
  512. foreach (List<CPoint> Item in inkParam.InkPath)
  513. {
  514. for (int i = 0; i < Item.Count; i++)
  515. {
  516. CPoint paintPoint = Item[i];
  517. if (minLeft == -1)
  518. {
  519. minLeft = (int)paintPoint.x;
  520. maxLeft = (int)paintPoint.x;
  521. minTop = (int)paintPoint.y;
  522. maxTop = (int)paintPoint.y;
  523. }
  524. else
  525. {
  526. minLeft = (int)Math.Min(minLeft, paintPoint.x);
  527. maxLeft = (int)Math.Max(maxLeft, paintPoint.x);
  528. minTop = (int)Math.Min(minTop, paintPoint.y);
  529. maxTop = (int)Math.Max(maxTop, paintPoint.y);
  530. }
  531. }
  532. }
  533. if (minLeft >= 0 && maxLeft > minLeft && minTop >= 0 && maxTop > minTop)
  534. {
  535. List<List<CPoint>> points = new List<List<CPoint>>();
  536. foreach (List<CPoint> Item in inkParam.InkPath)
  537. {
  538. PathGeometry PaintPath = new PathGeometry();
  539. PathFigureCollection Figures = new PathFigureCollection();
  540. PathFigure AddFigure = new PathFigure();
  541. Figures.Add(AddFigure);
  542. PaintPath.Figures = Figures;
  543. PaintGeomtry.Children.Add(PaintPath);
  544. List<CPoint> changeList = new List<CPoint>();
  545. for (int i = 0; i < Item.Count; i++)
  546. {
  547. Point paintPoint = new Point(DpiHelper.PDFNumToStandardNum(Item[i].x - minLeft), DpiHelper.PDFNumToStandardNum(Item[i].y - minTop));
  548. changeList.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(paintPoint)));
  549. if (i == 0)
  550. {
  551. AddFigure.StartPoint = paintPoint;
  552. }
  553. else
  554. {
  555. LineSegment AddSegment = new LineSegment();
  556. AddSegment.Point = paintPoint;
  557. AddFigure.Segments.Add(AddSegment);
  558. }
  559. }
  560. if (changeList.Count > 0)
  561. {
  562. points.Add(changeList);
  563. }
  564. }
  565. int drawWidth = (int)DpiHelper.PDFNumToStandardNum(maxLeft - minLeft);
  566. int drawHeight = (int)DpiHelper.PDFNumToStandardNum(maxTop - minTop);
  567. inkParam.InkPath = points;
  568. DefaultSettingParam defaultSettingParam = pdfViewerControl.PDFViewTool.GetDefaultSettingParam();
  569. defaultSettingParam.SetAnnotParam(inkParam);
  570. DrawingVisual copyVisual = new DrawingVisual();
  571. DrawingContext copyContext = copyVisual.RenderOpen();
  572. Color color = ParamConverter.ConverterByteForColor(inkParam.InkColor);
  573. color.A = inkParam.Transparency;
  574. Pen drawPen = new Pen(new SolidColorBrush(color), inkParam.Thickness);
  575. copyContext?.DrawGeometry(null, drawPen, PaintGeomtry);
  576. copyContext.Close();
  577. RenderTargetBitmap targetBitmap = new RenderTargetBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Pbgra32);
  578. targetBitmap.Render(copyVisual);
  579. byte[] ImageArray = new byte[targetBitmap.PixelWidth * targetBitmap.PixelHeight * 4];
  580. targetBitmap.CopyPixels(new Int32Rect(0, 0, (int)targetBitmap.PixelWidth, (int)targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  581. WriteableBitmap writeBitmap = new WriteableBitmap(targetBitmap.PixelWidth, targetBitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
  582. writeBitmap.WritePixels(new Int32Rect(0, 0, targetBitmap.PixelWidth, targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  583. return writeBitmap;
  584. }
  585. }
  586. return null;
  587. }
  588. public void SetSignature(ref AnnotParam annotParam, CPDFSignatureData stamp)
  589. {
  590. switch (stamp.Type)
  591. {
  592. case SignatureType.TextType:
  593. case SignatureType.ImageType:
  594. {
  595. annotParam = new StampParam();
  596. annotParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  597. (annotParam as StampParam).Transparency = 255;
  598. (annotParam as StampParam).StampType = C_STAMP_TYPE.IMAGE_STAMP;
  599. if (!string.IsNullOrEmpty(stamp.SourcePath))
  600. {
  601. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  602. PngBitmapEncoder encoder = new PngBitmapEncoder();
  603. encoder.Frames.Add(BitmapFrame.Create(image));
  604. MemoryStream memoryStream = new MemoryStream();
  605. encoder.Save(memoryStream);
  606. (annotParam as StampParam).ImageStream = memoryStream;
  607. }
  608. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  609. }
  610. break;
  611. case SignatureType.Drawing:
  612. {
  613. annotParam = new InkParam();
  614. (annotParam as InkParam).Transparency = stamp.inkColor.A;
  615. annotParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_INK;
  616. List<List<Point>> RawPointList = GetPoints(stamp.DrawingPath);
  617. if (RawPointList != null && RawPointList.Count > 0)
  618. {
  619. List<List<CPoint>> inkPath = new List<List<CPoint>>();
  620. foreach (List<Point> inkPoints in RawPointList)
  621. {
  622. List<CPoint> ink = new List<CPoint>();
  623. foreach (Point point in inkPoints)
  624. {
  625. ink.Add(new CPoint((float)DpiHelper.StandardNumToPDFNum(point.X), (float)DpiHelper.StandardNumToPDFNum(point.Y)));
  626. }
  627. if (ink.Count > 0)
  628. {
  629. inkPath.Add(ink);
  630. }
  631. }
  632. if (inkPath.Count > 0)
  633. {
  634. (annotParam as InkParam).InkPath = inkPath;
  635. }
  636. }
  637. (annotParam as InkParam).Thickness = stamp.inkThickness;
  638. byte[] InkColor = new byte[] { stamp.inkColor.R, stamp.inkColor.G, stamp.inkColor.B };
  639. (annotParam as InkParam).InkColor = InkColor;
  640. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  641. }
  642. break;
  643. default:
  644. break;
  645. }
  646. }
  647. private List<List<Point>> GetPoints(string Path)
  648. {
  649. StrokeCollection Strokes;
  650. List<List<Point>> RawPointList = new List<List<Point>>();
  651. using (FileStream strokeStream = File.OpenRead(Path))
  652. {
  653. Strokes = new StrokeCollection(strokeStream);
  654. }
  655. for (int kk = 0; kk < Strokes.Count; kk++)
  656. {
  657. List<Point> p = new List<Point>();
  658. RawPointList.Add(p);
  659. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  660. {
  661. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  662. if (point.X >= 0 && point.Y >= 0)
  663. RawPointList[kk].Add(point);
  664. }
  665. }
  666. return RawPointList;
  667. }
  668. private void SetStamp(ref StampParam stampParam, CPDFStampData stamp)
  669. {
  670. stampParam.StampText = stamp.StampText;
  671. stampParam.Author = CPDFMarkupData.Author;
  672. stampParam.Transparency = Convert.ToByte(stamp.Opacity * 255);
  673. stampParam.DateText = stamp.StampTextDate;
  674. stampParam.TextStampColor = stamp.TextColor;
  675. stampParam.TextStampShape = stamp.TextSharp;
  676. if (!string.IsNullOrEmpty(stamp.SourcePath))
  677. {
  678. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  679. PngBitmapEncoder encoder = new PngBitmapEncoder();
  680. encoder.Frames.Add(BitmapFrame.Create(image));
  681. MemoryStream memoryStream = new MemoryStream();
  682. encoder.Save(memoryStream);
  683. stampParam.ImageStream = memoryStream;
  684. }
  685. else
  686. {
  687. var i = pdfStampUI.CustomStampList.IndexOf(stamp);
  688. Settings.Default.CustomStampList.RemoveAt(i);
  689. Settings.Default.Save();
  690. pdfStampUI.LoadSettings();
  691. return;
  692. }
  693. stampParam.StampType = stamp.Type;
  694. }
  695. public void AnnotationCancel()
  696. {
  697. this.pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_NONE);
  698. this.pdfViewerControl.SetToolType(ToolType.Pan);
  699. pdfViewerControl.SetIsShowStampMouse(false);
  700. pdfViewerControl.SetIsVisibleCustomMouse(false);
  701. ClearPanel();
  702. }
  703. private void CPDFAnnotationControl_PropertyChanged(object sender, CPDFAnnotationData e)
  704. {
  705. SetAnnotationProperty(e);
  706. }
  707. public void InitAnnotationPanel(CPDFAnnotationType annotationType)
  708. {
  709. switch (annotationType)
  710. {
  711. case CPDFAnnotationType.Highlight:
  712. if (pdfHighlightUI == null)
  713. {
  714. pdfHighlightUI = new CPDFMarkupUI();
  715. }
  716. annotationPanel = pdfHighlightUI;
  717. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  718. break;
  719. case CPDFAnnotationType.Underline:
  720. if (pdfUnderlineUI == null)
  721. {
  722. pdfUnderlineUI = new CPDFMarkupUI();
  723. }
  724. annotationPanel = pdfUnderlineUI;
  725. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  726. break;
  727. case CPDFAnnotationType.Strikeout:
  728. if (pdfStrikeoutUI == null)
  729. {
  730. pdfStrikeoutUI = new CPDFMarkupUI();
  731. }
  732. annotationPanel = pdfStrikeoutUI;
  733. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  734. break;
  735. case CPDFAnnotationType.Squiggly:
  736. if (pdfSquigglyUI == null)
  737. {
  738. pdfSquigglyUI = new CPDFMarkupUI();
  739. }
  740. annotationPanel = pdfSquigglyUI;
  741. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  742. break;
  743. case CPDFAnnotationType.Square:
  744. if (pdfSquareUI == null)
  745. {
  746. pdfSquareUI = new CPDFShapeUI();
  747. }
  748. annotationPanel = pdfSquareUI;
  749. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  750. break;
  751. case CPDFAnnotationType.Circle:
  752. if (pdfCircleUI == null)
  753. {
  754. pdfCircleUI = new CPDFShapeUI();
  755. }
  756. annotationPanel = pdfCircleUI;
  757. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  758. break;
  759. case CPDFAnnotationType.Polygon:
  760. if (pdfCloudUI == null)
  761. {
  762. pdfCloudUI = new CPDFCloudUI();
  763. }
  764. annotationPanel = pdfCloudUI;
  765. break;
  766. case CPDFAnnotationType.Arrow:
  767. if (pdfArrowUI == null)
  768. {
  769. pdfArrowUI = new CPDFShapeUI();
  770. }
  771. annotationPanel = pdfArrowUI;
  772. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  773. break;
  774. case CPDFAnnotationType.Line:
  775. if (pdfLineUI == null)
  776. {
  777. pdfLineUI = new CPDFShapeUI();
  778. }
  779. annotationPanel = pdfLineUI;
  780. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  781. break;
  782. case CPDFAnnotationType.Freehand:
  783. if (pdfFreehandUI == null)
  784. {
  785. pdfFreehandUI = new CPDFFreehandUI();
  786. pdfFreehandUI.EraseClickHandler -= PdfFreehandUI_EraseClickHandler;
  787. pdfFreehandUI.EraseChangeHandler -= PdfFreehandUI_EraseChangeHandler;
  788. pdfFreehandUI.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  789. pdfFreehandUI.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  790. }
  791. annotationPanel = pdfFreehandUI;
  792. break;
  793. case CPDFAnnotationType.FreeText:
  794. if (pdfFreeTextUI == null)
  795. {
  796. pdfFreeTextUI = new CPDFFreeTextUI();
  797. }
  798. annotationPanel = pdfFreeTextUI;
  799. break;
  800. case CPDFAnnotationType.Note:
  801. if (pdfNoteUI == null)
  802. {
  803. pdfNoteUI = new CPDFNoteUI();
  804. }
  805. annotationPanel = pdfNoteUI;
  806. break;
  807. case CPDFAnnotationType.Stamp:
  808. if (pdfStampUI == null)
  809. {
  810. pdfStampUI = new CPDFStampUI();
  811. }
  812. annotationPanel = pdfStampUI;
  813. break;
  814. case CPDFAnnotationType.Signature:
  815. if (pdfSignatureUI == null)
  816. {
  817. pdfSignatureUI = new CPDFSignatureUI();
  818. }
  819. annotationPanel = pdfSignatureUI;
  820. break;
  821. case CPDFAnnotationType.Link:
  822. if (pdfLinkUI == null)
  823. {
  824. pdfLinkUI = new CPDFLinkUI();
  825. }
  826. LinkParam linkAnnotArgs = new LinkParam();
  827. if (this.pdfViewerControl != null && pdfViewerControl.PDFViewTool != null && pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument() != null)
  828. {
  829. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  830. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINK);
  831. pdfLinkUI.InitLinkAnnotArgs(linkAnnotArgs, pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument().PageCount);
  832. }
  833. annotationPanel = pdfLinkUI;
  834. break;
  835. case CPDFAnnotationType.Audio:
  836. SoundParam soundParam = new SoundParam();
  837. soundParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_SOUND;
  838. OpenFileDialog openAudioDialog = new OpenFileDialog();
  839. openAudioDialog.Filter = "Wave Files(*.wav)|*.wav|All Files(*.*;)|*.*;";
  840. if (openAudioDialog.ShowDialog() == true)
  841. {
  842. BitmapImage img = new BitmapImage(new Uri("pack://application:,,,/ComPDFKit.Controls;component/Asset/Resource/SoundAnnot.png"));
  843. PngBitmapEncoder encoder = new PngBitmapEncoder();
  844. encoder.Frames.Add(BitmapFrame.Create(img));
  845. MemoryStream memoryStream = new MemoryStream();
  846. encoder.Save(memoryStream);
  847. soundParam.ImageStream = memoryStream;
  848. soundParam.SoundFilePath = openAudioDialog.FileName;
  849. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  850. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND);
  851. pdfViewerControl.SetAnnotParam(soundParam);
  852. }
  853. else
  854. {
  855. pdfViewerControl.SetToolType(ToolType.Pan);
  856. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  857. }
  858. ClearPanel();
  859. break;
  860. case CPDFAnnotationType.Image:
  861. StampParam stampParam = new StampParam();
  862. OpenFileDialog openFileDialog = new OpenFileDialog();
  863. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  864. if (openFileDialog.ShowDialog() == true)
  865. {
  866. try
  867. {
  868. using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  869. {
  870. PngBitmapEncoder encoder = new PngBitmapEncoder();
  871. encoder.Frames.Add(BitmapFrame.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None));
  872. MemoryStream memoryStream = new MemoryStream();
  873. encoder.Save(memoryStream);
  874. stampParam.ImageStream = memoryStream;
  875. stampParam.Transparency = 255;
  876. stampParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  877. stampParam.StampType = C_STAMP_TYPE.IMAGE_STAMP;
  878. BitmapFrame frame = null;
  879. if (encoder != null && encoder.Frames.Count > 0)
  880. {
  881. frame = encoder.Frames[0];
  882. }
  883. if (frame != null)
  884. {
  885. byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  886. if (frame.Format != PixelFormats.Bgra32)
  887. {
  888. FormatConvertedBitmap covert = new FormatConvertedBitmap(
  889. frame,
  890. PixelFormats.Bgra32,
  891. frame.Palette,
  892. 0);
  893. covert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  894. }
  895. else
  896. {
  897. frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  898. }
  899. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  900. pdfViewerControl.SetStampMouseImage(imageArray, frame.PixelWidth, frame.PixelHeight);
  901. }
  902. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  903. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  904. pdfViewerControl.SetIsVisibleCustomMouse(true);
  905. pdfViewerControl.SetIsShowStampMouse(true);
  906. pdfViewerControl.SetAnnotParam(stampParam);
  907. }
  908. }
  909. catch
  910. {
  911. }
  912. }
  913. else
  914. {
  915. pdfViewerControl.SetToolType(ToolType.Pan);
  916. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  917. }
  918. ClearPanel();
  919. break;
  920. default:
  921. break;
  922. }
  923. }
  924. private void PdfFreehandUI_EraseChangeHandler(object sender, double e)
  925. {
  926. if (pdfViewerControl != null)
  927. {
  928. pdfViewerControl.PDFViewTool.SetEraseZoom(e);
  929. }
  930. }
  931. private void PdfFreehandUI_EraseClickHandler(object sender, bool e)
  932. {
  933. if (e)
  934. {
  935. //pdfViewerControl.PDFToolManager.ClearSelect();
  936. pdfViewerControl.PDFToolManager.SetToolType(ToolType.Customize);
  937. }
  938. else
  939. {
  940. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  941. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  942. }
  943. }
  944. public void CreatTempAnnotationPanel(BaseAnnot baseAnnot)
  945. {
  946. switch (baseAnnot.CurrentType)
  947. {
  948. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  949. {
  950. tempAnnotationPanel = new CPDFMarkupUI();
  951. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Highlight);
  952. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  953. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  954. baseAnnot.GetAnnotData().PageIndex,
  955. baseAnnot.GetAnnotData().Annot);
  956. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  957. }
  958. break;
  959. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  960. {
  961. tempAnnotationPanel = new CPDFMarkupUI();
  962. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Underline);
  963. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  964. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  965. baseAnnot.GetAnnotData().PageIndex,
  966. baseAnnot.GetAnnotData().Annot);
  967. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  968. }
  969. break;
  970. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  971. {
  972. tempAnnotationPanel = new CPDFMarkupUI();
  973. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Strikeout);
  974. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  975. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  976. baseAnnot.GetAnnotData().PageIndex,
  977. baseAnnot.GetAnnotData().Annot);
  978. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  979. }
  980. break;
  981. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  982. {
  983. tempAnnotationPanel = new CPDFMarkupUI();
  984. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Squiggly);
  985. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  986. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  987. baseAnnot.GetAnnotData().PageIndex,
  988. baseAnnot.GetAnnotData().Annot);
  989. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  990. }
  991. break;
  992. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  993. {
  994. tempAnnotationPanel = new CPDFShapeUI();
  995. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Square);
  996. int page = baseAnnot.GetAnnotData().PageIndex;
  997. var annot = baseAnnot.GetAnnotData().Annot;
  998. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  999. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1000. baseAnnot.GetAnnotData().PageIndex,
  1001. baseAnnot.GetAnnotData().Annot);
  1002. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1003. }
  1004. break;
  1005. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  1006. {
  1007. tempAnnotationPanel = new CPDFShapeUI();
  1008. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Circle);
  1009. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1010. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1011. baseAnnot.GetAnnotData().PageIndex,
  1012. baseAnnot.GetAnnotData().Annot);
  1013. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1014. }
  1015. break;
  1016. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1017. {
  1018. tempAnnotationPanel = new CPDFShapeUI();
  1019. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Line);
  1020. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1021. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1022. baseAnnot.GetAnnotData().PageIndex,
  1023. baseAnnot.GetAnnotData().Annot);
  1024. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1025. }
  1026. break;
  1027. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1028. {
  1029. CPDFFreehandUI tempFreehandPanel = new CPDFFreehandUI();
  1030. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1031. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1032. baseAnnot.GetAnnotData().PageIndex,
  1033. baseAnnot.GetAnnotData().Annot);
  1034. tempFreehandPanel.SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1035. tempFreehandPanel.EraseClickHandler -= PdfFreehandUI_EraseClickHandler;
  1036. tempFreehandPanel.EraseChangeHandler -= PdfFreehandUI_EraseChangeHandler;
  1037. tempFreehandPanel.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  1038. tempFreehandPanel.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  1039. tempAnnotationPanel = tempFreehandPanel;
  1040. }
  1041. break;
  1042. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1043. {
  1044. tempAnnotationPanel = new CPDFFreeTextUI();
  1045. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1046. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1047. baseAnnot.GetAnnotData().PageIndex,
  1048. baseAnnot.GetAnnotData().Annot);
  1049. (tempAnnotationPanel as CPDFFreeTextUI).SetPresentAnnotAttrib(annotParam as FreeTextParam, baseAnnot.GetAnnotData().Annot as CPDFFreeTextAnnotation, pdfViewerControl);
  1050. }
  1051. break;
  1052. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  1053. {
  1054. tempAnnotationPanel = new CPDFNoteUI();
  1055. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1056. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1057. baseAnnot.GetAnnotData().PageIndex,
  1058. baseAnnot.GetAnnotData().Annot);
  1059. (tempAnnotationPanel as CPDFNoteUI).SetPresentAnnotAttrib(annotParam as StickyNoteParam, baseAnnot.GetAnnotData().Annot as CPDFTextAnnotation, pdfViewerControl);
  1060. }
  1061. break;
  1062. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1063. {
  1064. tempAnnotationPanel = new CPDFTempStampUI();
  1065. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1066. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1067. baseAnnot.GetAnnotData().PageIndex,
  1068. baseAnnot.GetAnnotData().Annot);
  1069. (tempAnnotationPanel as CPDFTempStampUI).SetPresentAnnotAttrib(annotParam as StampParam, baseAnnot.GetAnnotData().Annot as CPDFStampAnnotation, pdfViewerControl.PDFToolManager.GetDocument(), pdfViewerControl);
  1070. }
  1071. break;
  1072. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1073. {
  1074. tempAnnotationPanel = new CPDFLinkUI();
  1075. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1076. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1077. baseAnnot.GetAnnotData().PageIndex,
  1078. baseAnnot.GetAnnotData().Annot);
  1079. (tempAnnotationPanel as CPDFLinkUI).SetPresentAnnotAttrib(annotParam as LinkParam, baseAnnot.GetAnnotData().Annot as CPDFLinkAnnotation, pdfViewerControl, pdfViewerControl.PDFToolManager.GetDocument().PageCount);
  1080. }
  1081. break;
  1082. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1083. {
  1084. tempAnnotationPanel = new CPDFCloudUI();
  1085. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1086. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1087. baseAnnot.GetAnnotData().PageIndex,
  1088. baseAnnot.GetAnnotData().Annot
  1089. );
  1090. (tempAnnotationPanel as CPDFCloudUI).SetPresentAnnotAttrib(annotParam as PolygonMeasureParam, baseAnnot.GetAnnotData().Annot as CPDFPolygonAnnotation, pdfViewerControl, pdfViewerControl.PDFToolManager.GetDocument().PageCount);
  1091. }
  1092. break;
  1093. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  1094. tempAnnotationPanel = null;
  1095. break;
  1096. default:
  1097. break;
  1098. }
  1099. }
  1100. public void LoadAnnotationPanel(CPDFAnnotationType annotationType)
  1101. {
  1102. this.pdfViewerControl.SetToolType(ToolType.Pan);
  1103. currentAnnotationType = annotationType;
  1104. annotationPanel = GetAnnotationPanel();
  1105. InitAnnotationPanel(annotationType);
  1106. ShowCurrentAnnotPanel();
  1107. }
  1108. private void ShowCurrentAnnotPanel()
  1109. {
  1110. if (annotationPanel != null)
  1111. {
  1112. if (annotationPanel is CPDFFreehandUI)
  1113. {
  1114. if (pdfViewerControl.PDFToolManager.GetToolType() == ToolType.Customize)
  1115. {
  1116. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(true);
  1117. return;
  1118. }
  1119. else
  1120. {
  1121. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(false);
  1122. }
  1123. }
  1124. SetAnnotationPanel(annotationPanel);
  1125. ExpandPanel();
  1126. ChangeAnnotationData();
  1127. EmptyMessage.Visibility = Visibility.Collapsed;
  1128. }
  1129. else
  1130. {
  1131. EmptyMessage.Visibility = Visibility.Visible;
  1132. SetAnnotationPanel(null);
  1133. if (pdfViewerControl != null)
  1134. {
  1135. EmptyMessage.Visibility = Visibility.Collapsed;
  1136. }
  1137. }
  1138. }
  1139. private void ShowTempAnnotPanel()
  1140. {
  1141. BaseAnnot baseAnnot = pdfViewerControl.GetCacheHitTestAnnot();
  1142. if (baseAnnot != null)
  1143. {
  1144. CreatTempAnnotationPanel(baseAnnot);
  1145. SetAnnotationPanel(tempAnnotationPanel);
  1146. ExpandPanel();
  1147. if (tempAnnotationPanel != null)
  1148. {
  1149. EmptyMessage.Visibility = Visibility.Collapsed;
  1150. }
  1151. else
  1152. {
  1153. EmptyMessage.Visibility = Visibility.Visible;
  1154. }
  1155. }
  1156. }
  1157. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  1158. {
  1159. SetAnnotEventData();
  1160. }
  1161. public void ClearPanel()
  1162. {
  1163. annotationPanel = null;
  1164. SetAnnotationPanel(annotationPanel);
  1165. EmptyMessage.Visibility = Visibility.Visible;
  1166. }
  1167. public void SetAnnotEventData()
  1168. {
  1169. if (pdfViewerControl.GetCacheHitTestAnnot() != null)
  1170. {
  1171. ShowTempAnnotPanel();
  1172. isTempPanel = true;
  1173. }
  1174. else
  1175. {
  1176. if (pdfViewerControl != null && (pdfViewerControl.PDFToolManager.GetToolType() == ToolType.CreateAnnot || pdfViewerControl.PDFToolManager.GetToolType() == ToolType.Customize))
  1177. {
  1178. ShowCurrentAnnotPanel();
  1179. isTempPanel = false;
  1180. }
  1181. else if (annotationPanel is CPDFStampUI && currentAnnotationType == CPDFAnnotationType.Stamp)
  1182. {
  1183. ShowCurrentAnnotPanel();
  1184. }
  1185. else if (annotationPanel is CPDFSignatureUI && currentAnnotationType == CPDFAnnotationType.Signature)
  1186. {
  1187. ShowCurrentAnnotPanel();
  1188. }
  1189. else if (disableClean == false)
  1190. {
  1191. ClearPanel();
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }