Example usage for com.lowagie.text.pdf AcroFields FIELD_TYPE_NONE

List of usage examples for com.lowagie.text.pdf AcroFields FIELD_TYPE_NONE

Introduction

In this page you can find the example usage for com.lowagie.text.pdf AcroFields FIELD_TYPE_NONE.

Prototype

int FIELD_TYPE_NONE

To view the source code for com.lowagie.text.pdf AcroFields FIELD_TYPE_NONE.

Click Source Link

Document

A field type invalid or not found.

Usage

From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java

License:Open Source License

/** uses itext to save out form data with any changes user has made */
public void saveFormsData(String file) {
    try {/*from w w w.  j a va  2 s  . c  o m*/
        org.jpedal.objects.acroforms.AcroRenderer formRenderer = dPDF.getCurrentFormRenderer();

        if (formRenderer == null)
            return;

        PdfReader reader = new PdfReader(selectedFile);
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(file));
        AcroFields form = stamp.getAcroFields();

        List names = formRenderer.getComponentNameList();

        /**
         * work through all components writing out values
         */
        for (int i = 0; i < names.size(); i++) {

            String name = (String) names.get(i);
            Component[] comps = formRenderer.getComponentsByName(name);

            int type = form.getFieldType(name);
            String value = "";
            switch (type) {
            case AcroFields.FIELD_TYPE_CHECKBOX:
                if (comps.length == 1) {
                    JCheckBox cb = (JCheckBox) comps[0];
                    value = cb.getName();
                    if (value != null) {
                        int ptr = value.indexOf("-(");
                        if (ptr != -1) {
                            value = value.substring(ptr + 2, value.length() - 1);
                        }
                    }

                    if (value.equals(""))
                        value = "On";

                    if (cb.isSelected())
                        form.setField(name, value);
                    else
                        form.setField(name, "Off");

                } else {
                    for (int j = 0; j < comps.length; j++) {
                        JCheckBox cb = (JCheckBox) comps[j];
                        if (cb.isSelected()) {

                            value = cb.getName();
                            if (value != null) {
                                int ptr = value.indexOf("-(");
                                if (ptr != -1) {
                                    value = value.substring(ptr + 2, value.length() - 1);

                                    //                              name is wrong it should be the piece of field data that needs changing.
                                    //TODO itext
                                    form.setField(name, value);
                                }
                            }

                            break;
                        }
                    }
                }

                break;
            case AcroFields.FIELD_TYPE_COMBO:
                JComboBox combobox = (JComboBox) comps[0];
                value = (String) combobox.getSelectedItem();

                /**
                 * allow for user adding new value to Combo to emulate
                 * Acrobat * String currentText = (String)
                 * combobox.getEditor().getItem();
                 * 
                 * if(!currentText.equals("")) value = currentText;
                 */

                if (value == null)
                    value = "";
                form.setField(name, value);

                break;
            case AcroFields.FIELD_TYPE_LIST:
                JList list = (JList) comps[0];
                value = (String) list.getSelectedValue();
                if (value == null)
                    value = "";
                form.setField(name, value);

                break;
            case AcroFields.FIELD_TYPE_NONE:

                break;
            case AcroFields.FIELD_TYPE_PUSHBUTTON:

                break;
            case AcroFields.FIELD_TYPE_RADIOBUTTON:

                for (int j = 0; j < comps.length; j++) {
                    JRadioButton radioButton = (JRadioButton) comps[j];
                    if (radioButton.isSelected()) {

                        value = radioButton.getName();
                        if (value != null) {
                            int ptr = value.indexOf("-(");
                            if (ptr != -1) {
                                value = value.substring(ptr + 2, value.length() - 1);
                                form.setField(name, value);
                            }
                        }

                        break;
                    }
                }

                break;
            case AcroFields.FIELD_TYPE_SIGNATURE:

                break;

            case AcroFields.FIELD_TYPE_TEXT:
                JTextComponent tc = (JTextComponent) comps[0];
                value = tc.getText();
                form.setField(name, value);

                // ArrayList objArrayList = form.getFieldItem(name).widgets;
                // PdfDictionary dic = (PdfDictionary)objArrayList.get(0);
                // PdfDictionary action
                // =(PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.MK));
                //
                // if (action == null) {
                // PdfDictionary d = new PdfDictionary(PdfName.MK);
                // dic.put(PdfName.MK, d);
                //
                // Color color = tc.getBackground();
                // PdfArray f = new PdfArray(new int[] { color.getRed(),
                // color.getGreen(), color.getBlue() });
                // d.put(PdfName.BG, f);
                // }

                // moderatly useful debug code
                // Item dd = form.getFieldItem(name);
                //               
                // ArrayList objArrayList = dd.widgets;
                // Iterator iter1 = objArrayList.iterator(),iter2;
                // String strName;
                // PdfDictionary objPdfDict = null;
                // PdfName objName = null;
                // PdfObject objObject = null;
                // while(iter1.hasNext())
                // {
                // objPdfDict = (PdfDictionary)iter1.next();
                // System.out.println("PdfDictionary Object: " +
                // objPdfDict.toString());
                // Set objSet = objPdfDict.getKeys();
                // for(iter2 = objSet.iterator(); iter2.hasNext();)
                // {
                // objName = (PdfName)iter2.next();
                // objObject = objPdfDict.get(objName);
                // if(objName.toString().indexOf("MK")!=-1)
                // System.out.println("here");
                // System.out.println("objName: " + objName.toString() + " -
                // objObject:" + objObject.toString() + " - Type: " +
                // objObject.type());
                // if(objObject.isDictionary())
                // {
                // Set objSet2 = ((PdfDictionary)objObject).getKeys();
                // PdfObject objObject2;
                // PdfName objName2;
                // for(Iterator iter3 = objSet2.iterator();
                // iter3.hasNext();)
                // {
                // objName2 = (PdfName)iter3.next();
                // objObject2 = ((PdfDictionary)objObject).get(objName2);
                // System.out.println("objName2: " + objName2.toString() + "
                // -objObject2: " + objObject2.toString() + " - Type: " +
                // objObject2.type());
                // }
                // }
                // }
                // }

                break;
            default:
                break;
            }
        }
        stamp.close();

    } catch (ClassCastException e1) {
        System.out.println("Expected component does not match actual component");
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}