0

I have a pdf form and I am writing some data to the pdf fields through code using iText & Java. I want to get Custom Format Script of the fields so that I get to know the valid inputs for the pdf fields. Thanks

Prabhakar
  • 402
  • 6
  • 20
  • I don't understand why you're using the [xpages-ssjs] tag. JavaScript in PDF is totally unrelated to PDF ECMAScript. Can that tag be removed? – Bruno Lowagie Apr 02 '14 at 13:32
  • @BrunoLowagie I have used that tag because at the end I will be implementing this in xpages, so thought if someone having xpage background already did it then it may help. Thanks – Prabhakar Apr 02 '14 at 14:06
  • @BrunoLowagie I don't understand why you rated the question as -ve, could you please explain me? It would help me for not making same mistake in future. Thanks – Prabhakar Apr 02 '14 at 14:07
  • I didn't vote the question down, somebody else did. I don't know why. – Bruno Lowagie Apr 02 '14 at 14:20

1 Answers1

1

Update:

OOPS, I misinterpreted your question. I assumed you wanted to add JavaScript to an existing PDF so that the user can add valid data.

What you are looking for is a way to extract the JavaScript from an existing PDF. You already get an impression on how to do that in the answer I referred to in my previous answer. For JavaScript added to specific fields, you need to inspect the Additional Actions:

AcroFields form = stamper.getAcroFields();
AcroFields.Item fd = form.getFieldItem("married");
// Get the PDF dictionary of the YES radio button and add an additional action
PdfDictionary dictYes =
    (PdfDictionary) PdfReader.getPdfObject(fd.getWidgetRef(0));
PdfDictionary yesAction = dictYes.getAsDict(PdfName.AA);
if (yesAction == null) yesAction = new PdfDictionary();
yesAction.put(new PdfName("Fo"),
        PdfAction.javaScript("setReadOnly(false);", stamper.getWriter()));
dictYes.put(PdfName.AA, yesAction);
// Get the PDF dictionary of the NO radio button and add an additional action
PdfDictionary dictNo =
    (PdfDictionary) PdfReader.getPdfObject(fd.getWidgetRef(1));
PdfDictionary noAction = dictNo.getAsDict(PdfName.AA);

If noAction isn't null, you'll need to examine the different values in that dictionary. For instance: the /Bl entry (if present) will give you the action that is triggered on blur, the /Fo entry (if present) will give you the action that is triggerd on focus, and so on.

If you want to get the document-level JavaScript, you need to fetch the appropriate entry from the name tree in the Catalog of the PDF document. It is hard to explain in words, but if you download RUPS and inspect the PDF, you should be able to find the different JavaScript snippets. If we look at the file created using my incorrect answer (see below), we get this:

JavaScript viewed in RUPS

This shows that you need something like this:

PdfDictionary catalog = reader.getCatalog();
PdfDictionary names = catalog.getAsDict(PdfName.NAMES);
PdfDictionary javascript = names.getAsDict(PdfName.JAVASCRIPT);

Once you have this javascript dictionary, you can extract all the Javascript snippets.

Incorrect answer:

I assume that you know how to write JavaScript (or more correctly ECMAScript). JavaScript in PDF is very similar to JavaScript in HTML. I assume you don't need help to write some JavaScript methods to check if input is valid.

If that is the case, you only need to know how to add the JavaScript to an existing PDF file. For instance: I have this PDF file named form_without_js.pdf to which I want to add some javascript, for instance extra.js. In extra.js, you'll find a method that sets a field to read-only as well as a method that validates a field: if the value of married is yes and there is no value for partner, an alert is shown, otherwise the form is submitted. You'll have to write similar JavaScript depending on the nature of your form and which fields you want to check.

The AddJavaScriptToForm example shows you how to add these JavaScript methods to the existing PDF, resulting in the file form_with_js.pdf.

This is done with PdfReader and PdfStamper:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
// do stuff
stamper.close();
reader.close();

Where it says // do stuff, you need to do two things:

[1.] You need to add the JavaScript snippet like this:

PdfWriter writer = stamper.getWriter();
writer.addJavaScript(Utilities.readFileToString(RESOURCE));

[2.] You need to add some JavaScript to specific fields to call the custom methods you've added. In the example, you see a case where we add JavaScript as an additional action. You also see how we add a new button with a specific action. It's up to you to decide what is needed in your specific case.

miken32
  • 42,008
  • 16
  • 111
  • 154
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165