CPDFAnnotationControl.xaml.cs 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. annotHandlerEventArgs = new PolygonMeasureParam()
  299. {
  300. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  301. };
  302. if (annotHandlerEventArgs is PolygonMeasureParam polygonMeasureParam)
  303. {
  304. polygonMeasureParam.LineColor = new byte[] { polygonData.BorderColor.R, polygonData.BorderColor.G, polygonData.BorderColor.B };
  305. polygonMeasureParam.HasFillColor = true;
  306. if(polygonData.FillColor.A == 0)
  307. {
  308. polygonMeasureParam.HasFillColor = false;
  309. }
  310. polygonMeasureParam.FillColor = new byte[] { polygonData.FillColor.R, polygonData.FillColor.G, polygonData.FillColor.B };
  311. polygonMeasureParam.BorderStyle = polygonData.BorderStyle;
  312. ParamConverter.ParseDashStyle(polygonData.DashStyle, out float[] LineDash, out C_BORDER_STYLE BorderStyle);
  313. polygonMeasureParam.LineDash = LineDash;
  314. polygonMeasureParam.BorderEffector = polygonData.BorderEffector;
  315. polygonMeasureParam.LineWidth = polygonData.Thickness;
  316. polygonMeasureParam.Transparency = (byte)(polygonData.Opacity / 100 * 255.0);
  317. polygonMeasureParam.IsMeasure = false;
  318. }
  319. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON);
  320. }
  321. break;
  322. case CPDFAnnotationType.Arrow:
  323. case CPDFAnnotationType.Line:
  324. {
  325. CPDFLineShapeData lineData = pdfAnnotationData as CPDFLineShapeData;
  326. annotHandlerEventArgs = new LineParam()
  327. {
  328. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINE
  329. };
  330. if (annotHandlerEventArgs is LineParam lineParam)
  331. {
  332. byte[] LineColor = new byte[] { lineData.BorderColor.R, lineData.BorderColor.G, lineData.BorderColor.B };
  333. lineParam.LineColor = LineColor;
  334. lineParam.LineWidth = lineData.Thickness;
  335. lineParam.Transparency = Convert.ToByte(lineData.Opacity * 255);
  336. ParamConverter.ParseDashStyle(lineData.DashStyle, out float[] LineDash, out C_BORDER_STYLE BorderStyle);
  337. lineParam.LineDash = LineDash;
  338. lineParam.BorderStyle = BorderStyle;
  339. lineParam.HeadLineType = lineData.LineType.HeadLineType;
  340. lineParam.TailLineType = lineData.LineType.TailLineType;
  341. lineParam.Author = CPDFMarkupData.Author;
  342. lineParam.Content = lineData.Note;
  343. }
  344. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINE);
  345. }
  346. break;
  347. case CPDFAnnotationType.Note:
  348. {
  349. CPDFNoteData noteData = pdfAnnotationData as CPDFNoteData;
  350. annotHandlerEventArgs = new StickyNoteParam();
  351. if (annotHandlerEventArgs is StickyNoteParam stickyNoteParam)
  352. {
  353. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_TEXT;
  354. byte[] StickyNoteColor = new byte[] { noteData.BorderColor.R, noteData.BorderColor.G, noteData.BorderColor.B };
  355. stickyNoteParam.StickyNoteColor = StickyNoteColor;
  356. stickyNoteParam.Content = noteData.Note;
  357. stickyNoteParam.Transparency = 255;
  358. stickyNoteParam.Author = CPDFMarkupData.Author;
  359. }
  360. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT);
  361. }
  362. break;
  363. case CPDFAnnotationType.Freehand:
  364. {
  365. CPDFFreehandData freehandData = pdfAnnotationData as CPDFFreehandData;
  366. annotHandlerEventArgs = new InkParam()
  367. {
  368. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_INK
  369. };
  370. if (annotHandlerEventArgs is InkParam inkParam)
  371. {
  372. byte[] LineColor = new byte[] { freehandData.BorderColor.R, freehandData.BorderColor.G, freehandData.BorderColor.B };
  373. inkParam.InkColor = LineColor;
  374. inkParam.Thickness = freehandData.Thickness;
  375. inkParam.Transparency = Convert.ToByte(freehandData.Opacity * 255);
  376. inkParam.Content = freehandData.Note;
  377. inkParam.Author = CPDFMarkupData.Author;
  378. }
  379. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  380. }
  381. break;
  382. case CPDFAnnotationType.FreeText:
  383. {
  384. CPDFFreeTextData freeTextData = pdfAnnotationData as CPDFFreeTextData;
  385. annotHandlerEventArgs = new FreeTextParam()
  386. {
  387. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT
  388. };
  389. if (annotHandlerEventArgs is FreeTextParam freeTextParam)
  390. {
  391. freeTextParam.Transparency = Convert.ToByte(freeTextData.Opacity * 255);
  392. freeTextParam.FontName = freeTextData.FontFamily.ToString();
  393. byte[] FontColor = new byte[] { freeTextData.BorderColor.R, freeTextData.BorderColor.G, freeTextData.BorderColor.B };
  394. freeTextParam.FontColor = FontColor;
  395. freeTextParam.FontSize = freeTextData.FontSize;
  396. string postScriptName = string.Empty;
  397. CPDFFont.GetPostScriptName(pdfFreeTextUI.CPDFFontControl.FontFamilyValue, pdfFreeTextUI.CPDFFontControl.FontStyleValue, ref postScriptName);
  398. freeTextParam.FontName = postScriptName;
  399. switch (freeTextData.TextAlignment)
  400. {
  401. case TextAlignment.Left:
  402. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_LEFT;
  403. break;
  404. case TextAlignment.Right:
  405. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT;
  406. break;
  407. case TextAlignment.Center:
  408. freeTextParam.Alignment = C_TEXT_ALIGNMENT.ALIGNMENT_CENTER;
  409. break;
  410. default:
  411. break;
  412. }
  413. freeTextParam.Content = freeTextData.Note;
  414. freeTextParam.Author = CPDFMarkupData.Author;
  415. }
  416. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT);
  417. }
  418. break;
  419. case CPDFAnnotationType.Stamp:
  420. {
  421. StampParam stampParam = new StampParam()
  422. {
  423. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP
  424. };
  425. if(pdfAnnotationData is CPDFStampData stampData)
  426. {
  427. SetStamp(ref stampParam, stampData);
  428. annotHandlerEventArgs = stampParam;
  429. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  430. byte[] imageData = null;
  431. int imageWidth = 0;
  432. int imageHeight = 0;
  433. PDFHelp.ImageStreamToByte(stampParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  434. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  435. {
  436. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  437. pdfViewerControl.SetStampMouseImage(imageData, imageWidth, imageHeight);
  438. }
  439. }
  440. pdfViewerControl.SetIsVisibleCustomMouse(true);
  441. pdfViewerControl.SetIsShowStampMouse(true);
  442. }
  443. break;
  444. case CPDFAnnotationType.Signature:
  445. {
  446. AnnotParam signatureParam = new AnnotParam();
  447. CPDFSignatureData SignatureData = pdfAnnotationData as CPDFSignatureData;
  448. SetSignature(ref signatureParam, SignatureData);
  449. switch (signatureParam.CurrentType)
  450. {
  451. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  452. {
  453. StampParam stampParam = (StampParam)signatureParam;
  454. stampParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  455. byte[] imageData = null;
  456. int imageWidth = 0;
  457. int imageHeight = 0;
  458. PDFHelp.ImageStreamToByte(stampParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  459. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  460. {
  461. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  462. pdfViewerControl.SetStampMouseImage(imageData, imageWidth, imageHeight);
  463. pdfViewerControl.SetIsVisibleCustomMouse(true);
  464. pdfViewerControl.SetIsShowStampMouse(true);
  465. annotHandlerEventArgs = signatureParam;
  466. }
  467. }
  468. break;
  469. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  470. {
  471. WriteableBitmap writeableBitmap = CreateInkImage(signatureParam as InkParam);
  472. byte[] imageArray = new byte[writeableBitmap.PixelWidth * writeableBitmap.PixelHeight * 4];
  473. writeableBitmap.CopyPixels(imageArray, writeableBitmap.PixelWidth * 4, 0);
  474. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
  475. pdfViewerControl.SetStampMouseImage(imageArray, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
  476. pdfViewerControl.SetIsVisibleCustomMouse(true);
  477. pdfViewerControl.SetIsShowStampMouse(true);
  478. annotHandlerEventArgs = signatureParam;
  479. }
  480. break;
  481. default:
  482. return;
  483. }
  484. }
  485. break;
  486. case CPDFAnnotationType.Link:
  487. if (annotHandlerEventArgs != null)
  488. {
  489. annotHandlerEventArgs.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_LINK;
  490. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINK);
  491. }
  492. break;
  493. case CPDFAnnotationType.Unknown:
  494. pdfViewerControl.SetToolType(ToolType.Pan);
  495. return;
  496. default:
  497. break;
  498. }
  499. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  500. if (annotationType != CPDFAnnotationType.Stamp && annotationType != CPDFAnnotationType.Signature && annotationType != CPDFAnnotationType.Image)
  501. {
  502. pdfViewerControl.SetIsShowStampMouse(false);
  503. }
  504. pdfViewerControl.SetAnnotParam(annotHandlerEventArgs);
  505. }
  506. private WriteableBitmap CreateInkImage(InkParam inkParam)
  507. {
  508. if (inkParam == null)
  509. {
  510. return null;
  511. }
  512. if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
  513. {
  514. GeometryGroup PaintGeomtry = new GeometryGroup();
  515. int minLeft = -1;
  516. int minTop = -1;
  517. int maxLeft = -1;
  518. int maxTop = -1;
  519. foreach (List<CPoint> Item in inkParam.InkPath)
  520. {
  521. for (int i = 0; i < Item.Count; i++)
  522. {
  523. CPoint paintPoint = Item[i];
  524. if (minLeft == -1)
  525. {
  526. minLeft = (int)paintPoint.x;
  527. maxLeft = (int)paintPoint.x;
  528. minTop = (int)paintPoint.y;
  529. maxTop = (int)paintPoint.y;
  530. }
  531. else
  532. {
  533. minLeft = (int)Math.Min(minLeft, paintPoint.x);
  534. maxLeft = (int)Math.Max(maxLeft, paintPoint.x);
  535. minTop = (int)Math.Min(minTop, paintPoint.y);
  536. maxTop = (int)Math.Max(maxTop, paintPoint.y);
  537. }
  538. }
  539. }
  540. if (minLeft >= 0 && maxLeft > minLeft && minTop >= 0 && maxTop > minTop)
  541. {
  542. List<List<CPoint>> points = new List<List<CPoint>>();
  543. foreach (List<CPoint> Item in inkParam.InkPath)
  544. {
  545. PathGeometry PaintPath = new PathGeometry();
  546. PathFigureCollection Figures = new PathFigureCollection();
  547. PathFigure AddFigure = new PathFigure();
  548. Figures.Add(AddFigure);
  549. PaintPath.Figures = Figures;
  550. PaintGeomtry.Children.Add(PaintPath);
  551. List<CPoint> changeList = new List<CPoint>();
  552. for (int i = 0; i < Item.Count; i++)
  553. {
  554. Point paintPoint = new Point(DpiHelper.PDFNumToStandardNum(Item[i].x - minLeft), DpiHelper.PDFNumToStandardNum(Item[i].y - minTop));
  555. changeList.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(paintPoint)));
  556. if (i == 0)
  557. {
  558. AddFigure.StartPoint = paintPoint;
  559. }
  560. else
  561. {
  562. LineSegment AddSegment = new LineSegment();
  563. AddSegment.Point = paintPoint;
  564. AddFigure.Segments.Add(AddSegment);
  565. }
  566. }
  567. if (changeList.Count > 0)
  568. {
  569. points.Add(changeList);
  570. }
  571. }
  572. int drawWidth = (int)DpiHelper.PDFNumToStandardNum(maxLeft - minLeft);
  573. int drawHeight = (int)DpiHelper.PDFNumToStandardNum(maxTop - minTop);
  574. inkParam.InkPath = points;
  575. DefaultSettingParam defaultSettingParam = pdfViewerControl.PDFViewTool.GetDefaultSettingParam();
  576. defaultSettingParam.SetAnnotParam(inkParam);
  577. DrawingVisual copyVisual = new DrawingVisual();
  578. DrawingContext copyContext = copyVisual.RenderOpen();
  579. Color color = ParamConverter.ConverterByteForColor(inkParam.InkColor);
  580. color.A = inkParam.Transparency;
  581. Pen drawPen = new Pen(new SolidColorBrush(color), inkParam.Thickness);
  582. copyContext?.DrawGeometry(null, drawPen, PaintGeomtry);
  583. copyContext.Close();
  584. RenderTargetBitmap targetBitmap = new RenderTargetBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Pbgra32);
  585. targetBitmap.Render(copyVisual);
  586. byte[] ImageArray = new byte[targetBitmap.PixelWidth * targetBitmap.PixelHeight * 4];
  587. targetBitmap.CopyPixels(new Int32Rect(0, 0, (int)targetBitmap.PixelWidth, (int)targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  588. WriteableBitmap writeBitmap = new WriteableBitmap(targetBitmap.PixelWidth, targetBitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
  589. writeBitmap.WritePixels(new Int32Rect(0, 0, targetBitmap.PixelWidth, targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  590. return writeBitmap;
  591. }
  592. }
  593. return null;
  594. }
  595. public void SetSignature(ref AnnotParam annotParam, CPDFSignatureData stamp)
  596. {
  597. switch (stamp.Type)
  598. {
  599. case SignatureType.TextType:
  600. case SignatureType.ImageType:
  601. {
  602. annotParam = new StampParam();
  603. annotParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  604. (annotParam as StampParam).Transparency = 255;
  605. (annotParam as StampParam).StampType = C_STAMP_TYPE.IMAGE_STAMP;
  606. if (!string.IsNullOrEmpty(stamp.SourcePath))
  607. {
  608. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  609. PngBitmapEncoder encoder = new PngBitmapEncoder();
  610. encoder.Frames.Add(BitmapFrame.Create(image));
  611. MemoryStream memoryStream = new MemoryStream();
  612. encoder.Save(memoryStream);
  613. (annotParam as StampParam).ImageStream = memoryStream;
  614. }
  615. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  616. }
  617. break;
  618. case SignatureType.Drawing:
  619. {
  620. annotParam = new InkParam();
  621. (annotParam as InkParam).Transparency = stamp.inkColor.A;
  622. annotParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_INK;
  623. List<List<Point>> RawPointList = GetPoints(stamp.DrawingPath);
  624. if (RawPointList != null && RawPointList.Count > 0)
  625. {
  626. List<List<CPoint>> inkPath = new List<List<CPoint>>();
  627. foreach (List<Point> inkPoints in RawPointList)
  628. {
  629. List<CPoint> ink = new List<CPoint>();
  630. foreach (Point point in inkPoints)
  631. {
  632. ink.Add(new CPoint((float)DpiHelper.StandardNumToPDFNum(point.X), (float)DpiHelper.StandardNumToPDFNum(point.Y)));
  633. }
  634. if (ink.Count > 0)
  635. {
  636. inkPath.Add(ink);
  637. }
  638. }
  639. if (inkPath.Count > 0)
  640. {
  641. (annotParam as InkParam).InkPath = inkPath;
  642. }
  643. }
  644. (annotParam as InkParam).Thickness = stamp.inkThickness;
  645. byte[] InkColor = new byte[] { stamp.inkColor.R, stamp.inkColor.G, stamp.inkColor.B };
  646. (annotParam as InkParam).InkColor = InkColor;
  647. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  648. }
  649. break;
  650. default:
  651. break;
  652. }
  653. }
  654. private List<List<Point>> GetPoints(string Path)
  655. {
  656. StrokeCollection Strokes;
  657. List<List<Point>> RawPointList = new List<List<Point>>();
  658. using (FileStream strokeStream = File.OpenRead(Path))
  659. {
  660. Strokes = new StrokeCollection(strokeStream);
  661. }
  662. for (int kk = 0; kk < Strokes.Count; kk++)
  663. {
  664. List<Point> p = new List<Point>();
  665. RawPointList.Add(p);
  666. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  667. {
  668. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  669. if (point.X >= 0 && point.Y >= 0)
  670. RawPointList[kk].Add(point);
  671. }
  672. }
  673. return RawPointList;
  674. }
  675. private void SetStamp(ref StampParam stampParam, CPDFStampData stamp)
  676. {
  677. stampParam.StampText = stamp.StampText;
  678. stampParam.Author = CPDFMarkupData.Author;
  679. stampParam.Transparency = Convert.ToByte(stamp.Opacity * 255);
  680. stampParam.DateText = stamp.StampTextDate;
  681. stampParam.TextStampColor = stamp.TextColor;
  682. stampParam.TextStampShape = stamp.TextSharp;
  683. if (!string.IsNullOrEmpty(stamp.SourcePath))
  684. {
  685. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  686. PngBitmapEncoder encoder = new PngBitmapEncoder();
  687. encoder.Frames.Add(BitmapFrame.Create(image));
  688. MemoryStream memoryStream = new MemoryStream();
  689. encoder.Save(memoryStream);
  690. stampParam.ImageStream = memoryStream;
  691. }
  692. else
  693. {
  694. var i = pdfStampUI.CustomStampList.IndexOf(stamp);
  695. Settings.Default.CustomStampList.RemoveAt(i);
  696. Settings.Default.Save();
  697. pdfStampUI.LoadSettings();
  698. return;
  699. }
  700. stampParam.StampType = stamp.Type;
  701. }
  702. public void AnnotationCancel()
  703. {
  704. this.pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_NONE);
  705. this.pdfViewerControl.SetToolType(ToolType.Pan);
  706. pdfViewerControl.SetIsShowStampMouse(false);
  707. pdfViewerControl.SetIsVisibleCustomMouse(false);
  708. ClearPanel();
  709. }
  710. private void CPDFAnnotationControl_PropertyChanged(object sender, CPDFAnnotationData e)
  711. {
  712. SetAnnotationProperty(e);
  713. }
  714. public void InitAnnotationPanel(CPDFAnnotationType annotationType)
  715. {
  716. switch (annotationType)
  717. {
  718. case CPDFAnnotationType.Highlight:
  719. if (pdfHighlightUI == null)
  720. {
  721. pdfHighlightUI = new CPDFMarkupUI();
  722. }
  723. annotationPanel = pdfHighlightUI;
  724. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  725. break;
  726. case CPDFAnnotationType.Underline:
  727. if (pdfUnderlineUI == null)
  728. {
  729. pdfUnderlineUI = new CPDFMarkupUI();
  730. }
  731. annotationPanel = pdfUnderlineUI;
  732. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  733. break;
  734. case CPDFAnnotationType.Strikeout:
  735. if (pdfStrikeoutUI == null)
  736. {
  737. pdfStrikeoutUI = new CPDFMarkupUI();
  738. }
  739. annotationPanel = pdfStrikeoutUI;
  740. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  741. break;
  742. case CPDFAnnotationType.Squiggly:
  743. if (pdfSquigglyUI == null)
  744. {
  745. pdfSquigglyUI = new CPDFMarkupUI();
  746. }
  747. annotationPanel = pdfSquigglyUI;
  748. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  749. break;
  750. case CPDFAnnotationType.Square:
  751. if (pdfSquareUI == null)
  752. {
  753. pdfSquareUI = new CPDFShapeUI();
  754. }
  755. annotationPanel = pdfSquareUI;
  756. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  757. break;
  758. case CPDFAnnotationType.Circle:
  759. if (pdfCircleUI == null)
  760. {
  761. pdfCircleUI = new CPDFShapeUI();
  762. }
  763. annotationPanel = pdfCircleUI;
  764. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  765. break;
  766. case CPDFAnnotationType.Polygon:
  767. if (pdfCloudUI == null)
  768. {
  769. pdfCloudUI = new CPDFCloudUI();
  770. }
  771. annotationPanel = pdfCloudUI;
  772. break;
  773. case CPDFAnnotationType.Arrow:
  774. if (pdfArrowUI == null)
  775. {
  776. pdfArrowUI = new CPDFShapeUI();
  777. }
  778. annotationPanel = pdfArrowUI;
  779. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  780. break;
  781. case CPDFAnnotationType.Line:
  782. if (pdfLineUI == null)
  783. {
  784. pdfLineUI = new CPDFShapeUI();
  785. }
  786. annotationPanel = pdfLineUI;
  787. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  788. break;
  789. case CPDFAnnotationType.Freehand:
  790. if (pdfFreehandUI == null)
  791. {
  792. pdfFreehandUI = new CPDFFreehandUI();
  793. pdfFreehandUI.EraseClickHandler -= PdfFreehandUI_EraseClickHandler;
  794. pdfFreehandUI.EraseChangeHandler -= PdfFreehandUI_EraseChangeHandler;
  795. pdfFreehandUI.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  796. pdfFreehandUI.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  797. }
  798. annotationPanel = pdfFreehandUI;
  799. break;
  800. case CPDFAnnotationType.FreeText:
  801. if (pdfFreeTextUI == null)
  802. {
  803. pdfFreeTextUI = new CPDFFreeTextUI();
  804. }
  805. annotationPanel = pdfFreeTextUI;
  806. break;
  807. case CPDFAnnotationType.Note:
  808. if (pdfNoteUI == null)
  809. {
  810. pdfNoteUI = new CPDFNoteUI();
  811. }
  812. annotationPanel = pdfNoteUI;
  813. break;
  814. case CPDFAnnotationType.Stamp:
  815. if (pdfStampUI == null)
  816. {
  817. pdfStampUI = new CPDFStampUI();
  818. }
  819. annotationPanel = pdfStampUI;
  820. break;
  821. case CPDFAnnotationType.Signature:
  822. if (pdfSignatureUI == null)
  823. {
  824. pdfSignatureUI = new CPDFSignatureUI();
  825. }
  826. annotationPanel = pdfSignatureUI;
  827. break;
  828. case CPDFAnnotationType.Link:
  829. if (pdfLinkUI == null)
  830. {
  831. pdfLinkUI = new CPDFLinkUI();
  832. }
  833. LinkParam linkAnnotArgs = new LinkParam();
  834. if (this.pdfViewerControl != null && pdfViewerControl.PDFViewTool != null && pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument() != null)
  835. {
  836. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  837. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_LINK);
  838. pdfLinkUI.InitLinkAnnotArgs(linkAnnotArgs, pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument().PageCount);
  839. }
  840. annotationPanel = pdfLinkUI;
  841. break;
  842. case CPDFAnnotationType.Audio:
  843. SoundParam soundParam = new SoundParam();
  844. soundParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_SOUND;
  845. OpenFileDialog openAudioDialog = new OpenFileDialog();
  846. openAudioDialog.Filter = "Wave Files(*.wav)|*.wav|All Files(*.*;)|*.*;";
  847. if (openAudioDialog.ShowDialog() == true)
  848. {
  849. BitmapImage img = new BitmapImage(new Uri("pack://application:,,,/ComPDFKit.Controls;component/Asset/Resource/SoundAnnot.png"));
  850. PngBitmapEncoder encoder = new PngBitmapEncoder();
  851. encoder.Frames.Add(BitmapFrame.Create(img));
  852. MemoryStream memoryStream = new MemoryStream();
  853. encoder.Save(memoryStream);
  854. soundParam.ImageStream = memoryStream;
  855. soundParam.SoundFilePath = openAudioDialog.FileName;
  856. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  857. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND);
  858. pdfViewerControl.SetAnnotParam(soundParam);
  859. }
  860. else
  861. {
  862. pdfViewerControl.SetToolType(ToolType.Pan);
  863. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  864. }
  865. ClearPanel();
  866. break;
  867. case CPDFAnnotationType.Image:
  868. StampParam stampParam = new StampParam();
  869. OpenFileDialog openFileDialog = new OpenFileDialog();
  870. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  871. if (openFileDialog.ShowDialog() == true)
  872. {
  873. try
  874. {
  875. using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  876. {
  877. PngBitmapEncoder encoder = new PngBitmapEncoder();
  878. encoder.Frames.Add(BitmapFrame.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None));
  879. MemoryStream memoryStream = new MemoryStream();
  880. encoder.Save(memoryStream);
  881. stampParam.ImageStream = memoryStream;
  882. stampParam.Transparency = 255;
  883. stampParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_STAMP;
  884. stampParam.StampType = C_STAMP_TYPE.IMAGE_STAMP;
  885. BitmapFrame frame = null;
  886. if (encoder != null && encoder.Frames.Count > 0)
  887. {
  888. frame = encoder.Frames[0];
  889. }
  890. if (frame != null)
  891. {
  892. byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  893. if (frame.Format != PixelFormats.Bgra32)
  894. {
  895. FormatConvertedBitmap covert = new FormatConvertedBitmap(
  896. frame,
  897. PixelFormats.Bgra32,
  898. frame.Palette,
  899. 0);
  900. covert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  901. }
  902. else
  903. {
  904. frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  905. }
  906. pdfViewerControl.PDFViewTool.GetCPDFViewer().SetMouseImageMaxSize(200, 300);
  907. pdfViewerControl.SetStampMouseImage(imageArray, frame.PixelWidth, frame.PixelHeight);
  908. }
  909. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  910. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP);
  911. pdfViewerControl.SetIsVisibleCustomMouse(true);
  912. pdfViewerControl.SetIsShowStampMouse(true);
  913. pdfViewerControl.SetAnnotParam(stampParam);
  914. }
  915. }
  916. catch
  917. {
  918. }
  919. }
  920. else
  921. {
  922. pdfViewerControl.SetToolType(ToolType.Pan);
  923. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  924. }
  925. ClearPanel();
  926. break;
  927. default:
  928. break;
  929. }
  930. }
  931. private void PdfFreehandUI_EraseChangeHandler(object sender, double e)
  932. {
  933. if (pdfViewerControl != null)
  934. {
  935. pdfViewerControl.PDFViewTool.SetEraseZoom(e);
  936. }
  937. }
  938. private void PdfFreehandUI_EraseClickHandler(object sender, bool e)
  939. {
  940. if (e)
  941. {
  942. //pdfViewerControl.PDFToolManager.ClearSelect();
  943. pdfViewerControl.PDFToolManager.SetToolType(ToolType.Customize);
  944. }
  945. else
  946. {
  947. pdfViewerControl.SetToolType(ToolType.CreateAnnot);
  948. pdfViewerControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  949. }
  950. }
  951. public void CreatTempAnnotationPanel(BaseAnnot baseAnnot)
  952. {
  953. switch (baseAnnot.CurrentType)
  954. {
  955. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  956. {
  957. tempAnnotationPanel = new CPDFMarkupUI();
  958. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Highlight);
  959. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  960. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  961. baseAnnot.GetAnnotData().PageIndex,
  962. baseAnnot.GetAnnotData().Annot);
  963. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  964. }
  965. break;
  966. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  967. {
  968. tempAnnotationPanel = new CPDFMarkupUI();
  969. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Underline);
  970. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  971. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  972. baseAnnot.GetAnnotData().PageIndex,
  973. baseAnnot.GetAnnotData().Annot);
  974. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  975. }
  976. break;
  977. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  978. {
  979. tempAnnotationPanel = new CPDFMarkupUI();
  980. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Strikeout);
  981. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  982. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  983. baseAnnot.GetAnnotData().PageIndex,
  984. baseAnnot.GetAnnotData().Annot);
  985. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  986. }
  987. break;
  988. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  989. {
  990. tempAnnotationPanel = new CPDFMarkupUI();
  991. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationType.Squiggly);
  992. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  993. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  994. baseAnnot.GetAnnotData().PageIndex,
  995. baseAnnot.GetAnnotData().Annot);
  996. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot as CPDFMarkupAnnotation, pdfViewerControl);
  997. }
  998. break;
  999. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  1000. {
  1001. tempAnnotationPanel = new CPDFShapeUI();
  1002. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Square);
  1003. int page = baseAnnot.GetAnnotData().PageIndex;
  1004. var annot = baseAnnot.GetAnnotData().Annot;
  1005. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1006. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1007. baseAnnot.GetAnnotData().PageIndex,
  1008. baseAnnot.GetAnnotData().Annot);
  1009. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1010. }
  1011. break;
  1012. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  1013. {
  1014. tempAnnotationPanel = new CPDFShapeUI();
  1015. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Circle);
  1016. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1017. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1018. baseAnnot.GetAnnotData().PageIndex,
  1019. baseAnnot.GetAnnotData().Annot);
  1020. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1021. }
  1022. break;
  1023. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1024. {
  1025. tempAnnotationPanel = new CPDFShapeUI();
  1026. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationType.Line);
  1027. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1028. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1029. baseAnnot.GetAnnotData().PageIndex,
  1030. baseAnnot.GetAnnotData().Annot);
  1031. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1032. }
  1033. break;
  1034. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1035. {
  1036. CPDFFreehandUI tempFreehandPanel = new CPDFFreehandUI();
  1037. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1038. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1039. baseAnnot.GetAnnotData().PageIndex,
  1040. baseAnnot.GetAnnotData().Annot);
  1041. tempFreehandPanel.SetPresentAnnotAttrib(annotParam, baseAnnot.GetAnnotData().Annot, pdfViewerControl);
  1042. tempFreehandPanel.EraseClickHandler -= PdfFreehandUI_EraseClickHandler;
  1043. tempFreehandPanel.EraseChangeHandler -= PdfFreehandUI_EraseChangeHandler;
  1044. tempFreehandPanel.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  1045. tempFreehandPanel.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  1046. tempAnnotationPanel = tempFreehandPanel;
  1047. }
  1048. break;
  1049. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1050. {
  1051. tempAnnotationPanel = new CPDFFreeTextUI();
  1052. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1053. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1054. baseAnnot.GetAnnotData().PageIndex,
  1055. baseAnnot.GetAnnotData().Annot);
  1056. (tempAnnotationPanel as CPDFFreeTextUI).SetPresentAnnotAttrib(annotParam as FreeTextParam, baseAnnot.GetAnnotData().Annot as CPDFFreeTextAnnotation, pdfViewerControl);
  1057. }
  1058. break;
  1059. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  1060. {
  1061. tempAnnotationPanel = new CPDFNoteUI();
  1062. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1063. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1064. baseAnnot.GetAnnotData().PageIndex,
  1065. baseAnnot.GetAnnotData().Annot);
  1066. (tempAnnotationPanel as CPDFNoteUI).SetPresentAnnotAttrib(annotParam as StickyNoteParam, baseAnnot.GetAnnotData().Annot as CPDFTextAnnotation, pdfViewerControl);
  1067. }
  1068. break;
  1069. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1070. {
  1071. tempAnnotationPanel = new CPDFTempStampUI();
  1072. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1073. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1074. baseAnnot.GetAnnotData().PageIndex,
  1075. baseAnnot.GetAnnotData().Annot);
  1076. (tempAnnotationPanel as CPDFTempStampUI).SetPresentAnnotAttrib(annotParam as StampParam, baseAnnot.GetAnnotData().Annot as CPDFStampAnnotation, pdfViewerControl.PDFToolManager.GetDocument(), pdfViewerControl);
  1077. }
  1078. break;
  1079. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1080. {
  1081. tempAnnotationPanel = new CPDFLinkUI();
  1082. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1083. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1084. baseAnnot.GetAnnotData().PageIndex,
  1085. baseAnnot.GetAnnotData().Annot);
  1086. (tempAnnotationPanel as CPDFLinkUI).SetPresentAnnotAttrib(annotParam as LinkParam, baseAnnot.GetAnnotData().Annot as CPDFLinkAnnotation, pdfViewerControl, pdfViewerControl.PDFToolManager.GetDocument().PageCount);
  1087. }
  1088. break;
  1089. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1090. {
  1091. tempAnnotationPanel = new CPDFCloudUI();
  1092. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(
  1093. pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument(),
  1094. baseAnnot.GetAnnotData().PageIndex,
  1095. baseAnnot.GetAnnotData().Annot
  1096. );
  1097. (tempAnnotationPanel as CPDFCloudUI).SetPresentAnnotAttrib(annotParam as PolygonMeasureParam, baseAnnot.GetAnnotData().Annot as CPDFPolygonAnnotation, pdfViewerControl, pdfViewerControl.PDFToolManager.GetDocument().PageCount);
  1098. }
  1099. break;
  1100. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  1101. tempAnnotationPanel = null;
  1102. break;
  1103. default:
  1104. break;
  1105. }
  1106. }
  1107. public void LoadAnnotationPanel(CPDFAnnotationType annotationType)
  1108. {
  1109. this.pdfViewerControl.SetToolType(ToolType.Pan);
  1110. currentAnnotationType = annotationType;
  1111. annotationPanel = GetAnnotationPanel();
  1112. InitAnnotationPanel(annotationType);
  1113. ShowCurrentAnnotPanel();
  1114. }
  1115. private void ShowCurrentAnnotPanel()
  1116. {
  1117. if (annotationPanel != null)
  1118. {
  1119. if (annotationPanel is CPDFFreehandUI)
  1120. {
  1121. if (pdfViewerControl.PDFToolManager.GetToolType() == ToolType.Customize)
  1122. {
  1123. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(true);
  1124. return;
  1125. }
  1126. else
  1127. {
  1128. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(false);
  1129. }
  1130. }
  1131. SetAnnotationPanel(annotationPanel);
  1132. ExpandPanel();
  1133. ChangeAnnotationData();
  1134. EmptyMessage.Visibility = Visibility.Collapsed;
  1135. }
  1136. else
  1137. {
  1138. EmptyMessage.Visibility = Visibility.Visible;
  1139. SetAnnotationPanel(null);
  1140. if (pdfViewerControl != null)
  1141. {
  1142. EmptyMessage.Visibility = Visibility.Collapsed;
  1143. }
  1144. }
  1145. }
  1146. private void ShowTempAnnotPanel()
  1147. {
  1148. BaseAnnot baseAnnot = pdfViewerControl.GetCacheHitTestAnnot();
  1149. if (baseAnnot != null)
  1150. {
  1151. CreatTempAnnotationPanel(baseAnnot);
  1152. SetAnnotationPanel(tempAnnotationPanel);
  1153. ExpandPanel();
  1154. if (tempAnnotationPanel != null)
  1155. {
  1156. EmptyMessage.Visibility = Visibility.Collapsed;
  1157. }
  1158. else
  1159. {
  1160. EmptyMessage.Visibility = Visibility.Visible;
  1161. }
  1162. }
  1163. }
  1164. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  1165. {
  1166. SetAnnotEventData();
  1167. }
  1168. public void ClearPanel()
  1169. {
  1170. annotationPanel = null;
  1171. SetAnnotationPanel(annotationPanel);
  1172. EmptyMessage.Visibility = Visibility.Visible;
  1173. }
  1174. public void SetAnnotEventData()
  1175. {
  1176. if (pdfViewerControl.GetCacheHitTestAnnot() != null)
  1177. {
  1178. ShowTempAnnotPanel();
  1179. isTempPanel = true;
  1180. }
  1181. else
  1182. {
  1183. if (pdfViewerControl != null && (pdfViewerControl.PDFToolManager.GetToolType() == ToolType.CreateAnnot || pdfViewerControl.PDFToolManager.GetToolType() == ToolType.Customize))
  1184. {
  1185. ShowCurrentAnnotPanel();
  1186. isTempPanel = false;
  1187. }
  1188. else if (annotationPanel is CPDFStampUI && currentAnnotationType == CPDFAnnotationType.Stamp)
  1189. {
  1190. ShowCurrentAnnotPanel();
  1191. }
  1192. else if (annotationPanel is CPDFSignatureUI && currentAnnotationType == CPDFAnnotationType.Signature)
  1193. {
  1194. ShowCurrentAnnotPanel();
  1195. }
  1196. else if (disableClean == false)
  1197. {
  1198. ClearPanel();
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }