Example usage for com.itextpdf.text.pdf PdfPCell setRunDirection

List of usage examples for com.itextpdf.text.pdf PdfPCell setRunDirection

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfPCell setRunDirection.

Prototype

public void setRunDirection(int runDirection) 

Source Link

Document

Sets the run direction of the text content in the cell.

Usage

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

License:Open Source License

private PdfPTable addDisplayItem(Parser parser, DisplayItem di, String basePath, String serverRoot,
        boolean generateBlank, GlobalVariables gv, String remoteUser, int oId)
        throws BadElementException, MalformedURLException, IOException {

    PdfPCell labelCell = new PdfPCell();
    PdfPCell valueCell = new PdfPCell();
    labelCell.setBorderColor(BaseColor.LIGHT_GRAY);
    valueCell.setBorderColor(BaseColor.LIGHT_GRAY);

    PdfPTable tItem = null;/* ww w .  j  av  a 2 s .  co  m*/

    // Add label
    StringBuffer html = new StringBuffer();
    html.append("<span class='label ");
    if (di.labelbold) {
        html.append(" lbold");
    }

    // Get text value
    String textValue = "";
    if (di.text != null && di.text.trim().length() > 0) {
        textValue = di.text;
    } else {
        textValue = di.name;
    }
    textValue = textValue.trim();
    if (textValue.charAt(textValue.length() - 1) != ':') {
        textValue += ":";
    }
    if (di.labelcaps) {
        textValue = textValue.toUpperCase();
    }

    // Add language class
    html.append(GeneralUtilityMethods.getLanguage(textValue));
    html.append("'>");

    // Add text value
    html.append(GeneralUtilityMethods.unesc(textValue));
    html.append("</span>");

    // Only include hints if we are generating a blank template
    if (generateBlank) {
        html.append("<span class='hint ");
        if (di.hint != null) {
            html.append(GeneralUtilityMethods.getLanguage(di.hint));
            html.append("'>");
            html.append(GeneralUtilityMethods.unesc(di.hint));
        }
        html.append("</span>");
    }

    parser.elements.clear();
    try {
        parser.xmlParser.parse(new StringReader(html.toString()));

        for (Element element : parser.elements) {
            if (textValue != null && textValue.length() > 0) {
                if (GeneralUtilityMethods.isRtlLanguage(textValue)) {
                    labelCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
                }
            } else if (di.hint != null && di.hint.length() > 0) {
                if (GeneralUtilityMethods.isRtlLanguage(textValue)) {
                    labelCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
                }
            }
            labelCell.addElement(element);
        }
    } catch (Exception e) {
        log.info("Error parsing: " + html.toString() + " : " + e.getMessage());
        lm.writeLog(sd, survey.getId(), remoteUser, LogManager.ERROR,
                e.getMessage() + " for: " + html.toString());
        labelCell.addElement(getPara(html.toString(), di, gv, null, null));
    }

    // Set the content of the value cell
    try {
        updateValueCell(parser, remoteUser, valueCell, di, generateBlank, basePath, serverRoot, gv, oId);
    } catch (Exception e) {
        log.info("Error updating value cell, continuing: " + basePath + " : " + di.value);
        log.log(Level.SEVERE, "Exception", e);
    }

    int widthValue = 5;
    if (di.widthLabel == 10) {
        widthValue = 1; // Label and value in 1 column
        di.widthLabel = 1;
        tItem = new PdfPTable(1);
    } else {
        // Label and value in 2 columns
        widthValue = 10 - di.widthLabel;
        tItem = new PdfPTable(10);
    }
    // Format label cell
    labelCell.setColspan(di.widthLabel);
    if (di.labelbg != null) {
        labelCell.setBackgroundColor(di.labelbg);
    }

    // Format value cell
    valueCell.setColspan(widthValue);
    if (di.valueHeight > -1.0) {
        valueCell.setMinimumHeight((float) di.valueHeight);
    }
    if (di.valuebg != null) {
        valueCell.setBackgroundColor(di.valuebg);
    }

    tItem.addCell(labelCell);
    tItem.addCell(valueCell);
    return tItem;
}

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

License:Open Source License

