CPDFAnnotationControl.xaml.cs 63 KB

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