CPDFAnnotationControl.xaml.cs 64 KB

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