AnnotationTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  16. static private string outputPath = Path.Combine(parentPath, "Output", "CS");
  17. static void Main(string[] args)
  18. {
  19. #region Preparation work
  20. Console.WriteLine("Running Annotation test sample…" + Environment.NewLine);
  21. SDKLicenseHelper.LicenseVerify();
  22. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  23. if (!Directory.Exists(outputPath))
  24. {
  25. Directory.CreateDirectory(outputPath);
  26. }
  27. #endregion
  28. #region Sample 1: Create annotations
  29. if (CreateAnnots(document))
  30. {
  31. Console.WriteLine("Create annots done.");
  32. }
  33. Console.WriteLine("--------------------");
  34. #endregion
  35. #region Sample 2: Delete annotations
  36. CPDFDocument annotsDocument = CPDFDocument.InitWithFilePath("Annotations.pdf");
  37. if (DeleteAnnotations(annotsDocument))
  38. {
  39. Console.WriteLine("Create annots done.");
  40. }
  41. Console.WriteLine("--------------------");
  42. #endregion
  43. Console.WriteLine("Done");
  44. Console.WriteLine("--------------------");
  45. Console.ReadLine();
  46. }
  47. /// <summary>
  48. /// Create freetext annotation
  49. /// </summary>
  50. static private void CreateFreetextAnnotation(CPDFDocument document)
  51. {
  52. CPDFPage page = document.PageAtIndex(0);
  53. CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  54. string str = "ComPDFKit Samples";
  55. freeText.SetContent(str);
  56. freeText.SetRect(new CRect(0, 100, 160, 0));
  57. CTextAttribute textAttribute = new CTextAttribute();
  58. textAttribute.FontName = "Helvetica";
  59. textAttribute.FontSize = 12;
  60. byte[] fontColor = { 255, 0, 0 };
  61. textAttribute.FontColor = fontColor;
  62. freeText.SetFreetextDa(textAttribute);
  63. freeText.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  64. freeText.UpdateAp();
  65. }
  66. /// <summary>
  67. /// Create freehand annotations
  68. /// </summary>
  69. /// <param name="document"></param>
  70. static private void CreateFreehandAnnotation(CPDFDocument document)
  71. {
  72. CPDFPage page = document.PageAtIndex(0);
  73. CPDFInkAnnotation ink = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
  74. ink.SetInkColor(new byte[] { 255, 0, 0 });
  75. ink.SetBorderWidth(2);
  76. ink.SetTransparency(128);
  77. List<List<CPoint>> points = new List<List<CPoint>>();
  78. ink.SetInkPath(points);
  79. ink.SetThickness(8);
  80. points.Clear();
  81. points.Add(new List<CPoint>()
  82. {
  83. new CPoint(10,100),
  84. new CPoint(100,10),
  85. });
  86. ink.SetInkPath(points);
  87. ink.SetInkRect(new CRect(10, 10, 200, 200));
  88. ink.UpdateAp();
  89. }
  90. /// <summary>
  91. /// Create Shape annotations
  92. /// Include:
  93. /// Square, Circle, Line
  94. /// </summary>
  95. /// <param name="document"></param>
  96. static private void CreateShapeAnnotation(CPDFDocument document)
  97. {
  98. CPDFPage page = document.PageAtIndex(0);
  99. float[] dashArray = { 2, 1 };
  100. byte[] lineColor = { 255, 0, 0 };
  101. byte[] bgColor = { 0, 255, 0 };
  102. // Square
  103. CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
  104. square.SetRect(new CRect(10, 250, 200, 200));
  105. square.SetLineColor(lineColor);
  106. square.SetBgColor(bgColor);
  107. square.SetTransparency(120);
  108. square.SetLineWidth(1);
  109. square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  110. square.UpdateAp();
  111. // Circle
  112. CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
  113. circle.SetRect(new CRect(10, 300, 110, 410));
  114. circle.SetLineColor(lineColor);
  115. circle.SetBgColor(bgColor);
  116. circle.SetTransparency(120);
  117. circle.SetLineWidth(1);
  118. circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  119. circle.UpdateAp();
  120. // Line
  121. CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  122. line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
  123. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
  124. line.SetLineColor(lineColor);
  125. line.SetTransparency(120);
  126. line.SetLineWidth(1);
  127. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  128. line.UpdateAp();
  129. }
  130. /// <summary>
  131. /// Create note annotations
  132. /// </summary>
  133. /// <param name="document"></param>
  134. static private void CreateNoteAnnotation(CPDFDocument document)
  135. {
  136. CPDFPage page = document.PageAtIndex(0);
  137. CPDFTextAnnotation textAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  138. textAnnotation.SetColor(new byte[] { 255, 0, 0 });
  139. textAnnotation.SetTransparency(255);
  140. textAnnotation.SetContent("ComPDFKit");
  141. textAnnotation.SetRect(new CRect(300, 600, 350, 650));
  142. textAnnotation.UpdateAp();
  143. }
  144. /// <summary>
  145. /// Create sound annotations
  146. /// </summary>
  147. /// <param name="document"></param>
  148. static private void CreateSoundAnnotation(CPDFDocument document)
  149. {
  150. CPDFPage page = document.PageAtIndex(0);
  151. CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
  152. sound.SetRect(new CRect(400, 700, 450, 750));
  153. // Note: The iconPath parameter is null.
  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. byte[] imageBytes = File.ReadAllBytes("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. private static void CreateLinkAnnotation(CPDFDocument document)
  240. {
  241. CPDFPage page = document.PageAtIndex(0);
  242. CPDFDestination dest = new CPDFDestination();
  243. dest.PageIndex = 1;
  244. CPDFLinkAnnotation link = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
  245. link.SetRect(new CRect(0, 50, 50, 0));
  246. link.SetDestination(document, dest);
  247. }
  248. /// <summary>
  249. /// Create annotations
  250. /// </summary>
  251. /// <param name="document"></param>
  252. /// <returns></returns>
  253. static private bool CreateAnnots(CPDFDocument document)
  254. {
  255. CreateFreetextAnnotation(document);
  256. CreateFreehandAnnotation(document);
  257. CreateShapeAnnotation(document);
  258. CreateNoteAnnotation(document);
  259. CreateShapeAnnotation(document);
  260. CreateSoundAnnotation(document);
  261. CreateMarkupAnnotation(document);
  262. CreateStampAnnotation(document);
  263. CreateLinkAnnotation(document);
  264. string path = Path.Combine(outputPath, "CreateAnnotsTest.pdf");
  265. if (!document.WriteToFilePath(path))
  266. {
  267. return false;
  268. }
  269. Console.WriteLine("Browse the changed file in " + path);
  270. return true;
  271. }
  272. /// <summary>
  273. /// Delete the first annotation
  274. /// </summary>
  275. /// <param name="document"></param>
  276. /// <returns></returns>
  277. static private bool DeleteAnnotations(CPDFDocument document)
  278. {
  279. CPDFPage page = document.PageAtIndex(0);
  280. List<CPDFAnnotation> annotList = page.GetAnnotations();
  281. if (!annotList[0].RemoveAnnot())
  282. {
  283. return false;
  284. }
  285. string path = Path.Combine(outputPath , "DeleteAnnotsTest.pdf");
  286. if (!document.WriteToFilePath(path))
  287. {
  288. return false;
  289. }
  290. Console.WriteLine("Browse the changed file in " + path);
  291. return true;
  292. }
  293. }
  294. }