AnnotationTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 (CreateAnnots(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. static private void CreateFreetextAnnotation(CPDFDocument document)
  53. {
  54. CPDFPage page = document.PageAtIndex(0);
  55. string str = "ComPDFKit Samples";
  56. CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  57. freeText.SetContent(str);
  58. freeText.SetRect(new CRect(0, 100, 160, 0));
  59. CTextAttribute textAttribute = new CTextAttribute();
  60. textAttribute.FontName = "Helvetica";
  61. textAttribute.FontSize = 12;
  62. byte[] fontColor = { 255, 0, 0 };
  63. textAttribute.FontColor = fontColor;
  64. freeText.SetFreetextDa(textAttribute);
  65. freeText.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  66. freeText.UpdateAp();
  67. }
  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. //inkCore.SetInkPath(inkPathList);
  79. points.Clear();
  80. points.Add(new List<CPoint>()
  81. {
  82. new CPoint(10,100),
  83. new CPoint(100,10),
  84. });
  85. ink.SetInkPath(points);
  86. ink.SetInkRect(new CRect(10, 10, 200, 200));
  87. ink.UpdateAp();
  88. }
  89. static private void CreateShapeAnnotation(CPDFDocument document)
  90. {
  91. CPDFPage page = document.PageAtIndex(0);
  92. float[] dashArray = { 2, 1 };
  93. byte[] lineColor = { 255, 0, 0 };
  94. byte[] bgColor = { 0, 255, 0 };
  95. // Square
  96. CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
  97. square.SetRect(new CRect(10, 250, 200, 200));
  98. square.SetLineColor(lineColor);
  99. square.SetBgColor(bgColor);
  100. square.SetTransparency(120);
  101. square.SetLineWidth(1);
  102. square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  103. square.UpdateAp();
  104. // Circle
  105. CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
  106. circle.SetRect(new CRect(10, 300, 110, 410));
  107. circle.SetLineColor(lineColor);
  108. circle.SetBgColor(bgColor);
  109. circle.SetTransparency(120);
  110. circle.SetLineWidth(1);
  111. circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  112. circle.UpdateAp();
  113. // Line
  114. CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  115. line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
  116. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
  117. line.SetLineColor(lineColor);
  118. line.SetTransparency(120);
  119. line.SetLineWidth(1);
  120. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  121. line.UpdateAp();
  122. }
  123. static private void CreateNoteAnnotation(CPDFDocument document)
  124. {
  125. CPDFPage page = document.PageAtIndex(0);
  126. CPDFTextAnnotation textAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  127. textAnnotation.SetColor(new byte[] { 255, 0, 0 });
  128. textAnnotation.SetTransparency(255);
  129. textAnnotation.SetContent("ComPDFKit");
  130. textAnnotation.SetRect(new CRect(300, 600, 350, 650));
  131. textAnnotation.UpdateAp();
  132. }
  133. static private void CreateSoundAnnotation(CPDFDocument document)
  134. {
  135. CPDFPage page = document.PageAtIndex(0);
  136. CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
  137. sound.SetRect(new CRect(400, 700, 450, 750));
  138. sound.SetSoundPath("Bird.wav");
  139. sound.UpdateAp();
  140. }
  141. static private void CreateMarkupAnnotation(CPDFDocument document)
  142. {
  143. List<CRect> cRectList = new List<CRect>();
  144. CRect rect = new CRect(300, 240, 400, 300);
  145. cRectList.Add(rect);
  146. byte[] color = { 255, 0, 0 };
  147. //highlight
  148. CPDFPage page1 = document.PageAtIndex(0);
  149. CPDFHighlightAnnotation highlight = page1.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  150. highlight.SetColor(color);
  151. highlight.SetTransparency(120);
  152. highlight.SetQuardRects(cRectList);
  153. highlight.UpdateAp();
  154. //underline
  155. CPDFPage page2 = document.PageAtIndex(1);
  156. CPDFUnderlineAnnotation underline = page2.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE) as CPDFUnderlineAnnotation;
  157. underline.SetColor(color);
  158. underline.SetTransparency(120);
  159. underline.SetQuardRects(cRectList);
  160. underline.UpdateAp();
  161. //strikeout
  162. CPDFPage page3 = document.PageAtIndex(2);
  163. CPDFStrikeoutAnnotation strikeout = page3.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT) as CPDFStrikeoutAnnotation;
  164. strikeout.SetColor(color);
  165. strikeout.SetTransparency(120);
  166. strikeout.SetQuardRects(cRectList);
  167. strikeout.UpdateAp();
  168. //squiggly
  169. CPDFPage page4 = document.PageAtIndex(3);
  170. CPDFSquigglyAnnotation squiggy = page4.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY) as CPDFSquigglyAnnotation;
  171. squiggy.SetColor(color);
  172. squiggy.SetTransparency(120);
  173. squiggy.SetQuardRects(cRectList);
  174. squiggy.UpdateAp();
  175. }
  176. public static byte[] BitmapToByteArray(Bitmap bitmap)
  177. {
  178. BitmapData bmpdata = null;
  179. try
  180. {
  181. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  182. int numbytes = bmpdata.Stride * bitmap.Height;
  183. byte[] bytedata = new byte[numbytes];
  184. IntPtr ptr = bmpdata.Scan0;
  185. Marshal.Copy(ptr, bytedata, 0, numbytes);
  186. return bytedata;
  187. }
  188. finally
  189. {
  190. if (bmpdata != null)
  191. bitmap.UnlockBits(bmpdata);
  192. }
  193. }
  194. static private void CreateStampAnnotation(CPDFDocument document)
  195. {
  196. CPDFPage page = document.PageAtIndex(0);
  197. // Standard
  198. CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  199. standard.SetStandardStamp("Approved");
  200. standard.SetRect(new CRect(300,100, 450, 160));
  201. standard.UpdateAp();
  202. // Text
  203. CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  204. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
  205. text.SetRect(new CRect(300, 220, 450, 300));
  206. text.UpdateAp();
  207. // Image
  208. Bitmap bitmap = new Bitmap("logo.png");
  209. CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  210. image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
  211. image.SetRect(new CRect(300, 320, 380, 400));
  212. image.SetTransparency(255);
  213. image.UpdateAp();
  214. }
  215. static private bool CreateAnnots(CPDFDocument document)
  216. {
  217. CreateFreetextAnnotation(document);
  218. CreateFreehandAnnotation(document);
  219. CreateShapeAnnotation(document);
  220. CreateNoteAnnotation(document);
  221. CreateShapeAnnotation(document);
  222. CreateSoundAnnotation(document);
  223. CreateMarkupAnnotation(document);
  224. CreateStampAnnotation(document);
  225. string path = outputPath + "\\CreateAnnotsTest.pdf";
  226. if (!document.WriteToFilePath(path))
  227. {
  228. return false;
  229. }
  230. Console.WriteLine("Browse the changed file in " + path);
  231. return true;
  232. }
  233. static private bool DeleteAnnotations(CPDFDocument document)
  234. {
  235. CPDFPage page = document.PageAtIndex(0);
  236. List<CPDFAnnotation> annotList = page.GetAnnotations();
  237. var annotNum = annotList.Count;
  238. annotList[0].RemoveAnnot();
  239. if (annotNum - annotList.Count != 1)
  240. {
  241. return false;
  242. }
  243. string path = outputPath + "\\DeleteAnnotsTest.pdf";
  244. if (!document.WriteToFilePath(path))
  245. {
  246. return false;
  247. }
  248. Console.WriteLine("Browse the changed file in " + path);
  249. return true;
  250. }
  251. }
  252. }