CPDFAnnotationControl.xaml.cs 70 KB

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