CPDFAnnotationControl.xaml.cs 68 KB

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