pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Grosso <gary.gro...@oberontech.com>
Subject calculating a field with a JavaScript action
Date Mon, 31 Jul 2017 04:33:05 GMT
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;
                            }
                   }
            }
        }

    }


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message