AnnotationTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. using ImageMagick;
  12. namespace AnnotationTest
  13. {
  14. internal class AnnotationTest
  15. {
  16. static private string parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  17. static private string outputPath = Path.Combine(parentPath, "Output", "CS");
  18. static void Main(string[] args)
  19. {
  20. #region Preparation work
  21. Console.WriteLine("Running Annotation test sample…" + Environment.NewLine);
  22. SDKLicenseHelper.LicenseVerify();
  23. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  24. if (!Directory.Exists(outputPath))
  25. {
  26. Directory.CreateDirectory(outputPath);
  27. }
  28. #endregion
  29. #region Sample 1: Create annotations
  30. if (CreateAnnots(document))
  31. {
  32. Console.WriteLine("Create annots done.");
  33. }
  34. Console.WriteLine("--------------------");
  35. #endregion
  36. #region Sample 2: Delete annotations
  37. CPDFDocument annotsDocument = CPDFDocument.InitWithFilePath("Annotations.pdf");
  38. if (DeleteAnnotations(annotsDocument))
  39. {
  40. Console.WriteLine("Create annots done.");
  41. }
  42. Console.WriteLine("--------------------");
  43. #endregion
  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. CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  55. string str = "ComPDFKit Samples";
  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(1);
  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(null,"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. /// <summary>
  197. /// Create stamp annotation
  198. /// </summary>
  199. /// <param name="document"></param>
  200. static private void CreateStampAnnotation(CPDFDocument document)
  201. {
  202. CPDFPage page = document.PageAtIndex(0);
  203. // Standard
  204. CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  205. standard.SetStandardStamp("Approved");
  206. standard.SetRect(new CRect(300, 100, 450, 160));
  207. standard.UpdateAp();
  208. // Text
  209. CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  210. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
  211. text.SetRect(new CRect(300, 220, 450, 300));
  212. text.UpdateAp();
  213. // Image
  214. CPDFStampAnnotation stampAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  215. using (var image = new MagickImage("ComPDFKit_Logo.ico"))
  216. {
  217. byte[] byteArray = image.ToByteArray(MagickFormat.Bgra);
  218. stampAnnotation.SetImageStamp(byteArray, image.Width, image.Height);
  219. }
  220. stampAnnotation.SetRect(new CRect(300, 320, 380, 400));
  221. stampAnnotation.SetTransparency(255);
  222. stampAnnotation.UpdateAp();
  223. }
  224. private static void CreateLinkAnnotation(CPDFDocument document)
  225. {
  226. CPDFPage page = document.PageAtIndex(0);
  227. CPDFDestination dest = new CPDFDestination();
  228. dest.PageIndex = 1;
  229. CPDFLinkAnnotation link = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
  230. link.SetRect(new CRect(0, 50, 50, 0));
  231. link.SetDestination(document, dest);
  232. }
  233. /// <summary>
  234. /// Create annotations
  235. /// </summary>
  236. /// <param name="document"></param>
  237. /// <returns></returns>
  238. static private bool CreateAnnots(CPDFDocument document)
  239. {
  240. CreateFreetextAnnotation(document);
  241. CreateFreehandAnnotation(document);
  242. CreateShapeAnnotation(document);
  243. CreateNoteAnnotation(document);
  244. CreateShapeAnnotation(document);
  245. CreateSoundAnnotation(document);
  246. CreateMarkupAnnotation(document);
  247. CreateStampAnnotation(document);
  248. CreateLinkAnnotation(document);
  249. string path = Path.Combine(outputPath, "CreateAnnotsTest.pdf");
  250. if (!document.WriteToFilePath(path))
  251. {
  252. return false;
  253. }
  254. Console.WriteLine("Browse the changed file in " + path);
  255. return true;
  256. }
  257. /// <summary>
  258. /// Delete the first annotation
  259. /// </summary>
  260. /// <param name="document"></param>
  261. /// <returns></returns>
  262. static private bool DeleteAnnotations(CPDFDocument document)
  263. {
  264. CPDFPage page = document.PageAtIndex(0);
  265. List<CPDFAnnotation> annotList = page.GetAnnotations();
  266. if (!annotList[0].RemoveAnnot())
  267. {
  268. return false;
  269. }
  270. string path = Path.Combine(outputPath , "DeleteAnnotsTest.pdf");
  271. if (!document.WriteToFilePath(path))
  272. {
  273. return false;
  274. }
  275. Console.WriteLine("Browse the changed file in " + path);
  276. return true;
  277. }
  278. }
  279. }