Setting Form Field Values in Code
Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan
Setting Form Field Values in Code
I'm looking for a way to set a Form Field Value through code. I have several PDF files that contain form fields. Rather than having the user open the PDF and interact with the form fields directly, I need to be able to set these Form Field values via code. I have a list of the form field names. Is there a way using your SDK to find the field and set its value?
-
- User
- Posts: 664
- Joined: Tue Nov 14, 2006 12:23 pm
Re: Setting Form Field Values in Code
For now the only way to set Form Field value programmatically is to use PDF JS in Viewer ActiveX component:
Code: Select all
var f = this.getField("HTMLText11");
f.value = "hello";
Re: Setting Form Field Values in Code
Pefect.
Is there a js call to get an array of all the Fields on the page?
Is there a js call to get an array of all the Fields on the page?
-
- User
- Posts: 664
- Joined: Tue Nov 14, 2006 12:23 pm
Re: Setting Form Field Values in Code
Just a little base JS snippets:
Code: Select all
for (var i = 0; i < this.numFields; i++)
console.println("Field[" + i + "] = " + this.getNthFieldName(i));
Code: Select all
for ( var i = 0; i < this.numFields; i++)
{
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if ( f.type == "text")
f.value = "hello";
}
Re: Setting Form Field Values in Code
Perfect. Thanks for the examples.
I also notice that the Result parameter in RunJavaScript returns the last value set in the script to any any variable. I thought I would have had to use the return statement. However, I found that using the JS return statement won't work. It gives an AV.
I also notice that the Result parameter in RunJavaScript returns the last value set in the script to any any variable. I thought I would have had to use the return statement. However, I found that using the JS return statement won't work. It gives an AV.
-
- User
- Posts: 664
- Joined: Tue Nov 14, 2006 12:23 pm
Re: Setting Form Field Values in Code
Can you give us that JS code that returns AV?
Re: Setting Form Field Values in Code
The following WORKS and returns the value set in the "str" variable in the JS, which is odd I feel because I haven't directed it to return anything.
The following FAILS and gives and OLE AV. I would have though I would need the "return str;" below for it to return a value.
Code: Select all
function TPDFViewerAX.GetTextFields: String;
var
js, AStr: WideString;
begin
js :=
'var str = "";' + #10 +
'for (var i = 0; i < this.numFields; i++)' + #10 +
'{' + #10 +
' var fname = this.getNthFieldName(i);' + #10 +
' var f = this.getField(fname);' + #10 +
' if (f.type == "text")' + #10 +
' str = str + f.name + "\n";' + #10 +
'}';
FPDF.RunJavaScript(js, AStr, 0, 0);
Result := Trim(AStr);
end;
Code: Select all
function TPDFViewerAX.GetTextFields: String;
var
js, AStr: WideString;
begin
js :=
'var str = "";' + #10 +
'for (var i = 0; i < this.numFields; i++)' + #10 +
'{' + #10 +
' var fname = this.getNthFieldName(i);' + #10 +
' var f = this.getField(fname);' + #10 +
' if (f.type == "text")' + #10 +
' str = str + f.name + "\n";' + #10 +
' return str;' + #10 +
'}';
FPDF.RunJavaScript(js, AStr, 0, 0);
Result := Trim(AStr);
end;
-
- User
- Posts: 664
- Joined: Tue Nov 14, 2006 12:23 pm
Re: Setting Form Field Values in Code
RunJavaScript function will return result of last succeeded operation, so there is no need to in "return" statement. And by the way – if you using some Viever AX functions which may return fail result, you should use “try ... except” statement.
Code: Select all
try
CoPDFXCview1.RunJavaScript(js, AStr, 0, 0);
except
on ex : Exception do
ShowErrorMessage(EOleException(ex).ErrorCode); // Your error handler
end;