CPDFAnnotationControl.xaml.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. namespace Compdfkit_Tools.PDFControl
  24. {
  25. public partial class CPDFAnnotationControl : UserControl
  26. {
  27. private bool isTempPanel = false;
  28. private CPDFAnnotationType currentAnnotationType = CPDFAnnotationType.Unknow;
  29. private CPDFViewer pdfViewer;
  30. private UIElement annotationPanel = null;
  31. private UIElement tempAnnotationPanel = null;
  32. private CPDFMarkupUI pdfHighlightUI;
  33. private CPDFMarkupUI pdfUnderlineUI;
  34. private CPDFMarkupUI pdfSquigglyUI;
  35. private CPDFMarkupUI pdfStrikeoutUI;
  36. private CPDFShapeUI pdfSquareUI;
  37. private CPDFShapeUI pdfCircleUI;
  38. private CPDFShapeUI pdfLineUI;
  39. private CPDFShapeUI pdfArrowUI;
  40. private CPDFFreehandUI pdfFreehandUI;
  41. private CPDFFreeTextUI pdfFreeTextUI;
  42. private CPDFNoteUI pdfNoteUI;
  43. private CPDFStampUI pdfStampUI;
  44. private CPDFSignatureUI pdfSignatureUI;
  45. private CPDFLinkUI pdfLinkUI;
  46. private AnnotHandlerEventArgs annotArgs;
  47. private EraseArgs eraseArgs;
  48. private bool disableClean;
  49. public event EventHandler ClearAnnotationBar;
  50. #region Rebuild
  51. private CPDFViewerTool cPDFViewerTool;
  52. CPDFAnnotation cPDFAnnotation;
  53. public void LoadPDFViewToolHandler()
  54. {
  55. cPDFViewerTool.SetViewerModel(ComPDFKit.Tool.ViewerModel.kCreateAnnot);
  56. cPDFViewerTool.MouseLeftButtonDownHandler += CPDFViewerTool_MouseLeftButtonDownHandler;
  57. cPDFViewerTool.MouseLeftButtonUpHandler += CPDFViewerTool_MouseLeftButtonUpHandler;
  58. cPDFViewerTool.MouseMoveHandler += CPDFViewerTool_MouseMoveHandler;
  59. }
  60. private void CPDFViewerTool_MouseMoveHandler(object sender, MouseHitTeseType e)
  61. {
  62. if (e == ComPDFKit.Tool.MouseHitTeseType.kSelectRect)
  63. {
  64. }
  65. else
  66. {
  67. cPDFViewerTool.MoveDrawAnnot();
  68. }
  69. }
  70. private void CPDFViewerTool_MouseLeftButtonUpHandler(object sender, MouseHitTeseType e)
  71. {
  72. if (e == ComPDFKit.Tool.MouseHitTeseType.kSelectRect)
  73. {
  74. }
  75. else
  76. {
  77. Rect rect = cPDFViewerTool.EndDrawAnnot();
  78. if (cPDFAnnotation != null)
  79. {
  80. Rect maxRect = cPDFViewerTool.GetDrawAnnotMaxRect();
  81. double zoom = cPDFViewerTool.GetCPDFViewer().GetZoom();
  82. Rect rect1 = new Rect(
  83. (rect.Left - maxRect.X) / zoom,
  84. (rect.Top - maxRect.Y) / zoom,
  85. rect.Width / zoom,
  86. rect.Height / zoom
  87. );
  88. Rect rect2 = DpiHelper.StandardRectToPDFRect(rect1);
  89. CRect cRect = new CRect((float)rect2.Left, (float)rect2.Bottom, (float)rect2.Right, (float)rect2.Top);
  90. cPDFAnnotation.SetRect(cRect);
  91. cPDFViewerTool.ClearDrawAnnot();
  92. cPDFViewerTool.GetCPDFViewer().UpDataRenderFrame();
  93. }
  94. }
  95. }
  96. private void CPDFViewerTool_MouseLeftButtonDownHandler(object sender, MouseHitTeseType e)
  97. {
  98. if (e == ComPDFKit.Tool.MouseHitTeseType.kUnknown)
  99. {
  100. cPDFAnnotation = cPDFViewerTool.StartDrawAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE);
  101. if (cPDFAnnotation != null)
  102. {
  103. cPDFViewerTool.CleanSelectedRect();
  104. (cPDFAnnotation as CPDFSquareAnnotation).SetLineWidth(2);
  105. return;
  106. }
  107. }
  108. }
  109. #endregion
  110. public CPDFAnnotationControl()
  111. {
  112. InitializeComponent();
  113. }
  114. public void SetPDFViewer(CPDFViewer pdfViewer,CPDFViewerTool tool)
  115. {
  116. cPDFViewerTool = tool;
  117. if (cPDFViewerTool!=null)
  118. {
  119. LoadPDFViewToolHandler();
  120. }
  121. if (this.pdfViewer != null)
  122. {
  123. UnLoadPDFViewHandler();
  124. }
  125. this.pdfViewer = pdfViewer;
  126. LoadPDFViewHandler();
  127. }
  128. public void LoadPDFViewHandler()
  129. {
  130. if (this.pdfViewer != null)
  131. {
  132. this.pdfViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  133. this.pdfViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  134. this.pdfViewer.AnnotEditHandler -= PdfViewer_AnnotEditHandler;
  135. this.pdfViewer.AnnotEditHandler += PdfViewer_AnnotEditHandler;
  136. }
  137. }
  138. private void PdfViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  139. {
  140. if (e != null && e.Count > 0)
  141. {
  142. if (e[0].EditAction == ActionType.Del)
  143. {
  144. if (pdfViewer.MouseMode == MouseModes.AnnotCreate && pdfViewer.ToolManager.CurrentAnnotArgs is EraseArgs)
  145. {
  146. return;
  147. }
  148. SetAnnotEventData(null);
  149. }
  150. if (e[0].EditAction == ActionType.Modify && e[0].EditAnnotArgs != null && e[0].EditAnnotArgs.EventType == AnnotArgsType.AnnotSticky)
  151. {
  152. CPDFNoteUI tempUI = annotationPanel as CPDFNoteUI;
  153. if (tempUI == null || tempUI.annotAttribEvent == null)
  154. {
  155. tempUI = tempAnnotationPanel as CPDFNoteUI;
  156. }
  157. if (tempUI == null || tempUI.annotAttribEvent == null)
  158. {
  159. tempUI = pdfNoteUI;
  160. }
  161. if (tempUI != null && tempUI.annotAttribEvent != null)
  162. {
  163. AnnotAttribEvent oldEvent = tempUI.annotAttribEvent;
  164. oldEvent.Attribs[AnnotAttrib.NoteText] = e[0].EditAnnotArgs.Content;
  165. tempUI.SetPresentAnnotAttrib(oldEvent);
  166. }
  167. }
  168. if (e[0].EditAction == ActionType.Add && e[0].EditAnnotArgs.EventType == AnnotArgsType.AnnotStamp)
  169. {
  170. pdfViewer.SetMouseMode(MouseModes.PanTool);
  171. }
  172. }
  173. }
  174. public void UnLoadPDFViewHandler()
  175. {
  176. if (this.pdfViewer != null)
  177. {
  178. this.pdfViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  179. }
  180. }
  181. private void AnnotationControl_Loaded(object sender, RoutedEventArgs e)
  182. {
  183. LoadPDFViewHandler();
  184. }
  185. private void AnnotationControl_Unloaded(object sender, RoutedEventArgs e)
  186. {
  187. UnLoadPDFViewHandler();
  188. }
  189. private UIElement GetAnnotationPanel()
  190. {
  191. return AnnotationPanelContainer.Child;
  192. }
  193. private void SetAnnotationPanel(UIElement newChild)
  194. {
  195. AnnotationPanelContainer.Child = newChild;
  196. }
  197. private void ExpandPanel()
  198. {
  199. AnnotationPanelContainer.Visibility = Visibility.Visible;
  200. }
  201. private void ChangeAnnotationData()
  202. {
  203. switch (currentAnnotationType)
  204. {
  205. case CPDFAnnotationType.Highlight:
  206. case CPDFAnnotationType.Underline:
  207. case CPDFAnnotationType.Squiggly:
  208. case CPDFAnnotationType.Strikeout:
  209. (annotationPanel as CPDFMarkupUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  210. (annotationPanel as CPDFMarkupUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  211. SetAnnotationProperty((annotationPanel as CPDFMarkupUI).GetMarkupData());
  212. break;
  213. case CPDFAnnotationType.Square:
  214. case CPDFAnnotationType.Circle:
  215. case CPDFAnnotationType.Line:
  216. case CPDFAnnotationType.Arrow:
  217. (annotationPanel as CPDFShapeUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  218. (annotationPanel as CPDFShapeUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  219. SetAnnotationProperty((annotationPanel as CPDFShapeUI).GetShapeData());
  220. break;
  221. case CPDFAnnotationType.Note:
  222. (annotationPanel as CPDFNoteUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  223. (annotationPanel as CPDFNoteUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  224. SetAnnotationProperty((annotationPanel as CPDFNoteUI).GetNoteData());
  225. break;
  226. case CPDFAnnotationType.Freehand:
  227. (annotationPanel as CPDFFreehandUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  228. (annotationPanel as CPDFFreehandUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  229. SetAnnotationProperty((annotationPanel as CPDFFreehandUI).GetFreehandData());
  230. break;
  231. case CPDFAnnotationType.FreeText:
  232. (annotationPanel as CPDFFreeTextUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  233. (annotationPanel as CPDFFreeTextUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  234. SetAnnotationProperty((annotationPanel as CPDFFreeTextUI).GetFreeTextData());
  235. break;
  236. case CPDFAnnotationType.Stamp:
  237. (annotationPanel as CPDFStampUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  238. (annotationPanel as CPDFStampUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  239. break;
  240. case CPDFAnnotationType.Signature:
  241. (annotationPanel as CPDFSignatureUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  242. (annotationPanel as CPDFSignatureUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  243. break;
  244. default:
  245. break;
  246. }
  247. }
  248. private void SetAnnotationProperty(CPDFAnnotationData pdfAnnotationData = null)
  249. {
  250. if (pdfAnnotationData == null)
  251. {
  252. pdfViewer.SetMouseMode(MouseModes.PanTool);
  253. return;
  254. }
  255. CPDFAnnotationType annotationType = pdfAnnotationData.AnnotationType;
  256. AnnotHandlerEventArgs annotHandlerEventArgs = null;
  257. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  258. switch (annotationType)
  259. {
  260. case CPDFAnnotationType.Highlight:
  261. CPDFMarkupData highlightData = pdfAnnotationData as CPDFMarkupData;
  262. annotHandlerEventArgs = new TextHighlightAnnotArgs();
  263. (annotHandlerEventArgs as TextHighlightAnnotArgs).Color = highlightData.Color;
  264. (annotHandlerEventArgs as TextHighlightAnnotArgs).Transparency = highlightData.Opacity;
  265. (annotHandlerEventArgs as TextHighlightAnnotArgs).Content = highlightData.Note;
  266. (annotHandlerEventArgs as TextHighlightAnnotArgs).Author = highlightData.Author;
  267. (annotHandlerEventArgs as TextHighlightAnnotArgs).Locked = highlightData.IsLocked;
  268. break;
  269. case CPDFAnnotationType.Underline:
  270. CPDFMarkupData underlineData = pdfAnnotationData as CPDFMarkupData;
  271. annotHandlerEventArgs = new TextUnderlineAnnotArgs();
  272. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Color = underlineData.Color;
  273. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Transparency = underlineData.Opacity;
  274. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Author = underlineData.Author;
  275. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Content = underlineData.Note;
  276. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Locked = underlineData.IsLocked;
  277. break;
  278. case CPDFAnnotationType.Strikeout:
  279. CPDFMarkupData strikeoutData = pdfAnnotationData as CPDFMarkupData;
  280. annotHandlerEventArgs = new TextStrikeoutAnnotArgs();
  281. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Color = strikeoutData.Color;
  282. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Transparency = strikeoutData.Opacity;
  283. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Locked = strikeoutData.IsLocked;
  284. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Author = strikeoutData.Author;
  285. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Content = strikeoutData.Note;
  286. break;
  287. case CPDFAnnotationType.Squiggly:
  288. CPDFMarkupData squigglyData = pdfAnnotationData as CPDFMarkupData;
  289. annotHandlerEventArgs = new TextSquigglyAnnotArgs();
  290. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Color = squigglyData.Color;
  291. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Transparency = squigglyData.Opacity;
  292. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Locked = squigglyData.IsLocked;
  293. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Author = squigglyData.Author;
  294. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Content = squigglyData.Note;
  295. break;
  296. case CPDFAnnotationType.Square:
  297. CPDFShapeData squareData = pdfAnnotationData as CPDFShapeData;
  298. annotHandlerEventArgs = new SquareAnnotArgs();
  299. (annotHandlerEventArgs as SquareAnnotArgs).LineColor = squareData.BorderColor;
  300. (annotHandlerEventArgs as SquareAnnotArgs).BgColor = squareData.FillColor;
  301. (annotHandlerEventArgs as SquareAnnotArgs).LineWidth = squareData.Thickness;
  302. (annotHandlerEventArgs as SquareAnnotArgs).Transparency = squareData.Opacity;
  303. (annotHandlerEventArgs as SquareAnnotArgs).LineDash = squareData.DashStyle;
  304. (annotHandlerEventArgs as SquareAnnotArgs).Author = squareData.Author;
  305. (annotHandlerEventArgs as SquareAnnotArgs).Content = squareData.Note;
  306. break;
  307. case CPDFAnnotationType.Circle:
  308. CPDFShapeData cicleData = pdfAnnotationData as CPDFShapeData;
  309. annotHandlerEventArgs = new CircleAnnotArgs();
  310. (annotHandlerEventArgs as CircleAnnotArgs).LineColor = cicleData.BorderColor;
  311. (annotHandlerEventArgs as CircleAnnotArgs).BgColor = cicleData.FillColor;
  312. (annotHandlerEventArgs as CircleAnnotArgs).LineWidth = cicleData.Thickness;
  313. (annotHandlerEventArgs as CircleAnnotArgs).Transparency = cicleData.Opacity;
  314. (annotHandlerEventArgs as CircleAnnotArgs).LineDash = cicleData.DashStyle;
  315. (annotHandlerEventArgs as CircleAnnotArgs).Author = cicleData.Author;
  316. (annotHandlerEventArgs as CircleAnnotArgs).Content = cicleData.Note;
  317. break;
  318. case CPDFAnnotationType.Arrow:
  319. case CPDFAnnotationType.Line:
  320. CPDFLineShapeData lineData = pdfAnnotationData as CPDFLineShapeData;
  321. annotHandlerEventArgs = new LineAnnotArgs();
  322. (annotHandlerEventArgs as LineAnnotArgs).LineColor = lineData.BorderColor;
  323. (annotHandlerEventArgs as LineAnnotArgs).LineWidth = lineData.Thickness;
  324. (annotHandlerEventArgs as LineAnnotArgs).Transparency = lineData.Opacity;
  325. (annotHandlerEventArgs as LineAnnotArgs).LineDash = lineData.DashStyle;
  326. (annotHandlerEventArgs as LineAnnotArgs).HeadLineType = lineData.LineType.HeadLineType;
  327. (annotHandlerEventArgs as LineAnnotArgs).TailLineType = lineData.LineType.TailLineType;
  328. (annotHandlerEventArgs as LineAnnotArgs).Author = lineData.Author;
  329. (annotHandlerEventArgs as LineAnnotArgs).Content = lineData.Note;
  330. break;
  331. case CPDFAnnotationType.Note:
  332. CPDFNoteData noteData = pdfAnnotationData as CPDFNoteData;
  333. annotHandlerEventArgs = new StickyAnnotArgs();
  334. (annotHandlerEventArgs as StickyAnnotArgs).Color = noteData.BorderColor;
  335. (annotHandlerEventArgs as StickyAnnotArgs).StickyNote = noteData.Note;
  336. (annotHandlerEventArgs as StickyAnnotArgs).Transparency = 1;
  337. (annotHandlerEventArgs as StickyAnnotArgs).Author = noteData.Author;
  338. break;
  339. case CPDFAnnotationType.Freehand:
  340. CPDFFreehandData freehandData = pdfAnnotationData as CPDFFreehandData;
  341. annotHandlerEventArgs = new FreehandAnnotArgs();
  342. (annotHandlerEventArgs as FreehandAnnotArgs).InkColor = freehandData.BorderColor;
  343. (annotHandlerEventArgs as FreehandAnnotArgs).LineWidth = freehandData.Thickness;
  344. (annotHandlerEventArgs as FreehandAnnotArgs).Transparency = freehandData.Opacity;
  345. (annotHandlerEventArgs as FreehandAnnotArgs).Content = freehandData.Note;
  346. (annotHandlerEventArgs as FreehandAnnotArgs).Author = freehandData.Author;
  347. break;
  348. case CPDFAnnotationType.FreeText:
  349. CPDFFreeTextData freeTextData = pdfAnnotationData as CPDFFreeTextData;
  350. annotHandlerEventArgs = new FreeTextAnnotArgs();
  351. (annotHandlerEventArgs as FreeTextAnnotArgs).Transparency = freeTextData.Opacity;
  352. (annotHandlerEventArgs as FreeTextAnnotArgs).FontName = freeTextData.FontFamily.ToString();
  353. (annotHandlerEventArgs as FreeTextAnnotArgs).FontColor = freeTextData.BorderColor;
  354. (annotHandlerEventArgs as FreeTextAnnotArgs).IsBold = freeTextData.IsBold;
  355. (annotHandlerEventArgs as FreeTextAnnotArgs).IsItalic = freeTextData.IsItalic;
  356. (annotHandlerEventArgs as FreeTextAnnotArgs).FontSize = freeTextData.FontSize;
  357. (annotHandlerEventArgs as FreeTextAnnotArgs).Align = freeTextData.TextAlignment;
  358. (annotHandlerEventArgs as FreeTextAnnotArgs).Content = freeTextData.Note;
  359. (annotHandlerEventArgs as FreeTextAnnotArgs).Author = freeTextData.Author;
  360. break;
  361. case CPDFAnnotationType.Stamp:
  362. annotHandlerEventArgs = new StampAnnotArgs();
  363. StampAnnotArgs stampAnnot = annotHandlerEventArgs as StampAnnotArgs;
  364. CPDFStampData stampData = pdfAnnotationData as CPDFStampData;
  365. SetStamp(ref stampAnnot, stampData);
  366. break;
  367. case CPDFAnnotationType.Signature:
  368. annotHandlerEventArgs = new StampAnnotArgs();
  369. StampAnnotArgs SignatureAnnot = annotHandlerEventArgs as StampAnnotArgs;
  370. CPDFSignatureData SignatureData = pdfAnnotationData as CPDFSignatureData;
  371. SetSignature(ref SignatureAnnot, SignatureData);
  372. break;
  373. case CPDFAnnotationType.Link:
  374. if (annotHandlerEventArgs != null)
  375. {
  376. pdfViewer.SetToolParam(annotHandlerEventArgs);
  377. }
  378. break;
  379. case CPDFAnnotationType.Unknow:
  380. pdfViewer.SetMouseMode(MouseModes.PanTool);
  381. return;
  382. default:
  383. break;
  384. }
  385. this.pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  386. pdfViewer.SetToolParam(annotHandlerEventArgs);
  387. }
  388. public void SetSignature(ref StampAnnotArgs Args, CPDFSignatureData stamp)
  389. {
  390. switch (stamp.Type)
  391. {
  392. case SignatureType.TextType:
  393. case SignatureType.ImageType:
  394. {
  395. Args.Opacity = 1;
  396. Args.Type = StampType.IMAGE_STAMP;
  397. Args.ImagePath = stamp.SourcePath;
  398. }
  399. break;
  400. case SignatureType.Drawing:
  401. {
  402. Args.SetInkData(GetPoints(stamp.DrawingPath), stamp.inkThickness, stamp.inkColor);
  403. }
  404. break;
  405. default:
  406. break;
  407. }
  408. }
  409. private List<List<Point>> GetPoints(string Path)
  410. {
  411. StrokeCollection Strokes;
  412. List<List<Point>> RawPointList = new List<List<Point>>();
  413. using (FileStream strokeStream = File.OpenRead(Path))
  414. {
  415. Strokes = new StrokeCollection(strokeStream);
  416. }
  417. for (int kk = 0; kk < Strokes.Count; kk++)
  418. {
  419. List<Point> p = new List<Point>();
  420. RawPointList.Add(p);
  421. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  422. {
  423. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  424. if (point.X >= 0 && point.Y >= 0)
  425. RawPointList[kk].Add(point);
  426. }
  427. }
  428. return RawPointList;
  429. }
  430. private void SetStamp(ref StampAnnotArgs Args, CPDFStampData stamp)
  431. {
  432. Args.StampText = stamp.StampText;
  433. Args.Author = stamp.Author;
  434. Args.Opacity = stamp.Opacity;
  435. if (stamp.Type == StampType.IMAGE_STAMP)
  436. {
  437. Args.ImageWidth = stamp.MaxWidth;
  438. Args.ImageHeight = stamp.MaxHeight;
  439. }
  440. else
  441. {
  442. Args.MaxWidth = stamp.MaxWidth;
  443. Args.MaxHeight = stamp.MaxHeight;
  444. }
  445. Args.StampTextDate = stamp.StampTextDate;
  446. Args.TextColor = stamp.TextColor;
  447. Args.TextSharp = stamp.TextSharp;
  448. if (!string.IsNullOrEmpty(stamp.SourcePath))
  449. {
  450. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  451. Args.ImageArray = new byte[image.PixelWidth * image.PixelHeight * 4];
  452. image.CopyPixels(Args.ImageArray, image.PixelWidth * 4, 0);
  453. Args.ImageHeight = image.PixelHeight;
  454. Args.ImageWidth = image.PixelWidth;
  455. }
  456. else
  457. {
  458. try
  459. {
  460. Args.ImageArray = new byte[stamp.ImageSource.PixelWidth * stamp.ImageSource.PixelHeight * 4];
  461. stamp.ImageSource.CopyPixels(Args.ImageArray, stamp.ImageSource.PixelWidth * 4, 0);
  462. Args.ImageHeight = stamp.ImageSource.PixelHeight;
  463. Args.ImageWidth = stamp.ImageSource.PixelWidth;
  464. }
  465. catch
  466. {
  467. var i = pdfStampUI.CustomStampList.IndexOf(stamp);
  468. Settings.Default.CustomStampList.RemoveAt(i);
  469. Settings.Default.Save();
  470. pdfStampUI.LoadSettings();
  471. return;
  472. }
  473. }
  474. Args.Type = stamp.Type;
  475. }
  476. public void AnnotationCancel()
  477. {
  478. this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  479. ClearPanel();
  480. }
  481. private void CPDFAnnotationControl_PropertyChanged(object sender, CPDFAnnotationData e)
  482. {
  483. SetAnnotationProperty(e);
  484. if (pdfViewer != null && pdfViewer.MouseMode == MouseModes.AnnotCreate && pdfViewer.ToolManager.CurrentAnnotArgs is FreehandAnnotArgs)
  485. {
  486. FreehandAnnotArgs freehandArgs = pdfViewer.ToolManager.CurrentAnnotArgs as FreehandAnnotArgs;
  487. CPDFFreehandData freehandData = e as CPDFFreehandData;
  488. if (freehandData != null)
  489. {
  490. freehandArgs.InkColor = freehandData.BorderColor;
  491. freehandArgs.LineWidth = freehandData.Thickness;
  492. freehandArgs.Transparency = freehandData.Opacity;
  493. freehandArgs.Content = freehandData.Note;
  494. freehandArgs.Author = freehandData.Author;
  495. }
  496. }
  497. }
  498. public void InitAnnotationPanel(CPDFAnnotationType annotationType)
  499. {
  500. switch (annotationType)
  501. {
  502. case CPDFAnnotationType.Highlight:
  503. if (pdfHighlightUI == null)
  504. {
  505. pdfHighlightUI = new CPDFMarkupUI();
  506. }
  507. annotationPanel = pdfHighlightUI;
  508. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  509. break;
  510. case CPDFAnnotationType.Underline:
  511. if (pdfUnderlineUI == null)
  512. {
  513. pdfUnderlineUI = new CPDFMarkupUI();
  514. }
  515. annotationPanel = pdfUnderlineUI;
  516. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  517. break;
  518. case CPDFAnnotationType.Strikeout:
  519. if (pdfStrikeoutUI == null)
  520. {
  521. pdfStrikeoutUI = new CPDFMarkupUI();
  522. }
  523. annotationPanel = pdfStrikeoutUI;
  524. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  525. break;
  526. case CPDFAnnotationType.Squiggly:
  527. if (pdfSquigglyUI == null)
  528. {
  529. pdfSquigglyUI = new CPDFMarkupUI();
  530. }
  531. annotationPanel = pdfSquigglyUI;
  532. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  533. break;
  534. case CPDFAnnotationType.Square:
  535. if (pdfSquareUI == null)
  536. {
  537. pdfSquareUI = new CPDFShapeUI();
  538. }
  539. annotationPanel = pdfSquareUI;
  540. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  541. break;
  542. case CPDFAnnotationType.Circle:
  543. if (pdfCircleUI == null)
  544. {
  545. pdfCircleUI = new CPDFShapeUI();
  546. }
  547. annotationPanel = pdfCircleUI;
  548. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  549. break;
  550. case CPDFAnnotationType.Arrow:
  551. if (pdfArrowUI == null)
  552. {
  553. pdfArrowUI = new CPDFShapeUI();
  554. }
  555. annotationPanel = pdfArrowUI;
  556. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  557. break;
  558. case CPDFAnnotationType.Line:
  559. if (pdfLineUI == null)
  560. {
  561. pdfLineUI = new CPDFShapeUI();
  562. }
  563. annotationPanel = pdfLineUI;
  564. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  565. break;
  566. case CPDFAnnotationType.Freehand:
  567. if (pdfFreehandUI == null)
  568. {
  569. pdfFreehandUI = new CPDFFreehandUI();
  570. pdfFreehandUI.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  571. pdfFreehandUI.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  572. }
  573. annotationPanel = pdfFreehandUI;
  574. break;
  575. case CPDFAnnotationType.FreeText:
  576. if (pdfFreeTextUI == null)
  577. {
  578. pdfFreeTextUI = new CPDFFreeTextUI();
  579. }
  580. annotationPanel = pdfFreeTextUI;
  581. break;
  582. case CPDFAnnotationType.Note:
  583. if (pdfNoteUI == null)
  584. {
  585. pdfNoteUI = new CPDFNoteUI();
  586. }
  587. annotationPanel = pdfNoteUI;
  588. break;
  589. case CPDFAnnotationType.Stamp:
  590. if (pdfStampUI == null)
  591. {
  592. pdfStampUI = new CPDFStampUI();
  593. }
  594. annotationPanel = pdfStampUI;
  595. break;
  596. case CPDFAnnotationType.Signature:
  597. if (pdfSignatureUI == null)
  598. {
  599. pdfSignatureUI = new CPDFSignatureUI();
  600. }
  601. annotationPanel = pdfSignatureUI;
  602. break;
  603. case CPDFAnnotationType.Link:
  604. if (pdfLinkUI == null)
  605. {
  606. pdfLinkUI = new CPDFLinkUI();
  607. }
  608. LinkAnnotArgs linkAnnotArgs = new LinkAnnotArgs();
  609. if (this.pdfViewer != null && this.pdfViewer.Document != null)
  610. {
  611. this.pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  612. pdfViewer.SetToolParam(linkAnnotArgs);
  613. pdfLinkUI.InitLinkAnnotArgs(linkAnnotArgs, pdfViewer.Document.PageCount);
  614. }
  615. annotationPanel = pdfLinkUI;
  616. break;
  617. case CPDFAnnotationType.Audio:
  618. SoundAnnotArgs soundArgs = new SoundAnnotArgs();
  619. OpenFileDialog openAudioDialog = new OpenFileDialog();
  620. openAudioDialog.Filter = "Wave Files(*.wav)|*.wav|All Files(*.*;)|*.*;";
  621. if (openAudioDialog.ShowDialog() == true)
  622. {
  623. soundArgs.SoundFilePath = openAudioDialog.FileName;
  624. this.pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  625. pdfViewer.SetToolParam(soundArgs);
  626. }
  627. else
  628. {
  629. this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  630. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  631. }
  632. ClearPanel();
  633. break;
  634. case CPDFAnnotationType.Image:
  635. StampAnnotArgs stampArgs = new StampAnnotArgs();
  636. stampArgs.Opacity = 1;
  637. stampArgs.Type = StampType.IMAGE_STAMP;
  638. OpenFileDialog openFileDialog = new OpenFileDialog();
  639. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  640. if (openFileDialog.ShowDialog() == true)
  641. {
  642. stampArgs.ImagePath = openFileDialog.FileName;
  643. this.pdfViewer?.SetMouseMode(MouseModes.AnnotCreate);
  644. this.pdfViewer?.SetToolParam(stampArgs);
  645. }
  646. else
  647. {
  648. this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  649. }
  650. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  651. ClearPanel();
  652. break;
  653. default:
  654. break;
  655. }
  656. }
  657. private void PdfFreehandUI_EraseChangeHandler(object sender, double e)
  658. {
  659. if (pdfViewer != null && eraseArgs != null)
  660. {
  661. eraseArgs.Thickness = e;
  662. }
  663. }
  664. private void PdfFreehandUI_EraseClickHandler(object sender, bool e)
  665. {
  666. if (pdfViewer != null)
  667. {
  668. CPDFFreehandUI freehandUI = sender as CPDFFreehandUI;
  669. if (e)
  670. {
  671. annotArgs = pdfViewer.ToolManager.CurrentAnnotArgs;
  672. eraseArgs = new EraseArgs();
  673. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  674. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  675. if (freehandUI != null)
  676. {
  677. eraseArgs.Thickness = freehandUI.GetEraseThickness();
  678. }
  679. else
  680. {
  681. eraseArgs.Thickness = 1;
  682. }
  683. disableClean = true;
  684. pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  685. pdfViewer.SetToolParam(eraseArgs);
  686. disableClean = false;
  687. EmptyMessage.Visibility = Visibility.Collapsed;
  688. }
  689. else
  690. {
  691. pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  692. FreehandAnnotArgs freehandAnnotArgs = annotArgs as FreehandAnnotArgs;
  693. if (freehandAnnotArgs == null)
  694. {
  695. freehandAnnotArgs = new FreehandAnnotArgs();
  696. freehandAnnotArgs.InkColor = Colors.Red;
  697. freehandAnnotArgs.Transparency = 1;
  698. freehandAnnotArgs.LineWidth = 1;
  699. annotArgs = freehandAnnotArgs;
  700. }
  701. if (freehandUI != null)
  702. {
  703. freehandUI.PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  704. Dictionary<AnnotAttrib, object> attribDict = new Dictionary<AnnotAttrib, object>();
  705. attribDict[AnnotAttrib.Color] = freehandAnnotArgs.InkColor;
  706. attribDict[AnnotAttrib.Transparency] = freehandAnnotArgs.Transparency;
  707. attribDict[AnnotAttrib.Thickness] = freehandAnnotArgs.LineWidth;
  708. attribDict[AnnotAttrib.NoteText] = freehandAnnotArgs.Content;
  709. AnnotAttribEvent annotEvent = AnnotAttribEvent.GetAnnotAttribEvent(freehandAnnotArgs, attribDict);
  710. freehandUI.SetPresentAnnotAttrib(annotEvent);
  711. freehandUI.PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  712. freehandUI.ClearAnnotAttribEvent();
  713. }
  714. pdfViewer.SetToolParam(freehandAnnotArgs);
  715. }
  716. }
  717. }
  718. public void CreatTempAnnotationPanel(AnnotAttribEvent annotAttribEvent)
  719. {
  720. AnnotArgsType annotArgsType = annotAttribEvent.GetAnnotTypes();
  721. switch (annotArgsType)
  722. {
  723. case AnnotArgsType.AnnotHighlight:
  724. case AnnotArgsType.AnnotUnderline:
  725. case AnnotArgsType.AnnotStrikeout:
  726. case AnnotArgsType.AnnotSquiggly:
  727. tempAnnotationPanel = new CPDFMarkupUI();
  728. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationDictionary.GetAnnotArgsTypeFromAnnotationType[annotArgsType]);
  729. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotAttribEvent);
  730. break;
  731. case AnnotArgsType.AnnotSquare:
  732. case AnnotArgsType.AnnotCircle:
  733. case AnnotArgsType.AnnotLine:
  734. tempAnnotationPanel = new CPDFShapeUI();
  735. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationDictionary.GetAnnotArgsTypeFromAnnotationType[annotArgsType]);
  736. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotAttribEvent);
  737. break;
  738. case AnnotArgsType.AnnotFreehand:
  739. CPDFFreehandUI tempFreehandPanel = new CPDFFreehandUI();
  740. tempFreehandPanel.SetPresentAnnotAttrib(annotAttribEvent);
  741. tempFreehandPanel.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  742. tempFreehandPanel.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  743. tempAnnotationPanel = tempFreehandPanel;
  744. break;
  745. case AnnotArgsType.AnnotFreeText:
  746. tempAnnotationPanel = new CPDFFreeTextUI();
  747. (tempAnnotationPanel as CPDFFreeTextUI).SetPresentAnnotAttrib(annotAttribEvent);
  748. break;
  749. case AnnotArgsType.AnnotSticky:
  750. tempAnnotationPanel = new CPDFNoteUI();
  751. (tempAnnotationPanel as CPDFNoteUI).SetPresentAnnotAttrib(annotAttribEvent);
  752. break;
  753. case AnnotArgsType.AnnotStamp:
  754. tempAnnotationPanel = new CPDFTempStampUI();
  755. (tempAnnotationPanel as CPDFTempStampUI).SetPresentAnnotAttrib(annotAttribEvent);
  756. break;
  757. case AnnotArgsType.AnnotLink:
  758. tempAnnotationPanel = new CPDFLinkUI();
  759. (tempAnnotationPanel as CPDFLinkUI).SetPresentAnnotAttrib(annotAttribEvent, pdfViewer.Document.PageCount);
  760. break;
  761. case AnnotArgsType.AnnotSound:
  762. tempAnnotationPanel = null;
  763. break;
  764. default:
  765. break;
  766. }
  767. }
  768. public void LoadAnnotationPanel(CPDFAnnotationType annotationType)
  769. {
  770. this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  771. currentAnnotationType = annotationType;
  772. annotationPanel = GetAnnotationPanel();
  773. InitAnnotationPanel(annotationType);
  774. ShowCurrentAnnotPanel();
  775. }
  776. private void ShowCurrentAnnotPanel()
  777. {
  778. if (annotationPanel != null)
  779. {
  780. if (annotationPanel is CPDFFreehandUI)
  781. {
  782. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(false);
  783. }
  784. SetAnnotationPanel(annotationPanel);
  785. ExpandPanel();
  786. ChangeAnnotationData();
  787. EmptyMessage.Visibility = Visibility.Collapsed;
  788. }
  789. else
  790. {
  791. EmptyMessage.Visibility = Visibility.Visible;
  792. SetAnnotationPanel(null);
  793. if (pdfViewer != null && pdfViewer.ToolManager.CurrentAnnotArgs is EraseArgs)
  794. {
  795. EmptyMessage.Visibility = Visibility.Collapsed;
  796. }
  797. }
  798. }
  799. private void ShowTempAnnotPanel(AnnotAttribEvent annotAttribEvent)
  800. {
  801. if (annotAttribEvent != null)
  802. {
  803. CreatTempAnnotationPanel(annotAttribEvent);
  804. SetAnnotationPanel(tempAnnotationPanel);
  805. ExpandPanel();
  806. if (tempAnnotationPanel != null)
  807. {
  808. EmptyMessage.Visibility = Visibility.Collapsed;
  809. }
  810. else
  811. {
  812. EmptyMessage.Visibility = Visibility.Visible;
  813. }
  814. }
  815. }
  816. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  817. {
  818. SetAnnotEventData(e);
  819. }
  820. public void ClearPanel()
  821. {
  822. annotationPanel = null;
  823. SetAnnotationPanel(annotationPanel);
  824. EmptyMessage.Visibility = Visibility.Visible;
  825. }
  826. public void SetAnnotEventData(AnnotAttribEvent newData)
  827. {
  828. if (newData != null)
  829. {
  830. if (newData.IsAnnotCreateReset && isTempPanel)
  831. {
  832. ShowCurrentAnnotPanel();
  833. isTempPanel = false;
  834. }
  835. else if (!newData.IsAnnotCreateReset)
  836. {
  837. AnnotArgsType annotArgsType = newData.GetAnnotTypes();
  838. ShowTempAnnotPanel(newData);
  839. isTempPanel = true;
  840. }
  841. }
  842. else
  843. {
  844. if (pdfViewer != null && pdfViewer.MouseMode == MouseModes.AnnotCreate)
  845. {
  846. ShowCurrentAnnotPanel();
  847. isTempPanel = false;
  848. }
  849. else if (annotationPanel is CPDFStampUI && currentAnnotationType == CPDFAnnotationType.Stamp)
  850. {
  851. ShowCurrentAnnotPanel();
  852. }
  853. else if (annotationPanel is CPDFSignatureUI && currentAnnotationType == CPDFAnnotationType.Signature)
  854. {
  855. ShowCurrentAnnotPanel();
  856. }
  857. else if (disableClean == false)
  858. {
  859. ClearPanel();
  860. }
  861. }
  862. }
  863. }
  864. }