CPDFAnnotationControl.xaml.cs 66 KB

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