CPDFAnnotationControl.xaml.cs 67 KB

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