CPDFAnnotationControl.xaml.cs 69 KB

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