AnnotationTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /// <summary>
  72. /// Create freehand annotations
  73. /// </summary>
  74. /// <param name="document"></param>
  75. static private void CreateFreehandAnnotation(CPDFDocument document)
  76. {
  77. CPDFPage page = document.PageAtIndex(0);
  78. CPDFInkAnnotation ink = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
  79. ink.SetInkColor(new byte[] { 255, 0, 0 });
  80. ink.SetBorderWidth(2);
  81. ink.SetTransparency(128);
  82. List<List<CPoint>> points = new List<List<CPoint>>();
  83. ink.SetInkPath(points);
  84. ink.SetThickness(8);
  85. points.Clear();
  86. points.Add(new List<CPoint>()
  87. {
  88. new CPoint(10,100),
  89. new CPoint(100,10),
  90. });
  91. ink.SetInkPath(points);
  92. ink.SetInkRect(new CRect(10, 10, 200, 200));
  93. ink.UpdateAp();
  94. }
  95. /// <summary>
  96. /// Create Shape annotations
  97. /// Include:
  98. /// Square, Circle, Line
  99. /// </summary>
  100. /// <param name="document"></param>
  101. static private void CreateShapeAnnotation(CPDFDocument document)
  102. {
  103. CPDFPage page = document.PageAtIndex(0);
  104. float[] dashArray = { 2, 1 };
  105. byte[] lineColor = { 255, 0, 0 };
  106. byte[] bgColor = { 0, 255, 0 };
  107. // Square
  108. CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
  109. square.SetRect(new CRect(10, 250, 200, 200));
  110. square.SetLineColor(lineColor);
  111. square.SetBgColor(bgColor);
  112. square.SetTransparency(120);
  113. square.SetLineWidth(1);
  114. square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  115. square.UpdateAp();
  116. // Circle
  117. CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
  118. circle.SetRect(new CRect(10, 300, 110, 410));
  119. circle.SetLineColor(lineColor);
  120. circle.SetBgColor(bgColor);
  121. circle.SetTransparency(120);
  122. circle.SetLineWidth(1);
  123. circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  124. circle.UpdateAp();
  125. // Line
  126. CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  127. line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
  128. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
  129. line.SetLineColor(lineColor);
  130. line.SetTransparency(120);
  131. line.SetLineWidth(1);
  132. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  133. line.UpdateAp();
  134. }
  135. /// <summary>
  136. /// Create note annotations
  137. /// </summary>
  138. /// <param name="document"></param>
  139. static private void CreateNoteAnnotation(CPDFDocument document)
  140. {
  141. CPDFPage page = document.PageAtIndex(0);
  142. CPDFTextAnnotation textAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  143. textAnnotation.SetColor(new byte[] { 255, 0, 0 });
  144. textAnnotation.SetTransparency(255);
  145. textAnnotation.SetContent("ComPDFKit");
  146. textAnnotation.SetRect(new CRect(300, 600, 350, 650));
  147. textAnnotation.UpdateAp();
  148. }
  149. /// <summary>
  150. /// Create sound annotations
  151. /// </summary>
  152. /// <param name="document"></param>
  153. static private void CreateSoundAnnotation(CPDFDocument document)
  154. {
  155. CPDFPage page = document.PageAtIndex(0);
  156. CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
  157. sound.SetRect(new CRect(400, 700, 450, 750));
  158. sound.SetSoundPath("Bird.wav");
  159. sound.UpdateAp();
  160. }
  161. /// <summary>
  162. /// Create Markup annotations
  163. /// </summary>
  164. /// <param name="document"></param>
  165. static private void CreateMarkupAnnotation(CPDFDocument document)
  166. {
  167. List<CRect> cRectList = new List<CRect>();
  168. CRect rect = new CRect(300, 240, 400, 300);
  169. cRectList.Add(rect);
  170. byte[] color = { 255, 0, 0 };
  171. //highlight
  172. CPDFPage page1 = document.PageAtIndex(0);
  173. CPDFHighlightAnnotation highlight = page1.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  174. highlight.SetColor(color);
  175. highlight.SetTransparency(120);
  176. highlight.SetQuardRects(cRectList);
  177. highlight.UpdateAp();
  178. //underline
  179. CPDFPage page2 = document.PageAtIndex(1);
  180. CPDFUnderlineAnnotation underline = page2.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE) as CPDFUnderlineAnnotation;
  181. underline.SetColor(color);
  182. underline.SetTransparency(120);
  183. underline.SetQuardRects(cRectList);
  184. underline.UpdateAp();
  185. //strikeout
  186. CPDFPage page3 = document.PageAtIndex(2);
  187. CPDFStrikeoutAnnotation strikeout = page3.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT) as CPDFStrikeoutAnnotation;
  188. strikeout.SetColor(color);
  189. strikeout.SetTransparency(120);
  190. strikeout.SetQuardRects(cRectList);
  191. strikeout.UpdateAp();
  192. //squiggly
  193. CPDFPage page4 = document.PageAtIndex(3);
  194. CPDFSquigglyAnnotation squiggy = page4.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY) as CPDFSquigglyAnnotation;
  195. squiggy.SetColor(color);
  196. squiggy.SetTransparency(120);
  197. squiggy.SetQuardRects(cRectList);
  198. squiggy.UpdateAp();
  199. }
  200. public static byte[] BitmapToByteArray(Bitmap bitmap)
  201. {
  202. BitmapData bmpdata = null;
  203. try
  204. {
  205. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  206. int numbytes = bmpdata.Stride * bitmap.Height;
  207. byte[] bytedata = new byte[numbytes];
  208. IntPtr ptr = bmpdata.Scan0;
  209. Marshal.Copy(ptr, bytedata, 0, numbytes);
  210. return bytedata;
  211. }
  212. finally
  213. {
  214. if (bmpdata != null)
  215. bitmap.UnlockBits(bmpdata);
  216. }
  217. }
  218. /// <summary>
  219. /// Create stamp annotation
  220. /// </summary>
  221. /// <param name="document"></param>
  222. static private void CreateStampAnnotation(CPDFDocument document)
  223. {
  224. CPDFPage page = document.PageAtIndex(0);
  225. // Standard
  226. CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  227. standard.SetStandardStamp("Approved");
  228. standard.SetRect(new CRect(300,100, 450, 160));
  229. standard.UpdateAp();
  230. // Text
  231. CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  232. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
  233. text.SetRect(new CRect(300, 220, 450, 300));
  234. text.UpdateAp();
  235. // Image
  236. Bitmap bitmap = new Bitmap("logo.png");
  237. CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  238. image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
  239. image.SetRect(new CRect(300, 320, 380, 400));
  240. image.SetTransparency(255);
  241. image.UpdateAp();
  242. }
  243. /// <summary>
  244. /// Create annotations
  245. /// </summary>
  246. /// <param name="document"></param>
  247. /// <returns></returns>
  248. static private bool CreateAnnots(CPDFDocument document)
  249. {
  250. CreateFreetextAnnotation(document);
  251. CreateFreehandAnnotation(document);
  252. CreateShapeAnnotation(document);
  253. CreateNoteAnnotation(document);
  254. CreateShapeAnnotation(document);
  255. CreateSoundAnnotation(document);
  256. CreateMarkupAnnotation(document);
  257. CreateStampAnnotation(document);
  258. string path = outputPath + "\\CreateAnnotsTest.pdf";
  259. if (!document.WriteToFilePath(path))
  260. {
  261. return false;
  262. }
  263. Console.WriteLine("Browse the changed file in " + path);
  264. return true;
  265. }
  266. /// <summary>
  267. /// Delete the first annotation
  268. /// </summary>
  269. /// <param name="document"></param>
  270. /// <returns></returns>
  271. static private bool DeleteAnnotations(CPDFDocument document)
  272. {
  273. CPDFPage page = document.PageAtIndex(0);
  274. List<CPDFAnnotation> annotList = page.GetAnnotations();
  275. var annotNum = annotList.Count;
  276. annotList[0].RemoveAnnot();
  277. if (annotNum - annotList.Count != 1)
  278. {
  279. return false;
  280. }
  281. string path = outputPath + "\\DeleteAnnotsTest.pdf";
  282. if (!document.WriteToFilePath(path))
  283. {
  284. return false;
  285. }
  286. Console.WriteLine("Browse the changed file in " + path);
  287. return true;
  288. }
  289. }
  290. }