NewActionJavaScript Method (GdPicturePDF)
In This Topic
Creates a new action of the type JavaScript. A JavaScript action (introduced in PDF 1.3) causes a script to be compiled and executed
by the JavaScript interpreter. JavaScript implements objects, methods, and properties that enable you to manipulate PDF files, modify the appearance of PDF
files, etc. You can tie JavaScript code to a specific PDF document, a page or a field, and much more.
You can read more about the content and effects of JavaScript scripts in the JavaScript for Acrobat API Reference.
Syntax
'Declaration
Public Function NewActionJavaScript( _
ByVal As String _
) As Integer
public int NewActionJavaScript(
string
)
public function NewActionJavaScript(
: String
): Integer;
public function NewActionJavaScript(
: String
) : int;
public: int NewActionJavaScript(
string*
)
public:
int NewActionJavaScript(
String^
)
Parameters
- JavaScript
- A text string containing a JavaScript script to be executed when the action is triggered.
Return Value
The unique action identifier of the newly created action of the type JavaScript. The
GetStat method can be subsequently used to determine if this method has been successful.
You can subsequently apply this identifier when creating actions using these methods: SetViewerOpenAction, SetBookmarkAction, SetAnnotationAction or SetFormFieldAction.
Example
How to create a form field of the type button with the associated JavaScript action.
Clicking this button in the created PDF document results in running the script. The alert window displays the string Hello! .
Dim gdpicturePDF As New GdPicturePDF()
Dim failedMethod As String = "NewPDF()"
If gdpicturePDF.NewPDF() = GdPictureStatus.OK Then
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
failedMethod = "NewPage() or SelectPage()"
If (gdpicturePDF.NewPage(21, 29.7F) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SelectPage(1) = GdPictureStatus.OK) Then
failedMethod = "AddStandardFont()"
Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontCourier)
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
failedMethod = "AddPushButtonFormField()"
Dim fieldID As Integer = gdpicturePDF.AddPushButtonFormField(1, 1, 5, 1, "Submit", "Hello!", fontResName, 6, 255, 0, 0)
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
failedMethod = "NewActionJavaScript()"
Dim javaScript As String = "app.alert(""Hello!"");"
Dim actionID As Integer = gdpicturePDF.NewActionJavaScript(javaScript)
If (gdpicturePDF.GetStat() = GdPictureStatus.OK) Then
failedMethod = "SetFormFieldAction() or SaveToFile()"
If (gdpicturePDF.SetFormFieldAction(fieldID, actionID) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SaveToFile("button_action_javascript.pdf") = GdPictureStatus.OK) Then
failedMethod = ""
MessageBox.Show("The PDF document containing the button associated with the JavaScript action has been saved successfully.", "Example: NewActionJavaScript")
End If
End If
End If
End If
End If
End If
If failedMethod <> "" Then
MessageBox.Show("The " + failedMethod + " method has failed. The presumed status: " + gdpicturePDF.GetStat().ToString(), "Example: NewActionJavaScript")
End If
gdpicturePDF.Dispose()
GdPicturePDF gdpicturePDF = new GdPicturePDF();
string failedMethod = "NewPDF()";
if (gdpicturePDF.NewPDF() == GdPictureStatus.OK)
{
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
failedMethod = "NewPage() or SelectPage()";
if ((gdpicturePDF.NewPage(21, 29.7f) == GdPictureStatus.OK) &&
(gdpicturePDF.SelectPage(1) == GdPictureStatus.OK))
{
failedMethod = "AddStandardFont()";
string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontCourier);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
failedMethod = "AddPushButtonFormField()";
int fieldID = gdpicturePDF.AddPushButtonFormField(1, 1, 5, 1, "Submit", "Hello!", fontResName, 6, 255, 0, 0);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
failedMethod = "NewActionJavaScript()";
string javaScript = "app.alert(\"Hello!\");";
int actionID = gdpicturePDF.NewActionJavaScript(javaScript);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
failedMethod = "SetFormFieldAction() or SaveToFile()";
if ((gdpicturePDF.SetFormFieldAction(fieldID, actionID) == GdPictureStatus.OK) &&
(gdpicturePDF.SaveToFile("button_action_javascript.pdf") == GdPictureStatus.OK))
{
failedMethod = "";
MessageBox.Show("The PDF document containing the button associated with the JavaScript action has been saved successfully.", "Example: NewActionJavaScript");
}
}
}
}
}
}
if (failedMethod != "")
MessageBox.Show("The " + failedMethod + " method has failed. The presumed status: " + gdpicturePDF.GetStat().ToString(), "Example: NewActionJavaScript");
gdpicturePDF.Dispose();
Example
How to create a form field of the type button with the associated JavaScript action.
Clicking this button in the created PDF document results in running the script. The alert window displays the string Hello! .
Dim gdpicturePDF As New GdPicturePDF()
Dim failedMethod As String = "NewPDF()"
If gdpicturePDF.NewPDF() = GdPictureStatus.OK Then
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
failedMethod = "NewPage() or SelectPage()"
If (gdpicturePDF.NewPage(21, 29.7F) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SelectPage(1) = GdPictureStatus.OK) Then
failedMethod = "AddStandardFont()"
Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontCourier)
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
failedMethod = "AddPushButtonFormField()"
Dim fieldID As Integer = gdpicturePDF.AddPushButtonFormField(1, 1, 5, 1, "Submit", "Hello!", fontResName, 6, 255, 0, 0)
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
failedMethod = "NewActionJavaScript()"
Dim javaScript As String = "app.alert(""Hello!"");"
Dim actionID As Integer = gdpicturePDF.NewActionJavaScript(javaScript)
If (gdpicturePDF.GetStat() = GdPictureStatus.OK) Then
failedMethod = "SetFormFieldAction() or SaveToFile()"
If (gdpicturePDF.SetFormFieldAction(fieldID, actionID) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SaveToFile("button_action_javascript.pdf") = GdPictureStatus.OK) Then
failedMethod = ""
MessageBox.Show("The PDF document containing the button associated with the JavaScript action has been saved successfully.", "Example: NewActionJavaScript")
End If
End If
End If
End If
End If
End If
If failedMethod <> "" Then
MessageBox.Show("The " + failedMethod + " method has failed. The presumed status: " + gdpicturePDF.GetStat().ToString(), "Example: NewActionJavaScript")
End If
gdpicturePDF.Dispose()
GdPicturePDF gdpicturePDF = new GdPicturePDF();
string failedMethod = "NewPDF()";
if (gdpicturePDF.NewPDF() == GdPictureStatus.OK)
{
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
failedMethod = "NewPage() or SelectPage()";
if ((gdpicturePDF.NewPage(21, 29.7f) == GdPictureStatus.OK) &&
(gdpicturePDF.SelectPage(1) == GdPictureStatus.OK))
{
failedMethod = "AddStandardFont()";
string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontCourier);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
failedMethod = "AddPushButtonFormField()";
int fieldID = gdpicturePDF.AddPushButtonFormField(1, 1, 5, 1, "Submit", "Hello!", fontResName, 6, 255, 0, 0);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
failedMethod = "NewActionJavaScript()";
string javaScript = "app.alert(\"Hello!\");";
int actionID = gdpicturePDF.NewActionJavaScript(javaScript);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
failedMethod = "SetFormFieldAction() or SaveToFile()";
if ((gdpicturePDF.SetFormFieldAction(fieldID, actionID) == GdPictureStatus.OK) &&
(gdpicturePDF.SaveToFile("button_action_javascript.pdf") == GdPictureStatus.OK))
{
failedMethod = "";
MessageBox.Show("The PDF document containing the button associated with the JavaScript action has been saved successfully.", "Example: NewActionJavaScript");
}
}
}
}
}
}
if (failedMethod != "")
MessageBox.Show("The " + failedMethod + " method has failed. The presumed status: " + gdpicturePDF.GetStat().ToString(), "Example: NewActionJavaScript");
gdpicturePDF.Dispose();
See Also
Reference
GdPicturePDF Class
GdPicturePDF Members
GetStat Method
GetStat Method
SetViewerOpenAction Method
SetBookmarkAction Method
SetAnnotationAction Method
SetFormFieldAction Method
GetActionJavaScript Method
NewActionGoTo(PdfDestinationType,Int32,Single,Single,Single,Single,Single) Method
NewActionGoToR(PdfDestinationType,String,Boolean,Int32,Single,Single,Single,Single,Single) Method
NewActionLaunch Method
NewActionNamed Method
NewActionURI Method
GetActionType Method