Search in sources :

Example 1 with FDFDocument

use of com.tom_roush.pdfbox.pdmodel.fdf.FDFDocument in project PdfBox-Android by TomRoush.

the class PDAcroForm method exportFDF.

/**
 * This will export all FDF form data.
 *
 * @return An FDF document used to export the document.
 * @throws IOException If there is an error when exporting the document.
 */
public FDFDocument exportFDF() throws IOException {
    FDFDocument fdf = new FDFDocument();
    FDFCatalog catalog = fdf.getCatalog();
    FDFDictionary fdfDict = new FDFDictionary();
    catalog.setFDF(fdfDict);
    List<FDFField> fdfFields = new ArrayList<FDFField>();
    List<PDField> fields = getFields();
    for (PDField field : fields) {
        fdfFields.add(field.exportFDF());
    }
    fdfDict.setID(document.getDocument().getDocumentID());
    if (!fdfFields.isEmpty()) {
        fdfDict.setFields(fdfFields);
    }
    return fdf;
}
Also used : ArrayList(java.util.ArrayList) COSArrayList(com.tom_roush.pdfbox.pdmodel.common.COSArrayList) FDFField(com.tom_roush.pdfbox.pdmodel.fdf.FDFField) FDFDocument(com.tom_roush.pdfbox.pdmodel.fdf.FDFDocument) FDFCatalog(com.tom_roush.pdfbox.pdmodel.fdf.FDFCatalog) FDFDictionary(com.tom_roush.pdfbox.pdmodel.fdf.FDFDictionary)

Example 2 with FDFDocument

use of com.tom_roush.pdfbox.pdmodel.fdf.FDFDocument in project PdfBox-Android by TomRoush.

the class TestFDF method checkFields.

private void checkFields(String name) throws IOException, URISyntaxException {
    FDFDocument fdf = FDFDocument.load(new File(TestFDF.class.getResource(name).toURI()));
    fdf.saveXFDF(new PrintWriter(new ByteArrayOutputStream()));
    List<FDFField> fields = fdf.getCatalog().getFDF().getFields();
    assertEquals(2, fields.size());
    assertEquals("Field1", fields.get(0).getPartialFieldName());
    assertEquals("Field2", fields.get(1).getPartialFieldName());
    assertEquals("Test1", fields.get(0).getValue());
    assertEquals("Test2", fields.get(1).getValue());
    PDDocument pdf = PDDocument.load(new File(TestFDF.class.getResource("/pdfbox/com/tom_roush/pdfbox/pdfparser/SimpleForm2Fields.pdf").toURI()));
    PDAcroForm acroForm = pdf.getDocumentCatalog().getAcroForm();
    acroForm.importFDF(fdf);
    assertEquals("Test1", acroForm.getField("Field1").getValueAsString());
    assertEquals("Test2", acroForm.getField("Field2").getValueAsString());
    pdf.close();
    fdf.close();
}
Also used : FDFField(com.tom_roush.pdfbox.pdmodel.fdf.FDFField) FDFDocument(com.tom_roush.pdfbox.pdmodel.fdf.FDFDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

FDFDocument (com.tom_roush.pdfbox.pdmodel.fdf.FDFDocument)2 FDFField (com.tom_roush.pdfbox.pdmodel.fdf.FDFField)2 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)1 FDFCatalog (com.tom_roush.pdfbox.pdmodel.fdf.FDFCatalog)1 FDFDictionary (com.tom_roush.pdfbox.pdmodel.fdf.FDFDictionary)1 PDAcroForm (com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1