AnnotationTypes.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

'' Shows how to add annotations to a PDF document.
Public Class AnnotationTypes
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        '' User names for annotations' authors:
        Dim user1 = "Jaime Smith"
        Dim user2 = "Jane Donahue"

        Dim tf = New TextFormat() With {.Font = StandardFonts.Helvetica, .FontSize = 10}
        Dim noteWidth = 72 * 4
        Dim gap = 8

        Dim rc = Util.AddNote(
            "This sample demonstrates some types of annotations that can be created with DsPdf." + vbLf +
            "Note that some annotation types may not display in certain viewers (such as built-in browser viewers)." +
            "To see all annotations on this page, open it in Acrobat Reader or other full-featured PDF viewer.",
            page)

        '' Text annotation:
        Dim ip = New PointF(rc.X, rc.Bottom + gap)
        rc = Util.AddNote(
            "A red text annotation is placed to the right of this note.",
            page, New RectangleF(ip.X, ip.Y, noteWidth, 100))
        Dim textAnnot = New TextAnnotation() With {
            .UserName = user1,
            .Contents = "This is annotation 1, a red one.",
            .Rect = New RectangleF(rc.Right, rc.Top, 72 * 2, 72),
            .Color = Color.Red
        }
        page.Annotations.Add(textAnnot)
        '' A reply to previous annotation:
        Dim textAnnotReply = New TextAnnotation() With {
            .UserName = user2,
            .Contents = "This is a reply to the first annotation.",
            .Rect = New RectangleF(rc.Right, rc.Top, 72 * 2, 72),
            .ReferenceAnnotation = textAnnot,
            .ReferenceType = AnnotationReferenceType.Reply
        }
        page.Annotations.Add(textAnnotReply)

        '' An initially open text annotation:
        ip = New PointF(rc.X, rc.Bottom + gap)
        rc = Util.AddNote(
            "A green text annotation that is initially open is placed to the right of this note.",
            page, New RectangleF(ip.X, ip.Y, noteWidth, 100))
        Dim textAnnotOpen = New TextAnnotation() With {
            .Open = True,
            .UserName = user1,
            .Contents = "This is an initially open annotation (green).",
            .Rect = New RectangleF(rc.Right, rc.Top, 72 * 2, 72),
            .Color = Color.Green
        }
        page.Annotations.Add(textAnnotOpen)

        '' A free text annotation (shows directly on page):
        ip = New PointF(rc.X, rc.Bottom + gap)
        rc = Util.AddNote(
            "A blue free text annotation is placed below and to the right, with a callout going from it to this note.",
            page, New RectangleF(ip.X, ip.Y, noteWidth, 100))
        Dim freeAnnot = New FreeTextAnnotation() With {
            .Rect = New RectangleF(rc.Right + 18, rc.Bottom + 9, 72 * 2, 72),
            .CalloutLine = {
                New PointF(rc.Left + rc.Width / 2, rc.Bottom),
                New PointF(rc.Left + rc.Width / 2, rc.Bottom + 9 + 36),
                New PointF(rc.Right + 18, rc.Bottom + 9 + 36)
            },
            .LineWidth = 1,
            .LineEndStyle = LineEndingStyle.OpenArrow,
            .LineDashPattern = New Single() {8, 4},
            .Contents = "This is a free text annotation with a callout line going to the note on the left.",
            .Color = Color.LightSkyBlue
        }
        page.Annotations.Add(freeAnnot)

        '' Another free text annotation, with some rich text inside:
        ip = New PointF(rc.X, freeAnnot.Rect.Bottom + gap)
        Dim freeRichAnnot = New FreeTextAnnotation() With {
            .Rect = New RectangleF(ip.X - 144, ip.Y, 72 * 5, 72),
            .LineWidth = 1,
            .Color = Color.LightSalmon,
            .RichText =
                "<body><p>This is another <i>free text annotation</i>, with <b><i>Rich Text</i></b> content.</p>" +
                "<p>Even though a <b>free text</b> annotation displays text directly on a page, " +
                "as other annotations it can be placed outside the page's bounds.</p></body>"
        }
        page.Annotations.Add(freeRichAnnot)

        '' A square annotatou around a note:
        ip = New PointF(rc.X, freeRichAnnot.Rect.Bottom + gap * 2)
        rc = Util.AddNote(
            "A square annotation drawn with a 3pt wide orange line around this note has a rich text associated with it.",
            page, New RectangleF(ip.X, ip.Y, noteWidth, 100))

        rc.Inflate(8, 8)
        Dim squareAnnot = New SquareAnnotation() With {
            .UserName = user2,
            .Rect = rc,
            .LineWidth = 3,
            .Color = Color.Orange,
            .RichText =
                "<body><p>This <b><i>rich text</i></b> is associated with the square annotation around a text note.</p></body>"
        }
        page.Annotations.Add(squareAnnot)
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class