CPDFAnnotationControl.xaml.cs 67 KB

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