Your second PDF form with Scribus (and Javascript): Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 66: Line 66:
==Create list boxes and combo boxes==
==Create list boxes and combo boxes==
[[Image:Changing field properties1.png]]
[[Image:Changing field properties1.png]]
[[Image:List box contents.png]]


==Add a new JavaScript==
==Add a new JavaScript==

Revision as of 22:11, 4 February 2008

  1. Your first PDF form with Scribus
  2. How to create an e-Mail PDF Survey Form
  3. Your second PDF form with Scribus
  4. Enhance PDF forms with JavaScript

Work in progress!

Introduction

JavaScript functions in a PDF document

Teaching material will be made available for download asap.

Adding new functions

Go to Edit/Javascripts... and click on Add. Choose a name for your new function and click on OK to launch the built-in JavaScript editor.

Javascript on launch1.png

Let us call our first function RunOnLaunch and start with a simple alert that will be displayed on launching the PDF formular in Adobe Reader:

function RunOnLaunch() {
  app.alert('Welcome to the Scribus Demo Form!');
}

Launching JavaScript functions

Go to File/Export/Save as PDF and click on the Viewer tab. Choose the JavaScript to be executed from the Special Actions drop down list. Save the PDF document and open in with the Adobe Reader to see how it works.

Javascript on launch2.png

Adding JavaScript to PDF form elements

Calculating fields1.png

Validate form fields

 if(!this.getField('field1').value) {
	app.alert('Please fill a number into field 1');
 } else if (!this.getField('field2').value) { 
	app.alert('Please fill a number into field 2');
 }

Calculate button action.png

Calculate form field values


 if(!this.getField('field1').value) {
	app.alert('Please fill a number into field 1');
 } else if (!this.getField('field2').value) { 
	app.alert('Please fill a number into field 2');
 } else {

	app.alert('Adding field1 to field2 and putting the result into field3');
	this.getField('field3').value = this.getField('field1').value + this.getField('field2').value;

	app.alert('and now add 20% VAT to the result, if checkbox1 is checked ');
	var v = this.getField('checkbox1');
	if(v.value=='Yes')
		var vat = this.getField('field3').value * 0.2;
	else
		var vat = 0;

	this.getField('field3').value = this.getField('field3').value + vat;
 }

Changing field properties with a document wide script executed on button click

Create list boxes and combo boxes

Changing field properties1.png


List box contents.png

Add a new JavaScript


 function SetFieldValues()
 {
  app.alert('Running a document wide script SetFieldValues. To see how it works, open the  source document in Scribus and go to  Edit/JavaScripts.');

  app.alert('Changing the values of the combobox and the listbox');

  //setting combobox value to 12
  this.getField("combobox1").value = '12';

  //setting listbox value to option 3
  this.getField("listbox1").value = 'option 3';

  // Get the current date and time
  var rightNow = new Date();
  rightNow 	= rightNow.toLocaleDateString();

  var fld = this.getField("date");
  //Change the date field border to dark blue
  app.alert('Changing the date field border to dark blue, setting the date to current value, and changing the date text color to purple.');
  fld.strokeColor = ["RGB",0.5,0.5,1];

  //Change fill to light blue, won't work for unknown reason
  fld.Color = ["RGB",0,0,0.7];

  //set the value of the date field to current date
  fld.value 	= rightNow;

  //Change text of the date field to purple
  fld.textColor = ["RGB", 0.5, 0, 0.5];


 }

Run js button action.png


(c)

The content of this page is available under the Creative Commons Attribution-ShareAlike Licence and Free Documentation Licence

If you alter, transform, or build upon this work, you may distribute the resulting work either under one or under both of the abovementioned licences.