You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gary Grosso <ga...@oberontech.com> on 2017/08/01 03:39:17 UTC
RE: calculating a field with a JavaScript action
(palm meet face)
Thanks, Maruan. I was aware of calculation orders, even used it before in my early proof of principle, but then got to thinking it would only be necessary if things needed to be calculated in other than "document order".
Of course there is no reliable "document order" in a PDF file anyway. I will confirm in the morning, but I quite expect this is what I need to fix.
Gary
-----Original Message-----
From: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
Sent: Monday, July 31, 2017 9:13 AM
To: users@pdfbox.apache.org
Subject: Re: calculating a field with a JavaScript action
Hi,
> Am 31.07.2017 um 14:10 schrieb Gary Grosso <ga...@oberontech.com>:
>
> Perhaps I should have mentioned that I can get the other events to trigger an alert. My problem is not being able to set a value with "event.value=".
I havn't looked at the code but quickly compared the PDFs. The non Acrobat version is missing the entries for the calculation order /CO entry on AcroForm level.
BR
Maruan
>
> -----Original Message-----
> From: Gary Grosso [mailto:gary.grosso@oberontech.com]
> Sent: Monday, July 31, 2017 12:33 AM
> To: users@pdfbox.apache.org
> Subject: calculating a field with a JavaScript action
>
> I would greatly appreciate any help with this endeavor.
>
> I have a document with 3 textboxes and I'm trying to set the value of the 3rd with the sum of the first 2.
>
> My resulting PDF is at http://aapro.net/PDF/actionTestOnChangeElsewhere.pdf.
>
> A PDF created is at http://aapro.net/PDF/actionTestAcrobatCalculated.pdf.
>
> The two are pretty similar.
>
> Perhaps someone can point out what is the significant difference between the two, or the error in my code, below.
>
> There is another possibility, that my PDF file is somehow invalid, which makes anything I do inadequate.
>
> Anyway, I'll be grateful for any advice.
>
> public static void addTextboxAtRect(PDPage page, PDAcroForm acroForm, String name, Map<String, String> optionMap, PDRectangle rect) {
> System.out.println("addTextboxAtRect(" + name + ", " + rect.toString() + ")");
> PDTextField textBox = new PDTextField(acroForm);
>
> // Ideally we would make sure each name is unique.
> String nameStr = name;
> textBox.setPartialName(nameStr);
>
> String tip = optionMap.get("tip");
> if ((tip != null) && !tip.isEmpty()) {
> textBox.setAlternateFieldName(tip);
> }
> if (Utils.isTrue(optionMap.get("nospell"))) {
> textBox.setDoNotSpellCheck(true);
> }
> if (Utils.isTrue(optionMap.get("ro"))) {
> textBox.setReadOnly(true);
> }
> if (Utils.isTrue(optionMap.get("req"))) {
> textBox.setRequired(true);
> }
> int maxlen = Utils.getInt(optionMap.get("maxlen"));
> if (maxlen > -1) {
> textBox.setMaxLen(maxlen);
> }
>
> String defaultAppearanceString = "/" + Utils.fntStr(optionMap.get("fnt")) + " "
> + Utils.fszStr(optionMap.get("fsz")) + " Tf " + Utils.hexToRgb(optionMap.get("fclr")) + " rg";
> System.out.println("defaultAppearanceString=" + defaultAppearanceString);
> textBox.setDefaultAppearance(defaultAppearanceString);
> // add the field to the acroform
> acroForm.getFields().add(textBox);
>
> // Specify the annotation associated with the field
> PDAnnotationWidget widget = textBox.getWidgets().get(0);
> widget.setRectangle(rect);
> if (rect.getHeight() < MIN_RECT_HEIGHT) {
> float diff = MIN_RECT_HEIGHT - rect.getHeight();
> rect.setLowerLeftY((float) (rect.getLowerLeftY() - diff/2.0));
> rect.setUpperRightY((float) (rect.getUpperRightY() + diff/2.0));
> }
> widget.setPage(page);
>
>
> COSDictionary apNDict = new COSDictionary();
>
> apNDict.setItem(COSName.BBOX, rect);
> apNDict.setItem(COSName.FORMTYPE, COSInteger.ONE);
> apNDict.setItem(COSName.TYPE, COSName.XOBJECT);
> apNDict.setItem(COSName.SUBTYPE, COSName.FORM);
>
> PDAppearanceDictionary appearance = new PDAppearanceDictionary();
> PDAppearanceEntry appearanceNEntry = new PDAppearanceEntry(apNDict);
>
> appearance.setNormalAppearance(appearanceNEntry);
>
> widget.setAppearance(appearance);
>
>
> PDFormFieldAdditionalActions fieldActions = new PDFormFieldAdditionalActions();
> PDAnnotationAdditionalActions annotationActions = new PDAnnotationAdditionalActions();
> setWidgetActions(optionMap, fieldActions, annotationActions);
> widget.setActions(annotationActions);
>
> PDAppearanceCharacteristicsDictionary fieldAppearance
> = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
> fieldAppearance.setBorderColour(Utils.hexToPDColor(optionMap.get("brdclr")));
> fieldAppearance.setBackground(Utils.hexToPDColor(optionMap.get("bclr")));
> widget.setAppearanceCharacteristics(fieldAppearance);
>
> PDField field = acroForm.getField(nameStr);
>
> field.getActions().getCOSObject().addAll(fieldActions.getCOSObject());
>
> // make sure the annotation is visible on screen and paper
> widget.setPrinted(true);
> // Add the annotation to the page
> try {
> page.getAnnotations().add(widget);
> } catch (IOException e) {
> e.printStackTrace();
> }
> String dflt = Utils.unescapeChar(optionMap.get("dflt"));
> if ((dflt != null) && !dflt.isEmpty()) {
> try {
> textBox.setValue(dflt);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
>
> private static void setWidgetActions(Map<String, String> optionMap, PDFormFieldAdditionalActions fieldActions, PDAnnotationAdditionalActions annotationActions) {
> if ((optionMap.get("js") != null) && !optionMap.get("js").isEmpty()) {
> String[] jsStrings = optionMap.get("js").split("\\|");
> String[] evts = optionMap.get("evt").split("\\|");
> for (int ix = 0 ; ix < jsStrings.length ; ix++) {
> String jsString = jsStrings[ix];
> String evt = evts[ix]; // jsStrings and evts are assumed to be parallel arrays
> PDActionJavaScript jsAction = new PDActionJavaScript();
> //Creating PDActionJavaScript object
> jsAction.setAction(Utils.decodeJsString(jsString));
> System.out.println(jsAction.getAction());
> if ((evt != null) && !evt.isEmpty()) {
> switch (evt) {
> case "onCursorEnters":
> // This refers to the mouse pointer, not the typing cursor.
> annotationActions.setE(jsAction);
> break;
> case "onCursorExits":
> // This refers to the mouse pointer, not the typing cursor.
> annotationActions.setX(jsAction);
> break;
> case "onFocus":
> annotationActions.setFo(jsAction);
> break;
> case "onBlur":
> /*
> * This will set an action to be performed when the annotation loses the input focus. The name Bl stands for "blurred".
> */
> annotationActions.setBl(jsAction);
> break;
> case "onKeyPress":
> fieldActions.setK(jsAction);
> break;
> case "onMouseDown":
> annotationActions.setD(jsAction);
> break;
> case "onMouseUp":
> annotationActions.setU(jsAction);
> break;
> case "onChange":
> fieldActions.setF(jsAction);
> break;
> case "onFormat":
> /*
> * This will set a JavaScript action to be performed before
> * the field is formatted to display its current value. This
> * allows the field's value to be modified before
> * formatting.
> */
> fieldActions.setV(jsAction);
> break;
> case "onPageClose":
> annotationActions.setPC(jsAction);
> break;
> case "onPageOpen":
> annotationActions.setPO(jsAction);
> break;
> case "onChangeElsewhere":
> /*
> * This will set a JavaScript action to be performed in
> * order to recalculate the value of this field when that of
> * another field changes. The order in which the document's
> * fields are recalculated is defined by the CO entry in the
> * interactive form dictionary.
> */
> fieldActions.setC(jsAction);
> break;
> }
> }
> }
> }
>
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
RE: calculating a field with a JavaScript action
Posted by Gary Grosso <ga...@oberontech.com>.
Yes, that was it alright.
Thanks again, Maruan.
-----Original Message-----
From: Gary Grosso [mailto:gary.grosso@oberontech.com]
Sent: Monday, July 31, 2017 11:39 PM
To: users@pdfbox.apache.org
Subject: RE: calculating a field with a JavaScript action
(palm meet face)
Thanks, Maruan. I was aware of calculation orders, even used it before in my early proof of principle, but then got to thinking it would only be necessary if things needed to be calculated in other than "document order".
Of course there is no reliable "document order" in a PDF file anyway. I will confirm in the morning, but I quite expect this is what I need to fix.
Gary
-----Original Message-----
From: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
Sent: Monday, July 31, 2017 9:13 AM
To: users@pdfbox.apache.org
Subject: Re: calculating a field with a JavaScript action
Hi,
> Am 31.07.2017 um 14:10 schrieb Gary Grosso <ga...@oberontech.com>:
>
> Perhaps I should have mentioned that I can get the other events to trigger an alert. My problem is not being able to set a value with "event.value=".
I havn't looked at the code but quickly compared the PDFs. The non Acrobat version is missing the entries for the calculation order /CO entry on AcroForm level.
BR
Maruan
>
> -----Original Message-----
> From: Gary Grosso [mailto:gary.grosso@oberontech.com]
> Sent: Monday, July 31, 2017 12:33 AM
> To: users@pdfbox.apache.org
> Subject: calculating a field with a JavaScript action
>
> I would greatly appreciate any help with this endeavor.
>
> I have a document with 3 textboxes and I'm trying to set the value of the 3rd with the sum of the first 2.
>
> My resulting PDF is at http://aapro.net/PDF/actionTestOnChangeElsewhere.pdf.
>
> A PDF created is at http://aapro.net/PDF/actionTestAcrobatCalculated.pdf.
>
> The two are pretty similar.
>
> Perhaps someone can point out what is the significant difference between the two, or the error in my code, below.
>
> There is another possibility, that my PDF file is somehow invalid, which makes anything I do inadequate.
>
> Anyway, I'll be grateful for any advice.
>
> public static void addTextboxAtRect(PDPage page, PDAcroForm acroForm, String name, Map<String, String> optionMap, PDRectangle rect) {
> System.out.println("addTextboxAtRect(" + name + ", " + rect.toString() + ")");
> PDTextField textBox = new PDTextField(acroForm);
>
> // Ideally we would make sure each name is unique.
> String nameStr = name;
> textBox.setPartialName(nameStr);
>
> String tip = optionMap.get("tip");
> if ((tip != null) && !tip.isEmpty()) {
> textBox.setAlternateFieldName(tip);
> }
> if (Utils.isTrue(optionMap.get("nospell"))) {
> textBox.setDoNotSpellCheck(true);
> }
> if (Utils.isTrue(optionMap.get("ro"))) {
> textBox.setReadOnly(true);
> }
> if (Utils.isTrue(optionMap.get("req"))) {
> textBox.setRequired(true);
> }
> int maxlen = Utils.getInt(optionMap.get("maxlen"));
> if (maxlen > -1) {
> textBox.setMaxLen(maxlen);
> }
>
> String defaultAppearanceString = "/" + Utils.fntStr(optionMap.get("fnt")) + " "
> + Utils.fszStr(optionMap.get("fsz")) + " Tf " + Utils.hexToRgb(optionMap.get("fclr")) + " rg";
> System.out.println("defaultAppearanceString=" + defaultAppearanceString);
> textBox.setDefaultAppearance(defaultAppearanceString);
> // add the field to the acroform
> acroForm.getFields().add(textBox);
>
> // Specify the annotation associated with the field
> PDAnnotationWidget widget = textBox.getWidgets().get(0);
> widget.setRectangle(rect);
> if (rect.getHeight() < MIN_RECT_HEIGHT) {
> float diff = MIN_RECT_HEIGHT - rect.getHeight();
> rect.setLowerLeftY((float) (rect.getLowerLeftY() - diff/2.0));
> rect.setUpperRightY((float) (rect.getUpperRightY() + diff/2.0));
> }
> widget.setPage(page);
>
>
> COSDictionary apNDict = new COSDictionary();
>
> apNDict.setItem(COSName.BBOX, rect);
> apNDict.setItem(COSName.FORMTYPE, COSInteger.ONE);
> apNDict.setItem(COSName.TYPE, COSName.XOBJECT);
> apNDict.setItem(COSName.SUBTYPE, COSName.FORM);
>
> PDAppearanceDictionary appearance = new PDAppearanceDictionary();
> PDAppearanceEntry appearanceNEntry = new PDAppearanceEntry(apNDict);
>
> appearance.setNormalAppearance(appearanceNEntry);
>
> widget.setAppearance(appearance);
>
>
> PDFormFieldAdditionalActions fieldActions = new PDFormFieldAdditionalActions();
> PDAnnotationAdditionalActions annotationActions = new PDAnnotationAdditionalActions();
> setWidgetActions(optionMap, fieldActions, annotationActions);
> widget.setActions(annotationActions);
>
> PDAppearanceCharacteristicsDictionary fieldAppearance
> = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
> fieldAppearance.setBorderColour(Utils.hexToPDColor(optionMap.get("brdclr")));
> fieldAppearance.setBackground(Utils.hexToPDColor(optionMap.get("bclr")));
> widget.setAppearanceCharacteristics(fieldAppearance);
>
> PDField field = acroForm.getField(nameStr);
>
> field.getActions().getCOSObject().addAll(fieldActions.getCOSObject());
>
> // make sure the annotation is visible on screen and paper
> widget.setPrinted(true);
> // Add the annotation to the page
> try {
> page.getAnnotations().add(widget);
> } catch (IOException e) {
> e.printStackTrace();
> }
> String dflt = Utils.unescapeChar(optionMap.get("dflt"));
> if ((dflt != null) && !dflt.isEmpty()) {
> try {
> textBox.setValue(dflt);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
>
> private static void setWidgetActions(Map<String, String> optionMap, PDFormFieldAdditionalActions fieldActions, PDAnnotationAdditionalActions annotationActions) {
> if ((optionMap.get("js") != null) && !optionMap.get("js").isEmpty()) {
> String[] jsStrings = optionMap.get("js").split("\\|");
> String[] evts = optionMap.get("evt").split("\\|");
> for (int ix = 0 ; ix < jsStrings.length ; ix++) {
> String jsString = jsStrings[ix];
> String evt = evts[ix]; // jsStrings and evts are assumed to be parallel arrays
> PDActionJavaScript jsAction = new PDActionJavaScript();
> //Creating PDActionJavaScript object
> jsAction.setAction(Utils.decodeJsString(jsString));
> System.out.println(jsAction.getAction());
> if ((evt != null) && !evt.isEmpty()) {
> switch (evt) {
> case "onCursorEnters":
> // This refers to the mouse pointer, not the typing cursor.
> annotationActions.setE(jsAction);
> break;
> case "onCursorExits":
> // This refers to the mouse pointer, not the typing cursor.
> annotationActions.setX(jsAction);
> break;
> case "onFocus":
> annotationActions.setFo(jsAction);
> break;
> case "onBlur":
> /*
> * This will set an action to be performed when the annotation loses the input focus. The name Bl stands for "blurred".
> */
> annotationActions.setBl(jsAction);
> break;
> case "onKeyPress":
> fieldActions.setK(jsAction);
> break;
> case "onMouseDown":
> annotationActions.setD(jsAction);
> break;
> case "onMouseUp":
> annotationActions.setU(jsAction);
> break;
> case "onChange":
> fieldActions.setF(jsAction);
> break;
> case "onFormat":
> /*
> * This will set a JavaScript action to be performed before
> * the field is formatted to display its current value. This
> * allows the field's value to be modified before
> * formatting.
> */
> fieldActions.setV(jsAction);
> break;
> case "onPageClose":
> annotationActions.setPC(jsAction);
> break;
> case "onPageOpen":
> annotationActions.setPO(jsAction);
> break;
> case "onChangeElsewhere":
> /*
> * This will set a JavaScript action to be performed in
> * order to recalculate the value of this field when that of
> * another field changes. The order in which the document's
> * fields are recalculated is defined by the CO entry in the
> * interactive form dictionary.
> */
> fieldActions.setC(jsAction);
> break;
> }
> }
> }
> }
>
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org