Example usage for com.lowagie.text.pdf PdfArray getAsString

List of usage examples for com.lowagie.text.pdf PdfArray getAsString

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfArray getAsString.

Prototype

public PdfString getAsString(int idx) 

Source Link

Document

Returns a PdfObject as a PdfString, resolving indirect references.

Usage

From source file:questions.forms.FillDynamicXfa.java

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