AnnotationTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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>7
  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. CPDFBorderEffector borderEffect = new CPDFBorderEffector( C_BORDER_TYPE.C_BORDER_TYPE_Cloud, C_BORDER_INTENSITY.C_INTENSITY_TWO);
  105. // Square
  106. CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
  107. square.SetSourceRect(new CRect(10, 250, 200, 200));
  108. square.SetLineColor(lineColor);
  109. square.SetBgColor(bgColor);
  110. square.SetTransparency(120);
  111. square.SetLineWidth(1);
  112. square.SetBorderWidth(1);
  113. square.SetAnnotBorderEffector(borderEffect);
  114. square.AnnotationRotator.SetRotation(45);
  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.SetAnnotBorderEffect(borderEffect);
  125. square.SetBorderWidth(1);
  126. circle.UpdateAp();
  127. // Line
  128. CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  129. line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
  130. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
  131. line.SetLineColor(lineColor);
  132. line.SetTransparency(120);
  133. line.SetLineWidth(1);
  134. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray);
  135. line.UpdateAp();
  136. }
  137. /// <summary>
  138. /// Create note annotations
  139. /// </summary>
  140. /// <param name="document"></param>
  141. static private void CreateNoteAnnotation(CPDFDocument document)
  142. {
  143. CPDFPage page = document.PageAtIndex(0);
  144. CPDFTextAnnotation textAnnotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  145. textAnnotation.SetColor(new byte[] { 255, 0, 0 });
  146. textAnnotation.SetTransparency(255);
  147. textAnnotation.SetContent("ComPDFKit");
  148. textAnnotation.SetRect(new CRect(300, 600, 350, 650));
  149. textAnnotation.UpdateAp();
  150. }
  151. /// <summary>
  152. /// Create sound annotations
  153. /// </summary>
  154. /// <param name="document"></param>
  155. static private void CreateSoundAnnotation(CPDFDocument document)
  156. {
  157. CPDFPage page = document.PageAtIndex(0);
  158. CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
  159. sound.SetRect(new CRect(400, 700, 450, 750));
  160. sound.SetSoundPath("", "Bird.wav");
  161. sound.UpdateAp();
  162. }
  163. /// <summary>
  164. /// Create Markup annotations
  165. /// </summary>
  166. /// <param name="document"></param>
  167. static private void CreateMarkupAnnotation(CPDFDocument document)
  168. {
  169. List<CRect> cRectList = new List<CRect>();
  170. CRect rect = new CRect(300, 240, 400, 300);
  171. cRectList.Add(rect);
  172. byte[] color = { 255, 0, 0 };
  173. //highlight
  174. CPDFPage page1 = document.PageAtIndex(0);
  175. CPDFHighlightAnnotation highlight = page1.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  176. highlight.SetColor(color);
  177. highlight.SetTransparency(120);
  178. highlight.SetQuardRects(cRectList);
  179. highlight.UpdateAp();
  180. //underline
  181. CPDFPage page2 = document.PageAtIndex(1);
  182. CPDFUnderlineAnnotation underline = page2.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE) as CPDFUnderlineAnnotation;
  183. underline.SetColor(color);
  184. underline.SetTransparency(120);
  185. underline.SetQuardRects(cRectList);
  186. underline.UpdateAp();
  187. //strikeout
  188. CPDFPage page3 = document.PageAtIndex(2);
  189. CPDFStrikeoutAnnotation strikeout = page3.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT) as CPDFStrikeoutAnnotation;
  190. strikeout.SetColor(color);
  191. strikeout.SetTransparency(120);
  192. strikeout.SetQuardRects(cRectList);
  193. strikeout.UpdateAp();
  194. //squiggly
  195. CPDFPage page4 = document.PageAtIndex(3);
  196. CPDFSquigglyAnnotation squiggy = page4.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY) as CPDFSquigglyAnnotation;
  197. squiggy.SetColor(color);
  198. squiggy.SetTransparency(120);
  199. squiggy.SetQuardRects(cRectList);
  200. squiggy.UpdateAp();
  201. }
  202. public static byte[] BitmapToByteArray(Bitmap bitmap)
  203. {
  204. BitmapData bmpdata = null;
  205. try
  206. {
  207. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  208. int numbytes = bmpdata.Stride * bitmap.Height;
  209. byte[] bytedata = new byte[numbytes];
  210. IntPtr ptr = bmpdata.Scan0;
  211. Marshal.Copy(ptr, bytedata, 0, numbytes);
  212. return bytedata;
  213. }
  214. finally
  215. {
  216. if (bmpdata != null)
  217. bitmap.UnlockBits(bmpdata);
  218. }
  219. }
  220. /// <summary>
  221. /// Create stamp annotation
  222. /// </summary>
  223. /// <param name="document"></param>
  224. static private void CreateStampAnnotation(CPDFDocument document)
  225. {
  226. CPDFPage page = document.PageAtIndex(0);
  227. CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  228. standard.SetStandardStamp("Approved");
  229. standard.SetSourceRect(new CRect(100, 100, 250, 150));
  230. standard.AnnotationRotator.SetRotation(45);
  231. standard.UpdateAp();
  232. // Text
  233. CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  234. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
  235. text.SetRect(new CRect(300, 220, 450, 300));
  236. text.UpdateAp();
  237. // Image
  238. Bitmap bitmap = new Bitmap("logo.png");
  239. CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  240. image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
  241. image.SetRect(new CRect(300, 320, 380, 400));
  242. image.SetTransparency(255);
  243. standard.AnnotationRotator.SetRotation(45);
  244. image.UpdateAp();
  245. }
  246. private static void CreateLinkAnnotation(CPDFDocument document)
  247. {
  248. CPDFPage page = document.PageAtIndex(0);
  249. CPDFDestination dest = new CPDFDestination();
  250. dest.PageIndex = 1;
  251. CPDFLinkAnnotation link = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
  252. link.SetRect(new CRect(0, 50, 50, 0));
  253. link.SetDestination(document, dest);
  254. }
  255. /// <summary>
  256. /// Create annotations
  257. /// </summary>
  258. /// <param name="document"></param>
  259. /// <returns></returns>
  260. static private bool CreateAnnots(CPDFDocument document)
  261. {
  262. //CreateFreetextAnnotation(document);
  263. //CreateFreehandAnnotation(document);
  264. CreateShapeAnnotation(document);
  265. //CreateNoteAnnotation(document);
  266. //CreateShapeAnnotation(document);
  267. //CreateSoundAnnotation(document);
  268. //CreateMarkupAnnotation(document);
  269. CreateStampAnnotation(document);
  270. //CreateLinkAnnotation(document);
  271. string path = Path.Combine(outputPath, "CreateAnnotsTest.pdf");
  272. if (!document.WriteToFilePath(path))
  273. {
  274. return false;
  275. }
  276. Console.WriteLine("Browse the changed file in " + path);
  277. return true;
  278. }
  279. /// <summary>
  280. /// Delete the first annotation
  281. /// </summary>
  282. /// <param name="document"></param>
  283. /// <returns></returns>
  284. static private bool DeleteAnnotations(CPDFDocument document)
  285. {
  286. CPDFPage page = document.PageAtIndex(0);
  287. List<CPDFAnnotation> annotList = page.GetAnnotations();
  288. if (!annotList[0].RemoveAnnot())
  289. {
  290. return false;
  291. }
  292. string path = Path.Combine(outputPath, "DeleteAnnotsTest.pdf");
  293. if (!document.WriteToFilePath(path))
  294. {
  295. return false;
  296. }
  297. Console.WriteLine("Browse the changed file in " + path);
  298. return true;
  299. }
  300. }
  301. }