Example usage for com.lowagie.text.pdf PdfName ACROFORM

List of usage examples for com.lowagie.text.pdf PdfName ACROFORM

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfName ACROFORM.

Prototype

PdfName ACROFORM

To view the source code for com.lowagie.text.pdf PdfName ACROFORM.

Click Source Link

Document

A name

Usage

From source file:ispyb.common.util.PDFFormFiller.java

License:Open Source License

/**
 * Render and finalize the PDF form filled with data
 * //from w w  w . j av  a 2s. co m
 * @throws Exception
 */
public void render() throws Exception {
    // This is mandatory not to have the "Expected a dict object"
    // message when the result ouput is openned!!!
    PdfDictionary pdfDictionary = (PdfDictionary) PdfReader
            .getPdfObject(this.reader.getCatalog().get(PdfName.ACROFORM));
    pdfDictionary.remove(new PdfName("XFA"));
    // ///////////////////////////////////////////////////////////

    if (this.stamper != null) {
        this.stamper.setFormFlattening(true);
        this.stamper.close();
        this.stamper = null;
    }
    if (this.reader != null) {
        this.reader.close();
        this.reader = null;
    }
    if (this.writer != null) {
        if (!this.writer.isCloseStream())
            this.writer.close();
        this.writer = null;
    }
    this.formFields = null;
}

From source file:org.opensignature.opensignpdf.PDFSigner.java

License:Open Source License

/**
 * // ww  w .  ja v  a 2 s. com
 * @param pdfFile
 * @return
 * @throws IOException
 * @throws DocumentException
 * @throws FileNotFoundException
 */
private PdfReader createPDFReader(File pdfFile) throws IOException, DocumentException, FileNotFoundException {

    logger.info("[createPDFReader.in]:: " + Arrays.asList(new Object[] { pdfFile }));

    PdfReader reader;

    if ("true".equals(openOfficeSelected)) {
        String fileName = pdfFile.getPath();
        String tempFileName = fileName + ".temp";
        PdfReader documentPDF = new PdfReader(fileName);

        PdfStamperOSP stamperTemp = new PdfStamperOSP(documentPDF, new FileOutputStream(tempFileName));
        AcroFields af = stamperTemp.getAcroFields();
        af.setGenerateAppearances(true);
        PdfDictionary acro = (PdfDictionary) PdfReader
                .getPdfObject(documentPDF.getCatalog().get(PdfName.ACROFORM));
        acro.remove(PdfName.DR);
        HashMap fields = af.getFields();
        String key;
        for (Iterator it = fields.keySet().iterator(); it.hasNext();) {
            key = (String) it.next();
            int a = af.getFieldType(key);
            if (a == 4) {
                ArrayList widgets = af.getFieldItem(key).widgets;
                PdfDictionary widget = (PdfDictionary) widgets.get(0);
                widget.put(PdfName.FT, new PdfName("Sig"));
                widget.remove(PdfName.V);
                widget.remove(PdfName.DV);
                widget.remove(PdfName.TU);
                widget.remove(PdfName.FF);
                widget.remove(PdfName.DA);
                widget.remove(PdfName.DR);
                widget.remove(PdfName.AP);
            }
        }

        stamperTemp.close();
        documentPDF.close();
        reader = new PdfReader(pdfFile.getPath() + ".temp");

    } else {
        reader = new PdfReader(pdfFile.getPath());

    }

    logger.info("[createPDFReader.retorna]:: ");
    return reader;

}

From source file:questions.forms.FillDynamicXfa.java

public static void main(String[] args) {
    try {//from  w  w w .  jav a 2  s . c  o m
        PdfReader reader = new PdfReader(RESOURCE_PDF);
        File file = new File(RESOURCE_DATA);
        byte[] data = new byte[(int) file.length()];
        FileInputStream is = new FileInputStream(file);
        int offset = 0;
        int numRead = 0;
        int datalength = data.length;
        while (offset < datalength && (numRead = is.read(data, offset, datalength - offset)) >= 0) {
            offset += numRead;
        }
        PdfDictionary root = reader.getCatalog();
        PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
        PdfArray xfa = acroform.getAsArray(PdfName.XFA);
        for (int i = 0; i < xfa.size(); i += 2) {
            if ("datasets".equals(xfa.getAsString(i).toString())) {
                PRStream s = (PRStream) xfa.getAsStream(i + 1);
                s.setData(data);
            }
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.GetTextFields.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try {//from   w w w  . ja  v a 2s .c om
        PrintStream out = new PrintStream(new File(RESULT));

        PdfLister lister = new PdfLister(out);

        PdfReader reader = new PdfReader(RESOURCE);

        PdfDictionary root = reader.getCatalog();
        PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);

        out.println("These are the form's font dictionaries:");
        PdfDictionary fonts = acroform.getAsDict(PdfName.DR).getAsDict(PdfName.FONT);
        for (PdfName key : (Set<PdfName>) fonts.getKeys()) {
            lister.listDict((PdfDictionary) PdfReader.getPdfObject(fonts.get(key)));
        }
        out.println("--------------");

        out.println("This is the default appearance for the complete form:");
        lister.listAnyObject(PdfReader.getPdfObject(acroform.get(PdfName.DA)));
        out.println("--------------");

        AcroFields form = reader.getAcroFields();
        Map<String, Item> fields = form.getFields();
        Item item;
        for (String name : fields.keySet()) {
            out.println(name);
            if (form.getFieldType(name) == AcroFields.FIELD_TYPE_TEXT) {
                item = form.getFieldItem(name);
                PdfDictionary dict = (PdfDictionary) item.getMerged(0);

                out.println("This is the field's font dictionary:");
                fonts = dict.getAsDict(PdfName.DR).getAsDict(PdfName.FONT);
                for (PdfName key : (Set<PdfName>) fonts.getKeys()) {
                    lister.listDict((PdfDictionary) PdfReader.getPdfObject(fonts.get(key)));
                }
                out.println("---");
                out.println("This is the field's default appearance");
                lister.listAnyObject(PdfReader.getPdfObject(dict.get(PdfName.DA)));
            } else {
                out.println("NOT A TEXT FIELD!");
            }
            out.println("--------------");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.ReadXfa.java

public static void main(String[] args) {
    try {/*from   w w  w  .  j av  a2s . co m*/
        PdfReader reader = new PdfReader(RESOURCE);
        FileOutputStream os = new FileOutputStream(RESULT);
        PdfDictionary root = reader.getCatalog();
        PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
        PdfArray xfa = acroform.getAsArray(PdfName.XFA);
        for (int i = 0; i < xfa.size(); i += 2) {
            System.out.println("Reading: " + xfa.getAsString(i));
            PRStream s = (PRStream) xfa.getAsStream(i + 1);
            os.write(PdfReader.getStreamBytes(s));
        }
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.RemoveXfa.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try {/*w w  w . ja  v a 2  s .  c  o  m*/
        PdfReader reader = new PdfReader(RESOURCE);
        PdfDictionary root = reader.getCatalog();
        PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
        acroform.remove(PdfName.XFA);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        AcroFields form = stamper.getAcroFields();
        Map<String, Item> fields = form.getFields();
        for (String field : fields.keySet()) {
            System.out.println(field);
            form.setField(field, "value");
        }
        stamper.partialFormFlattening("topmostSubform[0].Page1[0].SN_NUMBER[0]");
        stamper.setFormFlattening(true);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}