AnnotationTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. namespace AnnotationTest
  12. {
  13. internal class AnnotationTest
  14. {
  15. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Annotations";
  16. static void Main(string[] args)
  17. {
  18. Console.WriteLine("Running Annotation test sample…\r\n");
  19. SDKLicenseHelper.LicenseVerify();
  20. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  21. if (!Directory.Exists(outputPath))
  22. {
  23. Directory.CreateDirectory(outputPath);
  24. }
  25. if (CreateAnnots(document))
  26. {
  27. Console.WriteLine("Create annots done.");
  28. }
  29. else
  30. {
  31. Console.WriteLine("Create annots failed.");
  32. }
  33. Console.WriteLine("--------------------");
  34. CPDFDocument annotsDocument = CPDFDocument.InitWithFilePath("Annotations.pdf");
  35. if (DeleteAnnotations(annotsDocument))
  36. {
  37. Console.WriteLine("Create annots done.");
  38. }
  39. else
  40. {
  41. Console.WriteLine("Create annots failed.");
  42. }
  43. Console.WriteLine("--------------------");
  44. Console.WriteLine("Done");
  45. Console.WriteLine("--------------------");
  46. Console.ReadLine();
  47. }
  48. /// <summary>
  49. /// Create freetext annotation
  50. /// </summary>
  51. static private void CreateFreetextAnnotation(CPDFDocument document)
  52. {
  53. CPDFPage page = document.PageAtIndex(0);
  54. string str = "ComPDFKit Samples";
  55. CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  56. freeText.SetContent(str);
  57. freeText.SetRect(new CRect(0, 100, 160, 0));
  58. CTextAttribute textAttribute = new CTextAttribute();
  59. textAttribute.FontName = "Helvetica";
  60. textAttribute.FontSize = 12;
  61. byte[] fontColor = { 255, 0, 0 };
  62. textAttribute.FontColor = fontColor;
  63. freeText.SetFreetextDa(textAttribute);
  64. freeText.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  65. freeText.UpdateAp();
  66. }
  67. /// <summary>
  68. /// Create freehand annotations
  69. /// </summary>
  70. /// <param name="document"></param>
  71. static private void CreateFreehandAnnotation(CPDFDocument document)
  72. {
  73. CPDFPage page = document.PageAtIndex(0);
  74. CPDFInkAnnotation ink = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
  75. ink.SetInkColor(new byte[] { 255, 0, 0 });
  76. ink.SetBorderWidth(2);
  77. ink.SetTransparency(128);
  78. List<List<CPoint>> points = new List<List<CPoint>>();
  79. ink.SetInkPath(points);
  80. ink.SetThickness(8);
  81. points.Clear();
  82. points.Add(new List<CPoint>()
  83. {
  84. new CPoint(10,100),
  85. new CPoint(100,10),
  86. });
  87. ink.SetInkPath(points);
  88. ink.SetInkRect(new CRect(10, 10, 200, 200));
  89. ink.UpdateAp();
  90. }
  91. /// <summary>
  92. /// Create Shape annotations
  93. /// Include:
  94. /// Square, Circle, Line
  95. /// </summary>
  96. /// <param name="document"></param>
  97. static private void CreateShapeAnnotation(CPDFDocument document)
  98. {
  99. CPDFPage page = document.PageAtIndex(0);
  100. float[] dashArray = { 2, 1 };
  101. byte[] lineColor = { 255, 0, 0 };
  102. byte[] bgColor = { 0, 255, 0 };
  103. // Square
  104. CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
  105. square.SetRect(new CRect(10, 250, 200, 200));
  106. square.SetLineColor(lineColor);
  107. square.SetBgColor(bgColor);
  108. square.SetTransparency(120);
  109. square.SetLineWidth(1);
  110. square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  111. square.UpdateAp();
  112. // Circle
  113. CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
  114. circle.SetRect(new CRect(10, 300, 110, 410));
  115. circle.SetLineColor(lineColor);
  116. circle.SetBgColor(bgColor);
  117. circle.SetTransparency(120);
  118. circle.SetLineWidth(1);
  119. circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  120. circle.UpdateAp();
  121. // Line
  122. CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  123. line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
  124. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
  125. line.SetLineColor(lineColor);
  126. line.SetTransparency(120);
  127. line.SetLineWidth(1);
  128. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  129. line.UpdateAp();
  130. }
  131. /// <summary>
  132. /// Create note annotations
  133. /// </summary>
  134. /// <param name="document"></param>
  135. static private void CreateNoteAnnotation(CPDFDocument document)
  136. {
  137. CPDFPage page = document.PageAtIndex(0);
  138. CPDFTextAnnotation textAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  139. textAnnotation.SetColor(new byte[] { 255, 0, 0 });
  140. textAnnotation.SetTransparency(255);
  141. textAnnotation.SetContent("ComPDFKit");
  142. textAnnotation.SetRect(new CRect(300, 600, 350, 650));
  143. textAnnotation.UpdateAp();
  144. }
  145. /// <summary>
  146. /// Create sound annotations
  147. /// </summary>
  148. /// <param name="document"></param>
  149. static private void CreateSoundAnnotation(CPDFDocument document)
  150. {
  151. CPDFPage page = document.PageAtIndex(0);
  152. CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
  153. sound.SetRect(new CRect(400, 700, 450, 750));
  154. sound.SetSoundPath("Bird.wav");
  155. sound.UpdateAp();
  156. }
  157. /// <summary>
  158. /// Create Markup annotations
  159. /// </summary>
  160. /// <param name="document"></param>
  161. static private void CreateMarkupAnnotation(CPDFDocument document)
  162. {
  163. List<CRect> cRectList = new List<CRect>();
  164. CRect rect = new CRect(300, 240, 400, 300);
  165. cRectList.Add(rect);
  166. byte[] color = { 255, 0, 0 };
  167. //highlight
  168. CPDFPage page1 = document.PageAtIndex(0);
  169. CPDFHighlightAnnotation highlight = page1.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  170. highlight.SetColor(color);
  171. highlight.SetTransparency(120);
  172. highlight.SetQuardRects(cRectList);
  173. highlight.UpdateAp();
  174. //underline
  175. CPDFPage page2 = document.PageAtIndex(1);
  176. CPDFUnderlineAnnotation underline = page2.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE) as CPDFUnderlineAnnotation;
  177. underline.SetColor(color);
  178. underline.SetTransparency(120);
  179. underline.SetQuardRects(cRectList);
  180. underline.UpdateAp();
  181. //strikeout
  182. CPDFPage page3 = document.PageAtIndex(2);
  183. CPDFStrikeoutAnnotation strikeout = page3.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT) as CPDFStrikeoutAnnotation;
  184. strikeout.SetColor(color);
  185. strikeout.SetTransparency(120);
  186. strikeout.SetQuardRects(cRectList);
  187. strikeout.UpdateAp();
  188. //squiggly
  189. CPDFPage page4 = document.PageAtIndex(3);
  190. CPDFSquigglyAnnotation squiggy = page4.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY) as CPDFSquigglyAnnotation;
  191. squiggy.SetColor(color);
  192. squiggy.SetTransparency(120);
  193. squiggy.SetQuardRects(cRectList);
  194. squiggy.UpdateAp();
  195. }
  196. public static byte[] BitmapToByteArray(Bitmap bitmap)
  197. {
  198. BitmapData bmpdata = null;
  199. try
  200. {
  201. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  202. int numbytes = bmpdata.Stride * bitmap.Height;
  203. byte[] bytedata = new byte[numbytes];
  204. IntPtr ptr = bmpdata.Scan0;
  205. Marshal.Copy(ptr, bytedata, 0, numbytes);
  206. return bytedata;
  207. }
  208. finally
  209. {
  210. if (bmpdata != null)
  211. bitmap.UnlockBits(bmpdata);
  212. }
  213. }
  214. /// <summary>
  215. /// Create stamp annotation
  216. /// </summary>
  217. /// <param name="document"></param>
  218. static private void CreateStampAnnotation(CPDFDocument document)
  219. {
  220. CPDFPage page = document.PageAtIndex(0);
  221. // Standard
  222. CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  223. standard.SetStandardStamp("Approved");
  224. standard.SetRect(new CRect(300, 100, 450, 160));
  225. standard.UpdateAp();
  226. // Text
  227. CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  228. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
  229. text.SetRect(new CRect(300, 220, 450, 300));
  230. text.UpdateAp();
  231. // Image
  232. Bitmap bitmap = new Bitmap("logo.png");
  233. CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  234. image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
  235. image.SetRect(new CRect(300, 320, 380, 400));
  236. image.SetTransparency(255);
  237. image.UpdateAp();
  238. }
  239. /// <summary>
  240. /// Create annotations
  241. /// </summary>
  242. /// <param name="document"></param>
  243. /// <returns></returns>
  244. static private bool CreateAnnots(CPDFDocument document)
  245. {
  246. CreateFreetextAnnotation(document);
  247. CreateFreehandAnnotation(document);
  248. CreateShapeAnnotation(document);
  249. CreateNoteAnnotation(document);
  250. CreateShapeAnnotation(document);
  251. CreateSoundAnnotation(document);
  252. CreateMarkupAnnotation(document);
  253. CreateStampAnnotation(document);
  254. string path = outputPath + "\\CreateAnnotsTest.pdf";
  255. if (!document.WriteToFilePath(path))
  256. {
  257. return false;
  258. }
  259. Console.WriteLine("Browse the changed file in " + path);
  260. return true;
  261. }
  262. /// <summary>
  263. /// Delete the first annotation
  264. /// </summary>
  265. /// <param name="document"></param>
  266. /// <returns></returns>
  267. static private bool DeleteAnnotations(CPDFDocument document)
  268. {
  269. CPDFPage page = document.PageAtIndex(0);
  270. List<CPDFAnnotation> annotList = page.GetAnnotations();
  271. var annotNum = annotList.Count;
  272. if (!annotList[0].RemoveAnnot())
  273. {
  274. return false;
  275. }
  276. string path = outputPath + "\\DeleteAnnotsTest.pdf";
  277. if (!document.WriteToFilePath(path))
  278. {
  279. return false;
  280. }
  281. Console.WriteLine("Browse the changed file in " + path);
  282. return true;
  283. }
  284. }
  285. }