CPDFAnnotationControl.xaml.cs 65 KB

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