AnnotationTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. string str = "ComPDFKit Samples";
  54. CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  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. sound.SetSoundPath("Bird.wav");
  154. sound.UpdateAp();
  155. }
  156. /// <summary>
  157. /// Create Markup annotations
  158. /// </summary>
  159. /// <param name="document"></param>
  160. static private void CreateMarkupAnnotation(CPDFDocument document)
  161. {
  162. List<CRect> cRectList = new List<CRect>();
  163. CRect rect = new CRect(300, 240, 400, 300);
  164. cRectList.Add(rect);
  165. byte[] color = { 255, 0, 0 };
  166. //highlight
  167. CPDFPage page1 = document.PageAtIndex(0);
  168. CPDFHighlightAnnotation highlight = page1.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  169. highlight.SetColor(color);
  170. highlight.SetTransparency(120);
  171. highlight.SetQuardRects(cRectList);
  172. highlight.UpdateAp();
  173. //underline
  174. CPDFPage page2 = document.PageAtIndex(1);
  175. CPDFUnderlineAnnotation underline = page2.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE) as CPDFUnderlineAnnotation;
  176. underline.SetColor(color);
  177. underline.SetTransparency(120);
  178. underline.SetQuardRects(cRectList);
  179. underline.UpdateAp();
  180. //strikeout
  181. CPDFPage page3 = document.PageAtIndex(2);
  182. CPDFStrikeoutAnnotation strikeout = page3.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT) as CPDFStrikeoutAnnotation;
  183. strikeout.SetColor(color);
  184. strikeout.SetTransparency(120);
  185. strikeout.SetQuardRects(cRectList);
  186. strikeout.UpdateAp();
  187. //squiggly
  188. CPDFPage page4 = document.PageAtIndex(3);
  189. CPDFSquigglyAnnotation squiggy = page4.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY) as CPDFSquigglyAnnotation;
  190. squiggy.SetColor(color);
  191. squiggy.SetTransparency(120);
  192. squiggy.SetQuardRects(cRectList);
  193. squiggy.UpdateAp();
  194. }
  195. public static byte[] BitmapToByteArray(Bitmap bitmap)
  196. {
  197. BitmapData bmpdata = null;
  198. try
  199. {
  200. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  201. int numbytes = bmpdata.Stride * bitmap.Height;
  202. byte[] bytedata = new byte[numbytes];
  203. IntPtr ptr = bmpdata.Scan0;
  204. Marshal.Copy(ptr, bytedata, 0, numbytes);
  205. return bytedata;
  206. }
  207. finally
  208. {
  209. if (bmpdata != null)
  210. bitmap.UnlockBits(bmpdata);
  211. }
  212. }
  213. /// <summary>
  214. /// Create stamp annotation
  215. /// </summary>
  216. /// <param name="document"></param>
  217. static private void CreateStampAnnotation(CPDFDocument document)
  218. {
  219. CPDFPage page = document.PageAtIndex(0);
  220. // Standard
  221. CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  222. standard.SetStandardStamp("Approved");
  223. standard.SetRect(new CRect(300, 100, 450, 160));
  224. standard.UpdateAp();
  225. // Text
  226. CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  227. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
  228. text.SetRect(new CRect(300, 220, 450, 300));
  229. text.UpdateAp();
  230. // Image
  231. Bitmap bitmap = new Bitmap("logo.png");
  232. CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  233. image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
  234. image.SetRect(new CRect(300, 320, 380, 400));
  235. image.SetTransparency(255);
  236. image.UpdateAp();
  237. }
  238. /// <summary>
  239. /// Create annotations
  240. /// </summary>
  241. /// <param name="document"></param>
  242. /// <returns></returns>
  243. static private bool CreateAnnots(CPDFDocument document)
  244. {
  245. CreateFreetextAnnotation(document);
  246. CreateFreehandAnnotation(document);
  247. CreateShapeAnnotation(document);
  248. CreateNoteAnnotation(document);
  249. CreateShapeAnnotation(document);
  250. CreateSoundAnnotation(document);
  251. CreateMarkupAnnotation(document);
  252. CreateStampAnnotation(document);
  253. string path = Path.Combine(outputPath, "CreateAnnotsTest.pdf");
  254. if (!document.WriteToFilePath(path))
  255. {
  256. return false;
  257. }
  258. Console.WriteLine("Browse the changed file in " + path);
  259. return true;
  260. }
  261. /// <summary>
  262. /// Delete the first annotation
  263. /// </summary>
  264. /// <param name="document"></param>
  265. /// <returns></returns>
  266. static private bool DeleteAnnotations(CPDFDocument document)
  267. {
  268. CPDFPage page = document.PageAtIndex(0);
  269. List<CPDFAnnotation> annotList = page.GetAnnotations();
  270. var annotNum = annotList.Count;
  271. if (!annotList[0].RemoveAnnot())
  272. {
  273. return false;
  274. }
  275. string path = Path.Combine(outputPath , "DeleteAnnotsTest.pdf");
  276. if (!document.WriteToFilePath(path))
  277. {
  278. return false;
  279. }
  280. Console.WriteLine("Browse the changed file in " + path);
  281. return true;
  282. }
  283. }
  284. }