JavaScriptAction.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Actions

'' Shows how to associate a PDF action with a JavaScript script.
'' In this example the script is associated with a link on a page.
'' Note that JavaScript may not work in some PDF viewers (e.g. built-in browser viewers).
'' See JavaScript for Acrobat for details.
Public Class JavaScriptAction

    Const js =
            "var cChoice = app.popUpMenu(""Introduction"", "" - "", ""Chapter 1""," + vbCrLf +
            "[ ""Chapter 2"", ""Chapter 2 Start"", ""Chapter 2 Middle""," + vbCrLf +
            "[""Chapter 2 End"", ""The End""]]);" + vbCrLf +
            "app.alert(""You chose the '"" + cChoice + ""' menu item"");"

    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim g = doc.NewPage().Graphics
        Dim jsAction = New ActionJavaScript(js)
        Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
        '' Draw the link string in a rectangle:
        Dim text = "Click this to show the popup menu."
        Dim rect = New RectangleF(New PointF(72, 72), g.MeasureString(text, tf))
        g.FillRectangle(rect, Color.LightGoldenrodYellow)
        g.DrawString(text, tf, rect)
        Dim result = New LinkAnnotation(rect, jsAction)
        doc.Pages.Last.Annotations.Add(result)
        '' Add warning about this possibly not working in a browser:
        Util.AddNote(
                "Note that JavaScript may not work in some PDF viewers such as built-in browser viewers.",
                doc.Pages.Last, New RectangleF(rect.X, rect.Bottom + 36, 400, 400))
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class