CPDFAnnotationControl.xaml.cs 67 KB

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