CPDFAnnotationControl.xaml.cs 68 KB

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