private void updateValueCell(Parser parser, String remoteUser, PdfPCell valueCell, DisplayItem di,
        boolean generateBlank, String basePath, String serverRoot, GlobalVariables gv, int oId)
        throws Exception {

    // Questions that append their values to this question
    ArrayList<String> deps = gv.addToList.get(di.fIdx + "_" + di.rec_number + "_" + di.name);

    if (di.type.startsWith("select")) {
        processSelect(parser, remoteUser, valueCell, di, generateBlank, gv, oId);
    } else if (di.type.equals("image")) {
        if (di.value != null && !di.value.trim().equals("") && !di.value.trim().equals("Unknown")) {
            if (di.isHyperlink) {
                Anchor anchor = new Anchor(serverRoot + di.value);
                anchor.setReference(serverRoot + di.value);

                valueCell.addElement(getPara("", di, gv, deps, anchor));
            } else {
                try {
                    Image img = Image.getInstance(basePath + "/" + di.value);
                    valueCell.addElement(img);
                } catch (Exception e) {
                    log.info("Error: image " + basePath + "/" + di.value + " not added: " + e.getMessage());
                    log.log(Level.SEVERE, "Adding image to pdf", e);
                }/* ww  w  .ja va  2 s  . c o m*/
            }

        } else {
            // TODO add empty image
        }

    } else if (di.type.equals("video") || di.type.equals("audio")) {
        if (di.value != null && !di.value.trim().equals("") && !di.value.trim().equals("Unknown")) {
            Anchor anchor = new Anchor(serverRoot + di.value);
            anchor.setReference(serverRoot + di.value);

            valueCell.addElement(getPara("", di, gv, deps, anchor));

        } else {
            // TODO add empty image
        }

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

        Image img = PdfUtilities.getMapImage(sd, di.map, di.value, di.location, di.zoom, gv.mapbox_key,
                survey.id, user, di.markerColor);

        if (img != null) {
            valueCell.addElement(img);
        } else {
            valueCell.addElement(getPara(" ", di, gv, deps, null));
        }

    } else if (di.isBarcode) {
        BarcodeQRCode qrcode = new BarcodeQRCode(di.value.trim(), 1, 1, null);
        Image qrcodeImage = qrcode.getImage();
        qrcodeImage.setAbsolutePosition(10, 500);
        qrcodeImage.scalePercent(200);

        valueCell.addElement((qrcodeImage));

    } else {
        // Todo process other question types
        String value = null;

        if (di.value == null || di.value.trim().length() == 0) {
            value = " "; // Need a space to show a blank row
        } else {
            if (di.value != null && di.value.length() > 0) {
                if (GeneralUtilityMethods.isRtlLanguage(di.value)) {
                    valueCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
                }
            }

            if (di.type.equals("dateTime") || di.type.equals("timestamp")) { // Set date time to local time

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

        }
        valueCell.addElement(getPara(value, di, gv, deps, null));
    }
}

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

License:Open Source License

private void processSelect(Parser parser, String remoteUser, PdfPCell cell, DisplayItem di,
        boolean generateBlank, GlobalVariables gv, int oId) throws Exception {

    Font f = null;//ww w.  j av a 2  s  . com
    boolean isRtl = false;

    // If generating blank template
    List list = new List();
    list.setAutoindent(false);
    list.setSymbolIndent(24);

    String lang;

    boolean isSelectMultiple = di.type.equals("select") ? true : false;

    // Questions that append their values to this question
    ArrayList<String> deps = gv.addToList.get(di.fIdx + "_" + di.rec_number + "_" + di.name);

    /*
     * Add the value of this question unless
     *   The form is not blank and the value is "other" and their are 1 or more dependent questions
     *   In this case we assume that its only the values of the dependent questions that are needed
     */

    if (generateBlank) {
        // TODO get real choices using choice manager
        Form form = survey.forms.get(di.fIdx);
        Question question = form.questions.get(di.qIdx);
        OptionList ol = survey.optionLists.get(question.list_name);
        for (Option o : ol.options) {

            String text = null;
            if (o.display_name != null && o.display_name.trim().length() > 0) {
                text = o.display_name;
            } else {
                text = o.labels.get(languageIdx).text;
            }
            lang = GeneralUtilityMethods.getLanguage(text);
            f = getFont(lang);
            isRtl = isRtl(lang);

            ListItem item = new ListItem(GeneralUtilityMethods.unesc(text), f);

            if (isSelectMultiple) {
                item.setListSymbol(new Chunk("\uf096", Symbols));
                list.add(item);
            } else {
                item.setListSymbol(new Chunk("\uf10c", Symbols));
                list.add(item);
            }
        }

        if (isRtl) {
            cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        }
        cell.addElement(list);

    } else {
        if (deps == null || (di.value != null && !di.value.trim().toLowerCase().equals("other"))) {

            String value = di.value;
            if (di.type.equals("select1")) {

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

                ArrayList<String> matches = new ArrayList<String>();
                matches.add(di.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 (di.type.equals("select")) {
                String nameValue = 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(di.fIdx);
                    Question question = form.questions.get(di.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);
                }
            }

            if (GeneralUtilityMethods.isRtlLanguage(di.value)) {
                cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            }
            parser.elements.clear();
            String html = getHtml(value, di, gv, deps);
            try {
                parser.xmlParser.parse(new StringReader(html));
            } catch (Exception e) {
                log.info("Error parsing: " + html.toString() + " : " + e.getMessage());
                lm.writeLog(sd, survey.getId(), remoteUser, LogManager.ERROR,
                        e.getMessage() + " for: " + html.toString());
                cell.addElement(getPara(html.toString(), di, gv, null, null));
            }
            for (Element element : parser.elements) {
                cell.addElement(element);
            }
            //cell.addElement(getPara(value, di, gv, deps, null));
        }

    }

}