Example usage for com.itextpdf.text.pdf AcroFields getNewPushbuttonFromField

List of usage examples for com.itextpdf.text.pdf AcroFields getNewPushbuttonFromField

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf AcroFields getNewPushbuttonFromField.

Prototype

public PushbuttonField getNewPushbuttonFromField(String field) 

Source Link

Document

Creates a new pushbutton from an existing field.

Usage

From source file:org.smap.sdal.managers.PDFSurveyManager.java

License:Open Source License

private void fillTemplate(GlobalVariables gv, AcroFields pdfForm, ArrayList<Result> record, String basePath,
        String formName, int repeatIndex, String serverRoot, PdfStamper stamper, int oId)
        throws IOException, DocumentException {
    try {/* www  .j ava 2s.c  om*/

        for (Result r : record) {

            String value = "";
            boolean hideLabel = false;
            String fieldName = getFieldName(formName, repeatIndex, r.name);
            String fieldNameQR = getFieldName(formName, repeatIndex, r.name + "_qr");

            DisplayItem di = new DisplayItem();
            try {
                Form form = survey.forms.get(r.fIdx);
                Question question = getQuestionFromResult(sd, r, form);
                setQuestionFormats(question.appearance, di);
            } catch (Exception e) {
                // If we can't get the question details for this data then that is ok
            }

            /*
             * Set the value based on the result
             * Process subforms if this is a repeating group
             */
            if (r.type.equals("form")) {
                for (int k = 0; k < r.subForm.size(); k++) {
                    fillTemplate(gv, pdfForm, r.subForm.get(k), basePath, fieldName, k, serverRoot, stamper,
                            oId);
                }
            } else if (r.type.equals("select1")) {

                Form form = survey.forms.get(r.fIdx);
                Question question = form.questions.get(r.qIdx);

                ArrayList<String> matches = new ArrayList<String>();
                matches.add(r.value);
                value = choiceManager.getLabel(sd, cResults, user, oId, survey.id, question.id, question.l_id,
                        question.external_choices, question.external_table,
                        survey.languages.get(languageIdx).name, languageIdx, matches, survey.ident);

            } else if (r.type.equals("select")) {

                String nameValue = r.value;
                if (nameValue != null) {
                    String vArray[] = nameValue.split(" ");
                    ArrayList<String> matches = new ArrayList<String>();
                    if (vArray != null) {
                        for (String v : vArray) {
                            matches.add(v);
                        }
                    }
                    Form form = survey.forms.get(r.fIdx);
                    Question question = form.questions.get(r.qIdx);
                    value = choiceManager.getLabel(sd, cResults, user, oId, survey.id, question.id,
                            question.l_id, question.external_choices, question.external_table,
                            survey.languages.get(languageIdx).name, languageIdx, matches, survey.ident);
                }

            } else if (r.type.equals("dateTime") || r.type.equals("timestamp")) {

                value = null;
                if (r.value != null) {
                    // Convert date to local time
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    df.setTimeZone(TimeZone.getTimeZone("UTC"));
                    Date date = df.parse(r.value);
                    df.setTimeZone(TimeZone.getTimeZone(tz));
                    value = df.format(date);
                    log.info("Convert date to local time (template): " + r.name + " : " + r.value + " : "
                            + " : " + value + " : " + r.type + " : " + tz);
                }

            } else {
                value = r.value;
            }

            /*
             * Add the value to the form
             * Alternatively remove the fieldName if the value is empty.
             */
            if (value == null || value.trim().equals("")) {
                try {
                    pdfForm.removeField(fieldName);
                } catch (Exception e) {
                    log.info("Error removing field: " + fieldName + ": " + e.getMessage());
                }

            } else if (r.type.equals("geopoint") || r.type.equals("geoshape") || r.type.equals("geotrace")
                    || r.type.startsWith("geopolygon_") || r.type.startsWith("geolinestring_")) {

                PushbuttonField ad = pdfForm.getNewPushbuttonFromField(fieldName);
                if (ad != null) {
                    Image img = PdfUtilities.getMapImage(sd, di.map, r.value, di.location, di.zoom,
                            gv.mapbox_key, survey.id, user, di.markerColor);
                    PdfUtilities.addMapImageTemplate(pdfForm, ad, fieldName, img);
                } else {
                    log.info("No field for image (Mapbox not called: " + fieldName);
                }

            } else if (r.type.equals("image") || r.type.equals("video") || r.type.equals("audio")) {
                PdfUtilities.addImageTemplate(pdfForm, fieldName, basePath, value, serverRoot, stamper,
                        defaultFontLink);

            } else {
                if (hideLabel) {
                    try {
                        pdfForm.removeField(fieldName);
                    } catch (Exception e) {
                        log.info("Error removing field: " + fieldName + ": " + e.getMessage());
                    }
                } else {
                    if (di.isBarcode) {
                        PushbuttonField ad = pdfForm.getNewPushbuttonFromField(fieldName);
                        if (ad != null) {
                            BarcodeQRCode qrcode = new BarcodeQRCode(value.trim(), 1, 1, null);
                            Image qrcodeImage = qrcode.getImage();
                            qrcodeImage.setAbsolutePosition(10, 500);
                            qrcodeImage.scalePercent(200);
                            PdfUtilities.addMapImageTemplate(pdfForm, ad, fieldName, qrcodeImage);
                        }
                    } else {
                        pdfForm.setField(fieldName, value);

                    }
                }
            }

            /*
             * Add any QR code values to fields that have been identified using the QR suffix
             */
            if (fieldNameQR != null && value != null && value.trim().length() > 0) {
                PushbuttonField ad = pdfForm.getNewPushbuttonFromField(fieldName);
                if (ad != null) {
                    BarcodeQRCode qrcode = new BarcodeQRCode(value.trim(), 1, 1, null);
                    Image qrcodeImage = qrcode.getImage();
                    qrcodeImage.setAbsolutePosition(10, 500);
                    qrcodeImage.scalePercent(200);
                    PdfUtilities.addMapImageTemplate(pdfForm, ad, fieldNameQR, qrcodeImage);
                }
            }

        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error filling template", e);
    }
}

From source file:org.smap.sdal.managers.PDFSurveyManager.java

License:Open Source License

private static void fillTemplateUserDetails(AcroFields pdfForm, User user, String basePath)
        throws IOException, DocumentException {
    try {/* ww w .  java  2s  . c o m*/

        pdfForm.setField("user_name", user.name);
        pdfForm.setField("user_company", user.company_name);

        /*
         * User configurable data TODO This should be an array of key value pairs
         * As interim use a hard coded class to hold the data
         */
        String settings = user.settings;
        Type type = new TypeToken<UserSettings>() {
        }.getType();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        UserSettings us = gson.fromJson(settings, type);

        if (us != null) {
            pdfForm.setField("user_title", us.title);
            pdfForm.setField("user_license", us.license);

            PushbuttonField ad = pdfForm.getNewPushbuttonFromField("user_signature");
            if (ad != null) {
                ad.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
                ad.setProportionalIcon(true);
                String filename = null;
                try {
                    filename = basePath + "/media/users/" + user.id + "/sig/" + user.signature;
                    ad.setImage(Image.getInstance(filename));
                } catch (Exception e) {
                    log.info("Error: Failed to add signature " + filename + " to pdf");
                }
                pdfForm.replacePushbuttonField("user_signature", ad.getField());
            } else {
                //log.info("Picture field: user_signature not found");
            }
        }

    } catch (Exception e) {
        log.log(Level.SEVERE, "Error filling template", e);
    }
}