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

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

Introduction

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

Prototype

public boolean removeField(String name) 

Source Link

Document

Removes a field from the document.

Usage

From source file:org.h819.commons.file.MyPDFUtils.java

/**
 * http://www.javabeat.net/javascript-in-pdf-documents-using-itext/
 * http://www.javabeat.net/javascript-communication-between-html-and-pdf-in-itext/
 *  PDF  field  javaScript//  w w  w  . java 2 s .  c o  m
 * <p>
 * pdf ? javaScript?????
 * <p>
 * javaScript ?? filed  filed javaScript ???
 * <p>
 * ?? javaScript 
 * <p>
 *  itext  reader.getJavaScript()? pdf  JavaScript??(adobe pro
 * ????)
 * <p>
 * ??? field ??
 *
 * @param srcPdfFileDir   ?
 * @param descPdfFileDir  
 * @param removeFieldName  javaScript ???
 * @throws java.io.IOException
 */
public static void removeFieldJavaScript(File srcPdfFileDir, File descPdfFileDir, String removeFieldName)
        throws IOException {

    if (!srcPdfFileDir.isDirectory()) {
        logger.info("srcPdfFileDir is not a Directory: " + srcPdfFileDir.getAbsolutePath());
        return;
    }

    File listFiles[] = srcPdfFileDir.listFiles();

    if (listFiles.length == 0) {
        logger.info("srcPdfFileDir has not file. " + srcPdfFileDir.getAbsolutePath());
        return;
    }

    FileUtils.forceMkdir(descPdfFileDir);

    // ??

    // 
    long prefixDiskFreeSize = descPdfFileDir.getFreeSpace();
    // ??
    long srcSize = FileUtils.sizeOf(srcPdfFileDir);

    logger.info(descPdfFileDir.getAbsolutePath() + " " + prefixDiskFreeSize / 1000000.00
            + " M");
    logger.info(srcPdfFileDir.getAbsolutePath() + " ? :" + srcSize / 1000000.00 + " M");

    if (prefixDiskFreeSize < srcSize) {

        logger.info(FilenameUtils.getPrefix(descPdfFileDir.getAbsolutePath()) + " has not enoght disk size .");

        return;
    }

    // logger.info(descPdfFileDir.getAbsolutePath());

    for (File f : listFiles) {
        String fileName = f.getName();
        String extensiion = FilenameUtils.getExtension(fileName).toLowerCase();

        PdfReader reader = null;

        PdfStamper stamper = null;

        if (f.isFile()) {
            if (extensiion.equals("pdf")) {

                reader = getPdfReader(f);

                File fileDesc = new File(descPdfFileDir.getAbsolutePath() + File.separator + fileName);

                try {
                    stamper = new PdfStamper(reader, FileUtils.openOutputStream(fileDesc));

                    /**
                     * ???
                     * **/
                    // reader.removeFields();

                    /**
                     * ??? javaScript ?
                     * **/
                    AcroFields form = stamper.getAcroFields();
                    form.removeField(removeFieldName);

                    stamper.close();
                    reader.close();

                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } else
                continue;

        } // end if f.isFile

        else if (f.isDirectory()) {
            removeFieldJavaScript(f, new File(descPdfFileDir.getAbsolutePath() + File.separator + fileName),
                    removeFieldName);
        } // end if f.isDirectory
        else
            continue;

    } // end for

    logger.info("finished !");
}

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 {//from   w  w w.  ja va  2s.co m

        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);
    }
}