JavaScriptAction.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Actions

'' 演示如何将 PDF 操作与 JavaScript 脚本关联。
'' 在此示例中,脚本与页面上的链接相关联。
'' 请注意,JavaScript 可能无法在某些 PDF 查看器(例如内置浏览器查看器)中运行。
'' 请参考 将动作和脚本应用于 PDF
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}
        '' 在矩形中绘制链接字符串:
        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)
        '' 添加有关这可能无法在浏览器中运行的警告:
        Util.AddNote("请注意,JavaScript 可能无法在某些 PDF 查看器(例如内置浏览器查看器)中运行。",
                doc.Pages.Last, New RectangleF(rect.X, rect.Bottom + 36, 400, 400))
        ''
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class