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.IO;
  8. using ImageMagick;
  9. namespace AnnotationTest
  10. {
  11. internal class AnnotationTest
  12. {
  13. static private string parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  14. static private string outputPath = Path.Combine(parentPath, "Output", "CS");
  15. static void Main(string[] args)
  16. {
  17. #region Preparation work
  18. Console.WriteLine("Running Annotation test sample…" + Environment.NewLine);
  19. SDKLicenseHelper.LicenseVerify();
  20. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  21. if (!Directory.Exists(outputPath))
  22. {
  23. Directory.CreateDirectory(outputPath);
  24. }
  25. #endregion
  26. #region Sample 1: Create annotations
  27. if (CreateAnnots(document))
  28. {
  29. Console.WriteLine("Create annots done.");
  30. }
  31. Console.WriteLine("--------------------");
  32. #endregion
  33. #region Sample 2: Delete annotations
  34. CPDFDocument annotsDocument = CPDFDocument.InitWithFilePath("Annotations.pdf");
  35. if (DeleteAnnotations(annotsDocument))
  36. {
  37. Console.WriteLine("Create annots done.");
  38. }
  39. Console.WriteLine("--------------------");
  40. #endregion
  41. Console.WriteLine("Done");
  42. Console.WriteLine("--------------------");
  43. Console.ReadLine();
  44. }
  45. /// <summary>
  46. /// Create freetext annotation
  47. /// </summary>
  48. static private void CreateFreetextAnnotation(CPDFDocument document)
  49. {
  50. CPDFPage page = document.PageAtIndex(0);
  51. CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  52. string str = "ComPDFKit Samples";
  53. freeText.SetContent(str);
  54. freeText.SetRect(new CRect(0, 100, 160, 0));
  55. CTextAttribute textAttribute = new CTextAttribute();
  56. textAttribute.FontName = "Helvetica";
  57. textAttribute.FontSize = 12;
  58. byte[] fontColor = { 255, 0, 0 };
  59. textAttribute.FontColor = fontColor;
  60. freeText.SetFreetextDa(textAttribute);
  61. freeText.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  62. freeText.UpdateAp();
  63. }
  64. /// <summary>
  65. /// Create freehand annotations
  66. /// </summary>
  67. /// <param name="document"></param>
  68. static private void CreateFreehandAnnotation(CPDFDocument document)
  69. {
  70. CPDFPage page = document.PageAtIndex(0);
  71. CPDFInkAnnotation ink = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
  72. ink.SetInkColor(new byte[] { 255, 0, 0 });
  73. ink.SetBorderWidth(2);
  74. ink.SetTransparency(128);
  75. List<List<CPoint>> points = new List<List<CPoint>>();
  76. ink.SetInkPath(points);
  77. ink.SetThickness(8);
  78. points.Clear();
  79. points.Add(new List<CPoint>()
  80. {
  81. new CPoint(10,100),
  82. new CPoint(100,10),
  83. });
  84. ink.SetInkPath(points);
  85. ink.UpdateAp();
  86. }
  87. /// <summary>
  88. /// Create Shape annotations
  89. /// Include:
  90. /// Square, Circle, Line
  91. /// </summary>
  92. /// <param name="document"></param>
  93. static private void CreateShapeAnnotation(CPDFDocument document)
  94. {
  95. CPDFPage page = document.PageAtIndex(0);
  96. float[] dashArray = { 2, 1 };
  97. byte[] lineColor = { 255, 0, 0 };
  98. byte[] bgColor = { 0, 255, 0 };
  99. // Square
  100. CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
  101. square.SetRect(new CRect(10, 250, 200, 200));
  102. square.SetLineColor(lineColor);
  103. square.SetBgColor(bgColor);
  104. square.SetTransparency(120);
  105. square.SetLineWidth(1);
  106. square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  107. square.UpdateAp();
  108. // Circle
  109. CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
  110. circle.SetRect(new CRect(10, 410, 110, 300));
  111. circle.SetLineColor(lineColor);
  112. circle.SetBgColor(bgColor);
  113. circle.SetTransparency(120);
  114. circle.SetLineWidth(1);
  115. circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  116. circle.UpdateAp();
  117. // Line
  118. CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  119. line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
  120. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
  121. line.SetLineColor(lineColor);
  122. line.SetTransparency(120);
  123. line.SetLineWidth(1);
  124. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  125. line.UpdateAp();
  126. }
  127. /// <summary>
  128. /// Create note annotations
  129. /// </summary>
  130. /// <param name="document"></param>
  131. static private void CreateNoteAnnotation(CPDFDocument document)
  132. {
  133. CPDFPage page = document.PageAtIndex(0);
  134. CPDFTextAnnotation textAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  135. textAnnotation.SetColor(new byte[] { 255, 0, 0 });
  136. textAnnotation.SetTransparency(255);
  137. textAnnotation.SetContent("ComPDFKit");
  138. textAnnotation.SetRect(new CRect(300, 650, 350, 600));
  139. textAnnotation.UpdateAp();
  140. }
  141. /// <summary>
  142. /// Create sound annotations
  143. /// </summary>
  144. /// <param name="document"></param>
  145. static private void CreateSoundAnnotation(CPDFDocument document)
  146. {
  147. CPDFPage page = document.PageAtIndex(1);
  148. CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
  149. sound.SetRect(new CRect(400, 750, 450, 700));
  150. using (var image = new MagickImage("SoundAnnot.png"))
  151. {
  152. byte[] byteArray = image.ToByteArray(MagickFormat.Bgra);
  153. sound.SetSoundPath(byteArray, image.Width, image.Height, "Bird.wav");
  154. }
  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, 300, 400, 240);
  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, 160, 450, 100));
  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, 300, 450, 220));
  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, 400, 380, 320));
  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. }