AnnotationTest.cs 11 KB

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