I am trying to add an annotation to a pdf document, and everything appears to work properly (no errors) until I go to save the document at which time I get a "Object reference not set to an instance of an object". If I comment out the page.AnnotationsWidget.Add command, the document saves without an error. I'm using version 2.5.6.5040 of the Spire.PDF package. Please tell me what I am doing wrong. Here is my code:
- Code: Select all
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim doc As New PdfDocument("C:\temp\test.pdf")
Dim doc2 As New PdfDocument
Dim oAnn As PdfFreeTextAnnotation
doc2.InsertPage(doc, 0)
Dim page As PdfPageBase = doc2.Pages(0)
'Draw the text
page.Canvas.DrawString("Hello, World!", New PdfFont(PdfFontFamily.Helvetica, 30.0F), New PdfSolidBrush(Color.Black), 10, 10)
oAnn = New PdfFreeTextAnnotation(New Rectangle(0, 0, 50, 35))
With oAnn
.Border = New PdfAnnotationBorder(1)
.Color = Color.AliceBlue
.MarkupText = "Test"
.Flags = PdfAnnotationFlags.Default
End With
page.AnnotationsWidget.Add(oAnn)
doc2.SaveToFile("C:\Temp\Test2.pdf")
End Sub