Setting Form Field Values in Code

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
jeffp
User
Posts: 914
Joined: Wed Sep 30, 2009 6:53 pm

Setting Form Field Values in Code

Post by jeffp »

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?
Corwin - Tracker Sup
User
Posts: 664
Joined: Tue Nov 14, 2006 12:23 pm

Re: Setting Form Field Values in Code

Post by Corwin - Tracker Sup »

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";
jeffp
User
Posts: 914
Joined: Wed Sep 30, 2009 6:53 pm

Re: Setting Form Field Values in Code

Post by jeffp »

Pefect.

Is there a js call to get an array of all the Fields on the page?
Corwin - Tracker Sup
User
Posts: 664
Joined: Tue Nov 14, 2006 12:23 pm

Re: Setting Form Field Values in Code

Post by Corwin - Tracker Sup »

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";
}
jeffp
User
Posts: 914
Joined: Wed Sep 30, 2009 6:53 pm

Re: Setting Form Field Values in Code

Post by jeffp »

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.
Corwin - Tracker Sup
User
Posts: 664
Joined: Tue Nov 14, 2006 12:23 pm

Re: Setting Form Field Values in Code

Post by Corwin - Tracker Sup »

Can you give us that JS code that returns AV?
jeffp
User
Posts: 914
Joined: Wed Sep 30, 2009 6:53 pm

Re: Setting Form Field Values in Code

Post by jeffp »

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.

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;
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 +
    ' return str;' + #10 +
    '}';
  FPDF.RunJavaScript(js, AStr, 0, 0);
  Result := Trim(AStr);
end;
Corwin - Tracker Sup
User
Posts: 664
Joined: Tue Nov 14, 2006 12:23 pm

Re: Setting Form Field Values in Code

Post by Corwin - Tracker Sup »

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;
Post Reply