CPDFAnnotationControl.xaml.cs 67 KB

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