CPDFAnnotationControl.xaml.cs 39 KB

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