JavaScriptAction.vb
  1. '' 完毕:
  2. Imports System.IO
  3. Imports System.Drawing
  4. Imports GrapeCity.Documents.Text
  5. Imports GrapeCity.Documents.Pdf
  6. Imports GrapeCity.Documents.Pdf.Annotations
  7. Imports GrapeCity.Documents.Pdf.Actions
  8.  
  9. '' 演示如何将 PDF 操作与 JavaScript 脚本关联。
  10. '' 在此示例中,脚本与页面上的链接相关联。
  11. '' 请注意,JavaScript 可能无法在某些 PDF 查看器(例如内置浏览器查看器)中运行。
  12. '' 请参考 将动作和脚本应用于 PDF
  13. Public Class JavaScriptAction
  14.  
  15. Const js =
  16. "var cChoice = app.popUpMenu(""Introduction"", "" - "", ""Chapter 1""," + vbCrLf +
  17. "[ ""Chapter 2"", ""Chapter 2 Start"", ""Chapter 2 Middle""," + vbCrLf +
  18. "[""Chapter 2 End"", ""The End""]]);" + vbCrLf +
  19. "app.alert(""You chose the '"" + cChoice + ""' menu item"");"
  20.  
  21. Function CreatePDF(ByVal stream As Stream) As Integer
  22. Dim doc = New GcPdfDocument()
  23. Dim g = doc.NewPage().Graphics
  24. Dim jsAction = New ActionJavaScript(js)
  25. Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
  26. '' 在矩形中绘制链接字符串:
  27. Dim text = "Click this to show the popup menu."
  28. Dim rect = New RectangleF(New PointF(72, 72), g.MeasureString(text, tf))
  29. g.FillRectangle(rect, Color.LightGoldenrodYellow)
  30. g.DrawString(text, tf, rect)
  31. Dim result = New LinkAnnotation(rect, jsAction)
  32. doc.Pages.Last.Annotations.Add(result)
  33. '' 添加有关这可能无法在浏览器中运行的警告:
  34. Util.AddNote("请注意,JavaScript 可能无法在某些 PDF 查看器(例如内置浏览器查看器)中运行。",
  35. doc.Pages.Last, New RectangleF(rect.X, rect.Bottom + 36, 400, 400))
  36. ''
  37. '' 完毕:
  38. doc.Save(stream)
  39. Return doc.Pages.Count
  40. End Function
  41. End Class
  42.