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

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

Introduction

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

Prototype

PdfName XFA

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

Click Source Link

Document

A name

Usage

From source file:questions.forms.FillDynamicXfa.java

public static void main(String[] args) {
    try {/*w  w  w.  j av  a2s . 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.ReadXfa.java

public static void main(String[] args) {
    try {/*from  w  ww . jav a2s . c o  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 {//from ww w.ja v  a  2 s  . co 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();
    }
}