CPDFAnnotationControl.xaml.cs 68 KB

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