CPDFAnnotationControl.xaml.cs 68 KB

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