CPDFAnnotationControl.xaml.cs 69 KB

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