CPDFAnnotationControl.xaml.cs 60 KB

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