AnnotationTest.vb 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. Imports System.IO
  2. Imports ComPDFKit.PDFDocument
  3. Imports ComPDFKit.PDFPage
  4. Imports ComPDFKit.PDFAnnotation
  5. Imports ComPDFKit.Import
  6. Imports System.Runtime.InteropServices
  7. Imports System.Drawing
  8. Imports System.Drawing.Imaging
  9. Module AnnotationTest
  10. Private parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())))
  11. Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
  12. Sub Main()
  13. ' Preparation work
  14. SDKLicenseHelper.LicenseVerify()
  15. If Not Directory.Exists(outputPath) Then
  16. Directory.CreateDirectory(outputPath)
  17. End If
  18. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  19. ' Sample 1: Create annotations
  20. If CreateAnnots(document) Then
  21. Console.WriteLine("Create annots done.")
  22. End If
  23. Console.WriteLine("--------------------")
  24. ' Sample 2: Delete annotations
  25. Dim annotsDocument As CPDFDocument = CPDFDocument.InitWithFilePath("Annotations.pdf")
  26. If DeleteAnnotations(annotsDocument) Then
  27. Console.WriteLine("Delete annots done.")
  28. End If
  29. Console.WriteLine("--------------------")
  30. Console.WriteLine("Done")
  31. Console.WriteLine("--------------------")
  32. Console.ReadLine()
  33. End Sub
  34. Private Sub CreateFreetextAnnotation(document As CPDFDocument)
  35. Dim page As CPDFPage = document.PageAtIndex(0)
  36. Dim str As String = "ComPDFKit Samples"
  37. Dim freeText As CPDFFreeTextAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT), CPDFFreeTextAnnotation)
  38. freeText.SetContent(str)
  39. freeText.SetRect(New CRect(0, 100, 160, 0))
  40. Dim textAttribute As New CTextAttribute()
  41. textAttribute.FontName = "Helvetica"
  42. textAttribute.FontSize = 12
  43. Dim fontColor As Byte() = {255, 0, 0}
  44. textAttribute.FontColor = fontColor
  45. freeText.SetFreetextDa(textAttribute)
  46. freeText.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER)
  47. freeText.UpdateAp()
  48. End Sub
  49. Private Sub CreateFreehandAnnotation(document As CPDFDocument)
  50. Dim page As CPDFPage = document.PageAtIndex(0)
  51. Dim ink As CPDFInkAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK), CPDFInkAnnotation)
  52. ink.SetInkColor(New Byte() {255, 0, 0})
  53. ink.SetBorderWidth(2)
  54. ink.SetTransparency(128)
  55. Dim points As New List(Of List(Of CPoint))()
  56. ink.SetInkPath(points)
  57. ink.SetThickness(8)
  58. points.Clear()
  59. points.Add(New List(Of CPoint)() From
  60. {
  61. New CPoint(10, 100),
  62. New CPoint(100, 10)
  63. })
  64. ink.SetInkPath(points)
  65. ink.UpdateAp()
  66. End Sub
  67. Private Sub CreateShapeAnnotation(document As CPDFDocument)
  68. Dim page As CPDFPage = document.PageAtIndex(0)
  69. Dim dashArray As Single() = {2.0F, 1.0F}
  70. Dim lineColor As Byte() = {255, 0, 0}
  71. Dim bgColor As Byte() = {0, 255, 0}
  72. ' Square
  73. Dim square As CPDFSquareAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE), CPDFSquareAnnotation)
  74. square.SetRect(New CRect(10, 250, 200, 200))
  75. square.SetLineColor(lineColor)
  76. square.SetBgColor(bgColor)
  77. square.SetTransparency(120)
  78. square.SetLineWidth(1)
  79. square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray)
  80. square.UpdateAp()
  81. ' Circle
  82. Dim circle As CPDFCircleAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE), CPDFCircleAnnotation)
  83. circle.SetRect(New CRect(10, 410, 110, 300))
  84. circle.SetLineColor(lineColor)
  85. circle.SetBgColor(bgColor)
  86. circle.SetTransparency(120)
  87. circle.SetLineWidth(1)
  88. circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray)
  89. circle.UpdateAp()
  90. ' Line
  91. Dim line As CPDFLineAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE), CPDFLineAnnotation)
  92. line.SetLinePoints(New CPoint(300, 300), New CPoint(350, 350))
  93. line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW)
  94. line.SetLineColor(lineColor)
  95. line.SetTransparency(120)
  96. line.SetLineWidth(1)
  97. line.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, dashArray)
  98. line.UpdateAp()
  99. End Sub
  100. Private Sub CreateNoteAnnotation(document As CPDFDocument)
  101. Dim page As CPDFPage = document.PageAtIndex(0)
  102. Dim textAnnotation As CPDFTextAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT), CPDFTextAnnotation)
  103. textAnnotation.SetColor(New Byte() {255, 0, 0})
  104. textAnnotation.SetTransparency(255)
  105. textAnnotation.SetContent("ComPDFKit")
  106. textAnnotation.SetRect(New CRect(300, 650, 350, 600))
  107. textAnnotation.UpdateAp()
  108. End Sub
  109. Private Sub CreateSoundAnnotation(document As CPDFDocument)
  110. Dim page As CPDFPage = document.PageAtIndex(0)
  111. Dim sound As CPDFSoundAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND), CPDFSoundAnnotation)
  112. sound.SetRect(New CRect(400, 750, 450, 700))
  113. Dim bitmap As New Bitmap("SoundAnnot.png")
  114. sound.SetSoundPath(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height, "Bird.wav")
  115. sound.UpdateAp()
  116. End Sub
  117. Private Sub CreateMarkupAnnotation(document As CPDFDocument)
  118. Dim cRectList As New List(Of CRect)()
  119. Dim rect As New CRect(300, 300, 400, 240)
  120. cRectList.Add(rect)
  121. Dim color As Byte() = {255, 0, 0}
  122. ' Highlight
  123. Dim page1 As CPDFPage = document.PageAtIndex(0)
  124. Dim highlight As CPDFHighlightAnnotation = TryCast(page1.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT), CPDFHighlightAnnotation)
  125. highlight.SetColor(color)
  126. highlight.SetTransparency(120)
  127. highlight.SetQuardRects(cRectList)
  128. highlight.UpdateAp()
  129. ' Underline
  130. Dim page2 As CPDFPage = document.PageAtIndex(1)
  131. Dim underline As CPDFUnderlineAnnotation = TryCast(page2.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE), CPDFUnderlineAnnotation)
  132. underline.SetColor(color)
  133. underline.SetTransparency(120)
  134. underline.SetQuardRects(cRectList)
  135. underline.UpdateAp()
  136. ' Strikeout
  137. Dim page3 As CPDFPage = document.PageAtIndex(2)
  138. Dim strikeout As CPDFStrikeoutAnnotation = TryCast(page3.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT), CPDFStrikeoutAnnotation)
  139. strikeout.SetColor(color)
  140. strikeout.SetTransparency(120)
  141. strikeout.SetQuardRects(cRectList)
  142. strikeout.UpdateAp()
  143. ' Squiggly
  144. Dim page4 As CPDFPage = document.PageAtIndex(3)
  145. Dim squiggy As CPDFSquigglyAnnotation = TryCast(page4.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY), CPDFSquigglyAnnotation)
  146. squiggy.SetColor(color)
  147. squiggy.SetTransparency(120)
  148. squiggy.SetQuardRects(cRectList)
  149. squiggy.UpdateAp()
  150. End Sub
  151. Public Function BitmapToByteArray(bitmap As Bitmap) As Byte()
  152. Dim bmpdata As BitmapData = Nothing
  153. Try
  154. bmpdata = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat)
  155. Dim numbytes As Integer = bmpdata.Stride * bitmap.Height
  156. Dim bytedata As Byte() = New Byte(numbytes - 1) {}
  157. Dim ptr As IntPtr = bmpdata.Scan0
  158. Marshal.Copy(ptr, bytedata, 0, numbytes)
  159. Return bytedata
  160. Finally
  161. If bmpdata IsNot Nothing Then
  162. bitmap.UnlockBits(bmpdata)
  163. End If
  164. End Try
  165. End Function
  166. Private Sub CreateStampAnnotation(document As CPDFDocument)
  167. Dim page As CPDFPage = document.PageAtIndex(0)
  168. ' Standard
  169. Dim standard As CPDFStampAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP), CPDFStampAnnotation)
  170. standard.SetStandardStamp("Approved")
  171. standard.SetRect(New CRect(300, 160, 450, 100))
  172. standard.UpdateAp()
  173. ' Text
  174. Dim text As CPDFStampAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP), CPDFStampAnnotation)
  175. text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED)
  176. text.SetRect(New CRect(300, 300, 450, 220))
  177. text.UpdateAp()
  178. ' Image
  179. Dim bitmap As New Bitmap("logo.png")
  180. Dim image As CPDFStampAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP), CPDFStampAnnotation)
  181. image.SetImageStamp(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height)
  182. image.SetRect(New CRect(300, 400, 380, 320))
  183. image.SetTransparency(255)
  184. image.UpdateAp()
  185. End Sub
  186. Private Function CreateAnnots(document As CPDFDocument) As Boolean
  187. CreateFreetextAnnotation(document)
  188. CreateFreehandAnnotation(document)
  189. CreateShapeAnnotation(document)
  190. CreateNoteAnnotation(document)
  191. CreateShapeAnnotation(document)
  192. CreateSoundAnnotation(document)
  193. CreateMarkupAnnotation(document)
  194. CreateStampAnnotation(document)
  195. Dim path As String = outputPath & "\CreateAnnotsTest.pdf"
  196. If Not document.WriteToFilePath(path) Then
  197. Return False
  198. End If
  199. Console.WriteLine("Browse the changed file in " & path)
  200. Return True
  201. End Function
  202. Private Function DeleteAnnotations(document As CPDFDocument) As Boolean
  203. Dim page As CPDFPage = document.PageAtIndex(0)
  204. Dim annotList As List(Of CPDFAnnotation) = page.GetAnnotations()
  205. Dim annotNum = annotList.Count
  206. If Not annotList(0).RemoveAnnot() Then
  207. Return False
  208. End If
  209. Dim path As String = outputPath & "\DeleteAnnotsTest.pdf"
  210. If Not document.WriteToFilePath(path) Then
  211. Return False
  212. End If
  213. Console.WriteLine("Browse the changed file in " & path)
  214. Return True
  215. End Function
  216. End Module