CPDFAnnotationControl.xaml.cs 70 KB

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