CPDFAnnotationControl.xaml.cs 67 KB

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