AnnotationTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.Drawing.Printing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. namespace AnnotationTest
  14. {
  15. internal class AnnotationTest
  16. {
  17. static private string parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  18. static private string outputPath = Path.Combine(parentPath, "Output", "CS");
  19. static void Main(string[] args)
  20. {
  21. #region Preparation work
  22. Console.WriteLine("Running Annotation test sample…" + Environment.NewLine);
  23. SDKLicenseHelper.LicenseVerify();
  24. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  25. if (!Directory.Exists(outputPath))
  26. {
  27. Directory.CreateDirectory(outputPath);
  28. }
  29. #endregion
  30. #region Sample 1: Create annotations
  31. if (CreateAnnots(document))
  32. {
  33. Console.WriteLine("Create annots done.");
  34. }
  35. Console.WriteLine("--------------------");
  36. #endregion
  37. #region Sample 2: Delete annotations
  38. CPDFDocument annotsDocument = CPDFDocument.InitWithFilePath("Annotations.pdf");
  39. if (DeleteAnnotations(annotsDocument))
  40. {
  41. Console.WriteLine("Create annots done.");
  42. }
  43. Console.WriteLine("--------------------");
  44. #endregion
  45. Console.WriteLine("Done");
  46. Console.WriteLine("--------------------");
  47. Console.ReadLine();
  48. }
  49. /// <summary>
  50. /// Create freetext annotation
  51. /// </summary>
  52. static private void CreateFreetextAnnotation(CPDFDocument document)
  53. {
  54. CPDFPage page = document.PageAtIndex(0);
  55. CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  56. string str = "ComPDFKit Samples";
  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. /// <summary>
  69. /// Create freehand annotations
  70. /// </summary>
  71. /// <param name="document"></param>
  72. static private void CreateFreehandAnnotation(CPDFDocument document)
  73. {
  74. CPDFPage page = document.PageAtIndex(0);
  75. CPDFInkAnnotation ink = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
  76. ink.SetInkColor(new byte[] { 255, 0, 0 });
  77. ink.SetBorderWidth(2);
  78. ink.SetTransparency(128);
  79. List<List<CPoint>> points = new List<List<CPoint>>();
  80. ink.SetInkPath(points);
  81. ink.SetThickness(8);
  82. points.Clear();
  83. points.Add(new List<CPoint>()
  84. {
  85. new CPoint(10,100),
  86. new CPoint(100,10),
  87. });
  88. ink.SetInkPath(points);
  89. ink.SetRect(new CRect(10, 10, 200, 200));
  90. ink.UpdateAp();
  91. }
  92. /// <summary>
  93. /// Create Shape annotations
  94. /// Include:
  95. /// Square, Circle, Line
  96. /// </summary>
  97. /// <param name="document"></param>
  98. static private void CreateShapeAnnotation(CPDFDocument document)
  99. {
  100. CPDFPage page = document.PageAtIndex(0);
  101. float[] dashArray = { 2, 1 };
  102. byte[] lineColor = { 255, 0, 0 };
  103. byte[] bgColor = { 0, 255, 0 };
  104. // Square
  105. CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
  106. square.SetSourceRect(new CRect(10, 250, 200, 200));
  107. square.SetLineColor(lineColor);
  108. square.SetBgColor(bgColor);
  109. square.SetTransparency(120);
  110. square.SetLineWidth(1);
  111. square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  112. square.AnnotationRotator.SetRotation(45);
  113. square.UpdateAp();
  114. // Circle
  115. CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
  116. circle.SetRect(new CRect(10, 300, 110, 410));
  117. circle.SetLineColor(lineColor);
  118. circle.SetBgColor(bgColor);
  119. circle.SetTransparency(120);
  120. circle.SetLineWidth(1);
  121. circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  122. circle.UpdateAp();
  123. // Line
  124. CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  125. line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
  126. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
  127. line.SetLineColor(lineColor);
  128. line.SetTransparency(120);
  129. line.SetLineWidth(1);
  130. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  131. line.UpdateAp();
  132. }
  133. /// <summary>
  134. /// Create note annotations
  135. /// </summary>
  136. /// <param name="document"></param>
  137. static private void CreateNoteAnnotation(CPDFDocument document)
  138. {
  139. CPDFPage page = document.PageAtIndex(0);
  140. CPDFTextAnnotation textAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  141. textAnnotation.SetColor(new byte[] { 255, 0, 0 });
  142. textAnnotation.SetTransparency(255);
  143. textAnnotation.SetContent("ComPDFKit");
  144. textAnnotation.SetRect(new CRect(300, 600, 350, 650));
  145. textAnnotation.UpdateAp();
  146. }
  147. /// <summary>
  148. /// Create sound annotations
  149. /// </summary>
  150. /// <param name="document"></param>
  151. static private void CreateSoundAnnotation(CPDFDocument document)
  152. {
  153. CPDFPage page = document.PageAtIndex(0);
  154. CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
  155. sound.SetRect(new CRect(400, 700, 450, 750));
  156. sound.SetSoundPath("", "Bird.wav");
  157. sound.UpdateAp();
  158. }
  159. /// <summary>
  160. /// Create Markup annotations
  161. /// </summary>
  162. /// <param name="document"></param>
  163. static private void CreateMarkupAnnotation(CPDFDocument document)
  164. {
  165. List<CRect> cRectList = new List<CRect>();
  166. CRect rect = new CRect(300, 240, 400, 300);
  167. cRectList.Add(rect);
  168. byte[] color = { 255, 0, 0 };
  169. //highlight
  170. CPDFPage page1 = document.PageAtIndex(0);
  171. CPDFHighlightAnnotation highlight = page1.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  172. highlight.SetColor(color);
  173. highlight.SetTransparency(120);
  174. highlight.SetQuardRects(cRectList);
  175. highlight.UpdateAp();
  176. //underline
  177. CPDFPage page2 = document.PageAtIndex(1);
  178. CPDFUnderlineAnnotation underline = page2.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE) as CPDFUnderlineAnnotation;
  179. underline.SetColor(color);
  180. underline.SetTransparency(120);
  181. underline.SetQuardRects(cRectList);
  182. underline.UpdateAp();
  183. //strikeout
  184. CPDFPage page3 = document.PageAtIndex(2);
  185. CPDFStrikeoutAnnotation strikeout = page3.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT) as CPDFStrikeoutAnnotation;
  186. strikeout.SetColor(color);
  187. strikeout.SetTransparency(120);
  188. strikeout.SetQuardRects(cRectList);
  189. strikeout.UpdateAp();
  190. //squiggly
  191. CPDFPage page4 = document.PageAtIndex(3);
  192. CPDFSquigglyAnnotation squiggy = page4.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY) as CPDFSquigglyAnnotation;
  193. squiggy.SetColor(color);
  194. squiggy.SetTransparency(120);
  195. squiggy.SetQuardRects(cRectList);
  196. squiggy.UpdateAp();
  197. }
  198. public static byte[] BitmapToByteArray(Bitmap bitmap)
  199. {
  200. BitmapData bmpdata = null;
  201. try
  202. {
  203. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  204. int numbytes = bmpdata.Stride * bitmap.Height;
  205. byte[] bytedata = new byte[numbytes];
  206. IntPtr ptr = bmpdata.Scan0;
  207. Marshal.Copy(ptr, bytedata, 0, numbytes);
  208. return bytedata;
  209. }
  210. finally
  211. {
  212. if (bmpdata != null)
  213. bitmap.UnlockBits(bmpdata);
  214. }
  215. }
  216. /// <summary>
  217. /// Create stamp annotation
  218. /// </summary>
  219. /// <param name="document"></param>
  220. static private void CreateStampAnnotation(CPDFDocument document)
  221. {
  222. CPDFPage page = document.PageAtIndex(0);
  223. CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  224. standard.SetStandardStamp("Approved");
  225. standard.SetRect(new CRect(100, 100, 250, 150));
  226. // standard.AnnotationRotator.SetRotation(45);
  227. standard.UpdateAp();
  228. // Text
  229. CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  230. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
  231. text.SetRect(new CRect(300, 220, 450, 300));
  232. text.UpdateAp();
  233. // Image
  234. Bitmap bitmap = new Bitmap("logo.png");
  235. CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  236. image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
  237. image.SetRect(new CRect(300, 320, 380, 400));
  238. image.SetTransparency(255);
  239. standard.AnnotationRotator.SetRotation(45);
  240. image.UpdateAp();
  241. }
  242. private static void CreateLinkAnnotation(CPDFDocument document)
  243. {
  244. CPDFPage page = document.PageAtIndex(0);
  245. CPDFDestination dest = new CPDFDestination();
  246. dest.PageIndex = 1;
  247. CPDFLinkAnnotation link = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
  248. link.SetRect(new CRect(0, 50, 50, 0));
  249. link.SetDestination(document, dest);
  250. }
  251. /// <summary>
  252. /// Create annotations
  253. /// </summary>
  254. /// <param name="document"></param>
  255. /// <returns></returns>
  256. static private bool CreateAnnots(CPDFDocument document)
  257. {
  258. //CreateFreetextAnnotation(document);
  259. //CreateFreehandAnnotation(document);
  260. CreateShapeAnnotation(document);
  261. //CreateNoteAnnotation(document);
  262. //CreateShapeAnnotation(document);
  263. //CreateSoundAnnotation(document);
  264. //CreateMarkupAnnotation(document);
  265. CreateStampAnnotation(document);
  266. //CreateLinkAnnotation(document);
  267. string path = Path.Combine(outputPath, "CreateAnnotsTest.pdf");
  268. if (!document.WriteToFilePath(path))
  269. {
  270. return false;
  271. }
  272. Console.WriteLine("Browse the changed file in " + path);
  273. return true;
  274. }
  275. /// <summary>
  276. /// Delete the first annotation
  277. /// </summary>
  278. /// <param name="document"></param>
  279. /// <returns></returns>
  280. static private bool DeleteAnnotations(CPDFDocument document)
  281. {
  282. CPDFPage page = document.PageAtIndex(0);
  283. List<CPDFAnnotation> annotList = page.GetAnnotations();
  284. if (!annotList[0].RemoveAnnot())
  285. {
  286. return false;
  287. }
  288. string path = Path.Combine(outputPath, "DeleteAnnotsTest.pdf");
  289. if (!document.WriteToFilePath(path))
  290. {
  291. return false;
  292. }
  293. Console.WriteLine("Browse the changed file in " + path);
  294. return true;
  295. }
  296. }
  297. }