CPDFAnnotationControl.xaml.cs 67 KB

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