CPDFAnnotationControl.xaml.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. pdfViewerControl.PDFViewTool.GetDefaultSettingParam().IsCreateSquarePolygonMeasure = false;
  299. annotHandlerEventArgs = new PolygonMeasureParam()
  300. {
  301. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  302. };
  303. if (annotHandlerEventArgs is PolygonMeasureParam polygonMeasureParam)
  304. {
  305. polygonMeasureParam.LineColor = new byte[] { polygonData.BorderColor.R, polygonData.BorderColor.G, polygonData.BorderColor.B };
  306. polygonMeasureParam.HasFillColor = true;
  307. if(polygonData.FillColor.A == 0)
  308. {
  309. polygonMeasureParam.HasFillColor = false;
  310. }
  311. polygonMeasureParam.FillColor = new byte[] { polygonData.FillColor.R, polygonData.FillColor.G, polygonData.FillColor.B };
  312. polygonMeasureParam.BorderStyle = polygonData.BorderStyle;
  313. ParamConverter.ParseDashStyle(polygonData.DashStyle, out float[] LineDash, out C_BORDER_STYLE BorderStyle);
  314. polygonMeasureParam.LineDash = LineDash;
  315. polygonMeasureParam.BorderEffector = polygonData.BorderEffector;
  316. polygonMeasureParam.LineWidth = polygonData.Thickness;
  317. polygonMeasureParam.Transparency = (byte)(polygonData.Opacity / 100 * 255.0);
  318. polygonMeasureParam.IsMeasure = false;
  319. polygonMeasureParam.Content = polygonData.Note;
  320. }
  321. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON);
  322. }
  323. break;
  324. case CPDFAnnotationType.Arrow:
  325. case CPDFAnnotationType.Line:
  326. {
  327. CPDFLineShapeData lineData = pdfAnnotationData as CPDFLineShapeData;
  328. annotHandlerEventArgs = new LineParam()
  329. {
  330. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINE
  331. };
  332. if (annotHandlerEventArgs is LineParam lineParam)
  333. {
  334. byte[] LineColor = new byte[] { lineData.BorderColor.R, lineData.BorderColor.G, lineData.BorderColor.B };
  335. lineParam.LineColor = LineColor;
  336. lineParam.LineWidth = lineData.Thickness;
  337. lineParam.Transparency = Convert.ToByte(lineData.Opacity * 255);
  338. ParamConverter.ParseDashStyle(lineData.DashStyle, out float[] LineDash, out C_BORDER_STYLE BorderStyle);
  339. lineParam.LineDash = LineDash;
  340. lineParam.BorderStyle = BorderStyle;
  341. lineParam.HeadLineType = lineData.LineType.HeadLineType;
  342. lineParam.TailLineType = lineData.LineType.TailLineType;
  343. lineParam.Author = CPDFMarkupData.Author;
  344. lineParam.Content = lineData.Note;
  345. }
  346. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINE);
  347. }
  348. break;
  349. case CPDFAnnotationType.Note:
  350. {
  351. CPDFNoteData noteData = pdfAnnotationData as CPDFNoteData;
  352. annotHandlerEventArgs = new StickyNoteParam();
  353. if (annotHandlerEventArgs is StickyNoteParam stickyNoteParam)
  354. {
  355. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_TEXT;
  356. byte[] StickyNoteColor = new byte[] { noteData.BorderColor.R, noteData.BorderColor.G, noteData.BorderColor.B };
  357. stickyNoteParam.StickyNoteColor = StickyNoteColor;
  358. stickyNoteParam.Content = noteData.Note;
  359. stickyNoteParam.Transparency = 255;
  360. stickyNoteParam.Author = CPDFMarkupData.Author;
  361. }
  362. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT);
  363. }
  364. break;
  365. case CPDFAnnotationType.Freehand:
  366. {
  367. CPDFFreehandData freehandData = pdfAnnotationData as CPDFFreehandData;
  368. annotHandlerEventArgs = new InkParam()
  369. {
  370. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_INK
  371. };
  372. if (annotHandlerEventArgs is InkParam inkParam)
  373. {
  374. byte[] LineColor = new byte[] { freehandData.BorderColor.R, freehandData.BorderColor.G, freehandData.BorderColor.B };
  375. inkParam.InkColor = LineColor;
  376. inkParam.Thickness = freehandData.Thickness;
  377. inkParam.Transparency = Convert.ToByte(freehandData.Opacity * 255);
  378. inkParam.Content = freehandData.Note;
  379. inkParam.Author = CPDFMarkupData.Author;
  380. }
  381. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  382. }
  383. break;
  384. case CPDFAnnotationType.FreeText:
  385. {
  386. CPDFFreeTextData freeTextData = pdfAnnotationData as CPDFFreeTextData;
  387. annotHandlerEventArgs = new FreeTextParam()
  388. {
  389. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT
  390. };
  391. if (annotHandlerEventArgs is FreeTextParam freeTextParam)
  392. {
  393. freeTextParam.Transparency = Convert.ToByte(freeTextData.Opacity * 255);
  394. freeTextParam.FontName = freeTextData.FontFamily.ToString();
  395. byte[] FontColor = new byte[] { freeTextData.BorderColor.R, freeTextData.BorderColor.G, freeTextData.BorderColor.B };
  396. freeTextParam.FontColor = FontColor;
  397. freeTextParam.FontSize = freeTextData.FontSize;
  398. string postScriptName = string.Empty;
  399. CPDFFont.GetPostScriptName(pdfFreeTextUI.CPDFFontControl.FontFamilyValue, pdfFreeTextUI.CPDFFontControl.FontStyleValue, ref postScriptName);
  400. freeTextParam.FontName = postScriptName;
  401. switch (freeTextData.TextAlignment)
  402. {
  403. case TextAlignment.Left:
  404. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_LEFT;
  405. break;
  406. case TextAlignment.Right:
  407. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT;
  408. break;
  409. case TextAlignment.Center:
  410. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_CENTER;
  411. break;
  412. default:
  413. break;
  414. }
  415. freeTextParam.Content = freeTextData.Note;
  416. freeTextParam.Author = CPDFMarkupData.Author;
  417. }
  418. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT);
  419. }
  420. break;
  421. case CPDFAnnotationType.Stamp:
  422. {
  423. StampParam stampParam = new StampParam()
  424. {
  425. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP
  426. };
  427. if(pdfAnnotationData is CPDFStampData stampData)
  428. {
  429. SetStamp(ref stampParam, stampData);
  430. annotHandlerEventArgs = stampParam;
  431. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  432. byte[] imageData = null;
  433. int imageWidth = 0;
  434. int imageHeight = 0;
  435. PDFHelp.ImageStreamToByte(stampParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  436. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  437. {
  438. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  439. pdfViewerControl.SetStampMouseImage(imageData, imageWidth, imageHeight);
  440. }
  441. }
  442. pdfViewerControl.SetIsVisibleCustomMouse(true);
  443. pdfViewerControl.SetIsShowStampMouse(true);
  444. }
  445. break;
  446. case CPDFAnnotationType.Signature:
  447. {
  448. AnnotParam signatureParam = new AnnotParam();
  449. CPDFSignatureData SignatureData = pdfAnnotationData as CPDFSignatureData;
  450. SetSignature(ref signatureParam, SignatureData);
  451. switch (signatureParam.CurrentType)
  452. {
  453. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  454. {
  455. StampParam stampParam = (StampParam)signatureParam;
  456. stampParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  457. byte[] imageData = null;
  458. int imageWidth = 0;
  459. int imageHeight = 0;
  460. PDFHelp.ImageStreamToByte(stampParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  461. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  462. {
  463. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  464. pdfViewerControl.SetStampMouseImage(imageData, imageWidth, imageHeight);
  465. pdfViewerControl.SetIsVisibleCustomMouse(true);
  466. pdfViewerControl.SetIsShowStampMouse(true);
  467. annotHandlerEventArgs = signatureParam;
  468. }
  469. }
  470. break;
  471. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  472. {
  473. WriteableBitmap writeableBitmap = CreateInkImage(signatureParam as InkParam);
  474. byte[] imageArray = new byte[writeableBitmap.PixelWidth * writeableBitmap.PixelHeight * 4];
  475. writeableBitmap.CopyPixels(imageArray, writeableBitmap.PixelWidth * 4, 0);
  476. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
  477. pdfViewerControl.SetStampMouseImage(imageArray, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
  478. pdfViewerControl.SetIsVisibleCustomMouse(true);
  479. pdfViewerControl.SetIsShowStampMouse(true);
  480. annotHandlerEventArgs = signatureParam;
  481. }
  482. break;
  483. default:
  484. return;
  485. }
  486. }
  487. break;
  488. case CPDFAnnotationType.Link:
  489. if (annotHandlerEventArgs != null)
  490. {
  491. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINK;
  492. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINK);
  493. }
  494. break;
  495. case CPDFAnnotationType.Unknown:
  496. pdfViewerControl.SetToolType(ToolType.Pan);
  497. return;
  498. default:
  499. break;
  500. }
  501. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  502. if (annotationType != CPDFAnnotationType.Stamp && annotationType != CPDFAnnotationType.Signature && annotationType != CPDFAnnotationType.Image)
  503. {
  504. pdfViewerControl.SetIsShowStampMouse(false);
  505. }
  506. pdfViewerControl.SetAnnotParam(annotHandlerEventArgs);
  507. }
  508. private WriteableBitmap CreateInkImage(InkParam inkParam)
  509. {
  510. if (inkParam == null)
  511. {
  512. return null;
  513. }
  514. if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
  515. {
  516. GeometryGroup PaintGeomtry = new GeometryGroup();
  517. int minLeft = -1;
  518. int minTop = -1;
  519. int maxLeft = -1;
  520. int maxTop = -1;
  521. foreach (List<CPoint> Item in inkParam.InkPath)
  522. {
  523. for (int i = 0; i < Item.Count; i++)
  524. {
  525. CPoint paintPoint = Item[i];
  526. if (minLeft == -1)
  527. {
  528. minLeft = (int)paintPoint.x;
  529. maxLeft = (int)paintPoint.x;
  530. minTop = (int)paintPoint.y;
  531. maxTop = (int)paintPoint.y;
  532. }
  533. else
  534. {
  535. minLeft = (int)Math.Min(minLeft, paintPoint.x);
  536. maxLeft = (int)Math.Max(maxLeft, paintPoint.x);
  537. minTop = (int)Math.Min(minTop, paintPoint.y);
  538. maxTop = (int)Math.Max(maxTop, paintPoint.y);
  539. }
  540. }
  541. }
  542. if (minLeft >= 0 && maxLeft > minLeft && minTop >= 0 && maxTop > minTop)
  543. {
  544. List<List<CPoint>> points = new List<List<CPoint>>();
  545. foreach (List<CPoint> Item in inkParam.InkPath)
  546. {
  547. PathGeometry PaintPath = new PathGeometry();
  548. PathFigureCollection Figures = new PathFigureCollection();
  549. PathFigure AddFigure = new PathFigure();
  550. Figures.Add(AddFigure);
  551. PaintPath.Figures = Figures;
  552. PaintGeomtry.Children.Add(PaintPath);
  553. List<CPoint> changeList = new List<CPoint>();
  554. for (int i = 0; i < Item.Count; i++)
  555. {
  556. Point paintPoint = new Point(DpiHelper.PDFNumToStandardNum(Item[i].x - minLeft), DpiHelper.PDFNumToStandardNum(Item[i].y - minTop));
  557. changeList.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(paintPoint)));
  558. if (i == 0)
  559. {
  560. AddFigure.StartPoint = paintPoint;
  561. }
  562. else
  563. {
  564. LineSegment AddSegment = new LineSegment();
  565. AddSegment.Point = paintPoint;
  566. AddFigure.Segments.Add(AddSegment);
  567. }
  568. }
  569. if (changeList.Count > 0)
  570. {
  571. points.Add(changeList);
  572. }
  573. }
  574. int drawWidth = (int)DpiHelper.PDFNumToStandardNum(maxLeft - minLeft);
  575. int drawHeight = (int)DpiHelper.PDFNumToStandardNum(maxTop - minTop);
  576. inkParam.InkPath = points;
  577. DefaultSettingParam defaultSettingParam = pdfViewerControl.PDFViewTool.GetDefaultSettingParam();
  578. defaultSettingParam.SetAnnotParam(inkParam);
  579. DrawingVisual copyVisual = new DrawingVisual();
  580. DrawingContext copyContext = copyVisual.RenderOpen();
  581. Color color = ParamConverter.ConverterByteForColor(inkParam.InkColor);
  582. color.A = inkParam.Transparency;
  583. Pen drawPen = new Pen(new SolidColorBrush(color), inkParam.Thickness);
  584. copyContext?.DrawGeometry(null, drawPen, PaintGeomtry);
  585. copyContext.Close();
  586. RenderTargetBitmap targetBitmap = new RenderTargetBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Pbgra32);
  587. targetBitmap.Render(copyVisual);
  588. byte[] ImageArray = new byte[targetBitmap.PixelWidth * targetBitmap.PixelHeight * 4];
  589. targetBitmap.CopyPixels(new Int32Rect(0, 0, (int)targetBitmap.PixelWidth, (int)targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  590. WriteableBitmap writeBitmap = new WriteableBitmap(targetBitmap.PixelWidth, targetBitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
  591. writeBitmap.WritePixels(new Int32Rect(0, 0, targetBitmap.PixelWidth, targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  592. return writeBitmap;
  593. }
  594. }
  595. return null;
  596. }
  597. public void SetSignature(ref AnnotParam annotParam, CPDFSignatureData stamp)
  598. {
  599. switch (stamp.Type)
  600. {
  601. case SignatureType.TextType:
  602. case SignatureType.ImageType:
  603. {
  604. annotParam = new StampParam();
  605. annotParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  606. (annotParam as StampParam).Transparency = 255;
  607. (annotParam as StampParam).StampType = C_STAMP_TYPE.IMAGE_STAMP;
  608. if (!string.IsNullOrEmpty(stamp.SourcePath))
  609. {
  610. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  611. PngBitmapEncoder encoder = new PngBitmapEncoder();
  612. encoder.Frames.Add(BitmapFrame.Create(image));
  613. MemoryStream memoryStream = new MemoryStream();
  614. encoder.Save(memoryStream);
  615. (annotParam as StampParam).ImageStream = memoryStream;
  616. }
  617. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  618. }
  619. break;
  620. case SignatureType.Drawing:
  621. {
  622. annotParam = new InkParam();
  623. (annotParam as InkParam).Transparency = stamp.inkColor.A;
  624. annotParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_INK;
  625. List<List<Point>> RawPointList = GetPoints(stamp.DrawingPath);
  626. if (RawPointList != null && RawPointList.Count > 0)
  627. {
  628. List<List<CPoint>> inkPath = new List<List<CPoint>>();
  629. foreach (List<Point> inkPoints in RawPointList)
  630. {
  631. List<CPoint> ink = new List<CPoint>();
  632. foreach (Point point in inkPoints)
  633. {
  634. ink.Add(new CPoint((float)DpiHelper.StandardNumToPDFNum(point.X), (float)DpiHelper.StandardNumToPDFNum(point.Y)));
  635. }
  636. if (ink.Count > 0)
  637. {
  638. inkPath.Add(ink);
  639. }
  640. }
  641. if (inkPath.Count > 0)
  642. {
  643. (annotParam as InkParam).InkPath = inkPath;
  644. }
  645. }
  646. (annotParam as InkParam).Thickness = stamp.inkThickness;
  647. byte[] InkColor = new byte[] { stamp.inkColor.R, stamp.inkColor.G, stamp.inkColor.B };
  648. (annotParam as InkParam).InkColor = InkColor;
  649. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  650. }
  651. break;
  652. default:
  653. break;
  654. }
  655. }
  656. private List<List<Point>> GetPoints(string Path)
  657. {
  658. StrokeCollection Strokes;
  659. List<List<Point>> RawPointList = new List<List<Point>>();
  660. using (FileStream strokeStream = File.OpenRead(Path))
  661. {
  662. Strokes = new StrokeCollection(strokeStream);
  663. }
  664. for (int kk = 0; kk < Strokes.Count; kk++)
  665. {
  666. List<Point> p = new List<Point>();
  667. RawPointList.Add(p);
  668. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  669. {
  670. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  671. if (point.X >= 0 && point.Y >= 0)
  672. RawPointList[kk].Add(point);
  673. }
  674. }
  675. return RawPointList;
  676. }
  677. private void SetStamp(ref StampParam stampParam, CPDFStampData stamp)
  678. {
  679. stampParam.StampText = stamp.StampText;
  680. stampParam.Author = CPDFMarkupData.Author;
  681. stampParam.Transparency = Convert.ToByte(stamp.Opacity * 255);
  682. stampParam.DateText = stamp.StampTextDate;
  683. stampParam.TextStampColor = stamp.TextColor;
  684. stampParam.TextStampShape = stamp.TextSharp;
  685. if (!string.IsNullOrEmpty(stamp.SourcePath))
  686. {
  687. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  688. PngBitmapEncoder encoder = new PngBitmapEncoder();
  689. encoder.Frames.Add(BitmapFrame.Create(image));
  690. MemoryStream memoryStream = new MemoryStream();
  691. encoder.Save(memoryStream);
  692. stampParam.ImageStream = memoryStream;
  693. }
  694. else
  695. {
  696. var i = pdfStampUI.CustomStampList.IndexOf(stamp);
  697. Settings.Default.CustomStampList.RemoveAt(i);
  698. Settings.Default.Save();
  699. pdfStampUI.LoadSettings();
  700. return;
  701. }
  702. stampParam.StampType = stamp.Type;
  703. }
  704. public void AnnotationCancel()
  705. {
  706. this.pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_NONE);
  707. this.pdfViewerControl.SetToolType(ToolType.Pan);
  708. pdfViewerControl.SetIsShowStampMouse(false);
  709. pdfViewerControl.SetIsVisibleCustomMouse(false);
  710. ClearPanel();
  711. }
  712. private void CPDFAnnotationControl_PropertyChanged(object sender, CPDFAnnotationData e)
  713. {
  714. SetAnnotationProperty(e);
  715. }
  716. public void InitAnnotationPanel(CPDFAnnotationType annotationType)
  717. {
  718. switch (annotationType)
  719. {
  720. case CPDFAnnotationType.Highlight:
  721. if (pdfHighlightUI == null)
  722. {
  723. pdfHighlightUI = new CPDFMarkupUI();
  724. }
  725. annotationPanel = pdfHighlightUI;
  726. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  727. break;
  728. case CPDFAnnotationType.Underline:
  729. if (pdfUnderlineUI == null)
  730. {
  731. pdfUnderlineUI = new CPDFMarkupUI();
  732. }
  733. annotationPanel = pdfUnderlineUI;
  734. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  735. break;
  736. case CPDFAnnotationType.Strikeout:
  737. if (pdfStrikeoutUI == null)
  738. {
  739. pdfStrikeoutUI = new CPDFMarkupUI();
  740. }
  741. annotationPanel = pdfStrikeoutUI;
  742. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  743. break;
  744. case CPDFAnnotationType.Squiggly:
  745. if (pdfSquigglyUI == null)
  746. {
  747. pdfSquigglyUI = new CPDFMarkupUI();
  748. }
  749. annotationPanel = pdfSquigglyUI;
  750. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  751. break;
  752. case CPDFAnnotationType.Square:
  753. if (pdfSquareUI == null)
  754. {
  755. pdfSquareUI = new CPDFShapeUI();
  756. }
  757. annotationPanel = pdfSquareUI;
  758. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  759. break;
  760. case CPDFAnnotationType.Circle:
  761. if (pdfCircleUI == null)
  762. {
  763. pdfCircleUI = new CPDFShapeUI();
  764. }
  765. annotationPanel = pdfCircleUI;
  766. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  767. break;
  768. case CPDFAnnotationType.Polygon:
  769. if (pdfCloudUI == null)
  770. {
  771. pdfCloudUI = new CPDFCloudUI();
  772. }
  773. annotationPanel = pdfCloudUI;
  774. break;
  775. case CPDFAnnotationType.Arrow:
  776. if (pdfArrowUI == null)
  777. {
  778. pdfArrowUI = new CPDFShapeUI();
  779. }
  780. annotationPanel = pdfArrowUI;
  781. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  782. break;
  783. case CPDFAnnotationType.Line:
  784. if (pdfLineUI == null)
  785. {
  786. pdfLineUI = new CPDFShapeUI();
  787. }
  788. annotationPanel = pdfLineUI;
  789. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  790. break;
  791. case CPDFAnnotationType.Freehand:
  792. if (pdfFreehandUI == null)
  793. {
  794. pdfFreehandUI = new CPDFFreehandUI();
  795. pdfFreehandUI.EraseClickHandler -= PdfFreehandUI_EraseClickHandler;
  796. pdfFreehandUI.EraseChangeHandler -= PdfFreehandUI_EraseChangeHandler;
  797. pdfFreehandUI.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  798. pdfFreehandUI.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  799. }
  800. annotationPanel = pdfFreehandUI;
  801. break;
  802. case CPDFAnnotationType.FreeText:
  803. if (pdfFreeTextUI == null)
  804. {
  805. pdfFreeTextUI = new CPDFFreeTextUI();
  806. }
  807. annotationPanel = pdfFreeTextUI;
  808. break;
  809. case CPDFAnnotationType.Note:
  810. if (pdfNoteUI == null)
  811. {
  812. pdfNoteUI = new CPDFNoteUI();
  813. }
  814. annotationPanel = pdfNoteUI;
  815. break;
  816. case CPDFAnnotationType.Stamp:
  817. if (pdfStampUI == null)
  818. {
  819. pdfStampUI = new CPDFStampUI();
  820. }
  821. annotationPanel = pdfStampUI;
  822. break;
  823. case CPDFAnnotationType.Signature:
  824. if (pdfSignatureUI == null)
  825. {
  826. pdfSignatureUI = new CPDFSignatureUI();
  827. }
  828. annotationPanel = pdfSignatureUI;
  829. break;
  830. case CPDFAnnotationType.Link:
  831. if (pdfLinkUI == null)
  832. {
  833. pdfLinkUI = new CPDFLinkUI();
  834. }
  835. LinkParam linkAnnotArgs = new LinkParam();
  836. if (this.pdfViewerControl != null && pdfViewerControl.PDFViewTool != null && pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument() != null)
  837. {
  838. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  839. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINK);
  840. pdfLinkUI.InitLinkAnnotArgs(linkAnnotArgs, pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument().PageCount);
  841. }
  842. annotationPanel = pdfLinkUI;
  843. break;
  844. case CPDFAnnotationType.Audio:
  845. SoundParam soundParam = new SoundParam();
  846. soundParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_SOUND;
  847. OpenFileDialog openAudioDialog = new OpenFileDialog();
  848. openAudioDialog.Filter = "Wave Files(*.wav)|*.wav|All Files(*.*;)|*.*;";
  849. if (openAudioDialog.ShowDialog() == true)
  850. {
  851. BitmapImage img = new BitmapImage(new Uri("pack://application:,,,/ComPDFKit.Controls;component/Asset/Resource/SoundAnnot.png"));
  852. PngBitmapEncoder encoder = new PngBitmapEncoder();
  853. encoder.Frames.Add(BitmapFrame.Create(img));
  854. MemoryStream memoryStream = new MemoryStream();
  855. encoder.Save(memoryStream);
  856. soundParam.ImageStream = memoryStream;
  857. soundParam.SoundFilePath = openAudioDialog.FileName;
  858. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  859. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND);
  860. pdfViewerControl.SetAnnotParam(soundParam);
  861. }
  862. else
  863. {
  864. pdfViewerControl.SetToolType(ToolType.Pan);
  865. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  866. }
  867. ClearPanel();
  868. break;
  869. case CPDFAnnotationType.Image:
  870. StampParam stampParam = new StampParam();
  871. OpenFileDialog openFileDialog = new OpenFileDialog();
  872. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  873. if (openFileDialog.ShowDialog() == true)
  874. {
  875. try
  876. {
  877. using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  878. {
  879. PngBitmapEncoder encoder = new PngBitmapEncoder();
  880. encoder.Frames.Add(BitmapFrame.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None));
  881. MemoryStream memoryStream = new MemoryStream();
  882. encoder.Save(memoryStream);
  883. stampParam.ImageStream = memoryStream;
  884. stampParam.Transparency = 255;
  885. stampParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  886. stampParam.StampType = C_STAMP_TYPE.IMAGE_STAMP;
  887. BitmapFrame frame = null;
  888. if (encoder != null && encoder.Frames.Count > 0)
  889. {
  890. frame = encoder.Frames[0];
  891. }
  892. if (frame != null)
  893. {
  894. byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  895. if (frame.Format != PixelFormats.Bgra32)
  896. {
  897. FormatConvertedBitmap covert = new FormatConvertedBitmap(
  898. frame,
  899. PixelFormats.Bgra32,
  900. frame.Palette,
  901. 0);
  902. covert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  903. }
  904. else
  905. {
  906. frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  907. }
  908. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  909. pdfViewerControl.SetStampMouseImage(imageArray, frame.PixelWidth, frame.PixelHeight);
  910. }
  911. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  912. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  913. pdfViewerControl.SetIsVisibleCustomMouse(true);
  914. pdfViewerControl.SetIsShowStampMouse(true);
  915. pdfViewerControl.SetAnnotParam(stampParam);
  916. }
  917. }
  918. catch
  919. {
  920. }
  921. }
  922. else
  923. {
  924. pdfViewerControl.SetToolType(ToolType.Pan);
  925. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  926. }
  927. ClearPanel();
  928. break;
  929. default:
  930. break;
  931. }
  932. }
  933. private void PdfFreehandUI_EraseChangeHandler(object sender, double e)
  934. {
  935. if (pdfViewerControl != null)
  936. {
  937. pdfViewerControl.PDFViewTool.SetEraseZoom(e);
  938. }
  939. }
  940. private void PdfFreehandUI_EraseClickHandler(object sender, bool e)
  941. {
  942. if (e)
  943. {
  944. //pdfViewerControl.PDFToolManager.ClearSelect();
  945. pdfViewerControl.PDFToolManager.SetToolType(ToolType.Customize);
  946. }
  947. else
  948. {
  949. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  950. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  951. }
  952. }
  953. public void CreatTempAnnotationPanel(BaseAnnot baseAnnot)
  954. {
  955. switch (baseAnnot.CurrentType)
  956. {
  957. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  958. {
  959. tempAnnotationPanel = new CPDFMarkupUI();
  960. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Highlight);
  961. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  962. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  963. baseAnnot.GetAnnotData().PageIndex,
  964. baseAnnot.GetAnnotData().Annot);
  965. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  966. }
  967. break;
  968. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  969. {
  970. tempAnnotationPanel = new CPDFMarkupUI();
  971. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Underline);
  972. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  973. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  974. baseAnnot.GetAnnotData().PageIndex,
  975. baseAnnot.GetAnnotData().Annot);
  976. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  977. }
  978. break;
  979. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  980. {
  981. tempAnnotationPanel = new CPDFMarkupUI();
  982. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Strikeout);
  983. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  984. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  985. baseAnnot.GetAnnotData().PageIndex,
  986. baseAnnot.GetAnnotData().Annot);
  987. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  988. }
  989. break;
  990. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  991. {
  992. tempAnnotationPanel = new CPDFMarkupUI();
  993. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Squiggly);
  994. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  995. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  996. baseAnnot.GetAnnotData().PageIndex,
  997. baseAnnot.GetAnnotData().Annot);
  998. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  999. }
  1000. break;
  1001. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  1002. {
  1003. tempAnnotationPanel = new CPDFShapeUI();
  1004. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Square);
  1005. int page = baseAnnot.GetAnnotData().PageIndex;
  1006. var annot = baseAnnot.GetAnnotData().Annot;
  1007. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1008. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1009. baseAnnot.GetAnnotData().PageIndex,
  1010. baseAnnot.GetAnnotData().Annot);
  1011. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1012. }
  1013. break;
  1014. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  1015. {
  1016. tempAnnotationPanel = new CPDFShapeUI();
  1017. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Circle);
  1018. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1019. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1020. baseAnnot.GetAnnotData().PageIndex,
  1021. baseAnnot.GetAnnotData().Annot);
  1022. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1023. }
  1024. break;
  1025. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1026. {
  1027. tempAnnotationPanel = new CPDFShapeUI();
  1028. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Line);
  1029. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1030. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1031. baseAnnot.GetAnnotData().PageIndex,
  1032. baseAnnot.GetAnnotData().Annot);
  1033. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1034. }
  1035. break;
  1036. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1037. {
  1038. CPDFFreehandUI tempFreehandPanel = new CPDFFreehandUI();
  1039. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1040. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1041. baseAnnot.GetAnnotData().PageIndex,
  1042. baseAnnot.GetAnnotData().Annot);
  1043. tempFreehandPanel.SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1044. tempFreehandPanel.EraseClickHandler -= PdfFreehandUI_EraseClickHandler;
  1045. tempFreehandPanel.EraseChangeHandler -= PdfFreehandUI_EraseChangeHandler;
  1046. tempFreehandPanel.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  1047. tempFreehandPanel.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  1048. tempAnnotationPanel = tempFreehandPanel;
  1049. }
  1050. break;
  1051. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1052. {
  1053. tempAnnotationPanel = new CPDFFreeTextUI();
  1054. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1055. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1056. baseAnnot.GetAnnotData().PageIndex,
  1057. baseAnnot.GetAnnotData().Annot);
  1058. (tempAnnotationPanel as CPDFFreeTextUI).SetPresentAnnotAttrib(annotParam as FreeTextParam, baseAnnot.GetAnnotData().Annot as CPDFFreeTextAnnotation, pdfViewerControl);
  1059. }
  1060. break;
  1061. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  1062. {
  1063. tempAnnotationPanel = new CPDFNoteUI();
  1064. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1065. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1066. baseAnnot.GetAnnotData().PageIndex,
  1067. baseAnnot.GetAnnotData().Annot);
  1068. (tempAnnotationPanel as CPDFNoteUI).SetPresentAnnotAttrib(annotParam as StickyNoteParam, baseAnnot.GetAnnotData().Annot as CPDFTextAnnotation, pdfViewerControl);
  1069. }
  1070. break;
  1071. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1072. {
  1073. tempAnnotationPanel = new CPDFTempStampUI();
  1074. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1075. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1076. baseAnnot.GetAnnotData().PageIndex,
  1077. baseAnnot.GetAnnotData().Annot);
  1078. (tempAnnotationPanel as CPDFTempStampUI).SetPresentAnnotAttrib(annotParam as StampParam, baseAnnot.GetAnnotData().Annot as CPDFStampAnnotation, pdfViewerControl.PDFToolManager.GetDocument(), pdfViewerControl);
  1079. }
  1080. break;
  1081. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1082. {
  1083. tempAnnotationPanel = new CPDFLinkUI();
  1084. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1085. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1086. baseAnnot.GetAnnotData().PageIndex,
  1087. baseAnnot.GetAnnotData().Annot);
  1088. (tempAnnotationPanel as CPDFLinkUI).SetPresentAnnotAttrib(annotParam as LinkParam, baseAnnot.GetAnnotData().Annot as CPDFLinkAnnotation, pdfViewerControl, pdfViewerControl.PDFToolManager.GetDocument().PageCount);
  1089. }
  1090. break;
  1091. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1092. {
  1093. tempAnnotationPanel = new CPDFCloudUI();
  1094. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1095. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1096. baseAnnot.GetAnnotData().PageIndex,
  1097. baseAnnot.GetAnnotData().Annot
  1098. );
  1099. (tempAnnotationPanel as CPDFCloudUI).SetPresentAnnotAttrib(annotParam as PolygonMeasureParam, baseAnnot.GetAnnotData().Annot as CPDFPolygonAnnotation, pdfViewerControl, pdfViewerControl.PDFToolManager.GetDocument().PageCount);
  1100. }
  1101. break;
  1102. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  1103. tempAnnotationPanel = null;
  1104. break;
  1105. default:
  1106. break;
  1107. }
  1108. }
  1109. public void LoadAnnotationPanel(CPDFAnnotationType annotationType)
  1110. {
  1111. this.pdfViewerControl.SetToolType(ToolType.Pan);
  1112. currentAnnotationType = annotationType;
  1113. annotationPanel = GetAnnotationPanel();
  1114. InitAnnotationPanel(annotationType);
  1115. ShowCurrentAnnotPanel();
  1116. }
  1117. private void ShowCurrentAnnotPanel()
  1118. {
  1119. if (annotationPanel != null)
  1120. {
  1121. if (annotationPanel is CPDFFreehandUI)
  1122. {
  1123. if (pdfViewerControl.PDFToolManager.GetToolType() == ToolType.Customize)
  1124. {
  1125. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(true);
  1126. return;
  1127. }
  1128. else
  1129. {
  1130. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(false);
  1131. }
  1132. }
  1133. SetAnnotationPanel(annotationPanel);
  1134. ExpandPanel();
  1135. ChangeAnnotationData();
  1136. EmptyMessage.Visibility = Visibility.Collapsed;
  1137. }
  1138. else
  1139. {
  1140. EmptyMessage.Visibility = Visibility.Visible;
  1141. SetAnnotationPanel(null);
  1142. if (pdfViewerControl != null)
  1143. {
  1144. EmptyMessage.Visibility = Visibility.Collapsed;
  1145. }
  1146. }
  1147. }
  1148. public void ShowTempAnnotPanel()
  1149. {
  1150. BaseAnnot baseAnnot = pdfViewerControl.GetCacheHitTestAnnot();
  1151. if (baseAnnot != null)
  1152. {
  1153. CreatTempAnnotationPanel(baseAnnot);
  1154. SetAnnotationPanel(tempAnnotationPanel);
  1155. ExpandPanel();
  1156. if (tempAnnotationPanel != null)
  1157. {
  1158. EmptyMessage.Visibility = Visibility.Collapsed;
  1159. }
  1160. else
  1161. {
  1162. EmptyMessage.Visibility = Visibility.Visible;
  1163. }
  1164. }
  1165. }
  1166. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  1167. {
  1168. SetAnnotEventData();
  1169. }
  1170. public void ClearPanel()
  1171. {
  1172. annotationPanel = null;
  1173. SetAnnotationPanel(annotationPanel);
  1174. EmptyMessage.Visibility = Visibility.Visible;
  1175. }
  1176. public void SetAnnotEventData()
  1177. {
  1178. if (pdfViewerControl.GetCacheHitTestAnnot() != null)
  1179. {
  1180. ShowTempAnnotPanel();
  1181. isTempPanel = true;
  1182. }
  1183. else
  1184. {
  1185. if (pdfViewerControl != null && (pdfViewerControl.PDFToolManager.GetToolType() == ToolType.CreateAnnot || pdfViewerControl.PDFToolManager.GetToolType() == ToolType.Customize))
  1186. {
  1187. ShowCurrentAnnotPanel();
  1188. isTempPanel = false;
  1189. }
  1190. else if (annotationPanel is CPDFStampUI && currentAnnotationType == CPDFAnnotationType.Stamp)
  1191. {
  1192. ShowCurrentAnnotPanel();
  1193. }
  1194. else if (annotationPanel is CPDFSignatureUI && currentAnnotationType == CPDFAnnotationType.Signature)
  1195. {
  1196. ShowCurrentAnnotPanel();
  1197. }
  1198. else if (disableClean == false)
  1199. {
  1200. ClearPanel();
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }