@Test
public void fdfTest() throws Exception {

    String acroform_pdf = "./src/test/resources/com/itextpdf/text/pdf/AcroFieldsTest/acroform.pdf";
    String barcode_jpg = "./src/test/resources/com/itextpdf/text/pdf/AcroFieldsTest/barcode.jpg";
    String signature_pdf = "./src/test/resources/com/itextpdf/text/pdf/AcroFieldsTest/signature.pdf";
    String outFdf = outFolder + "acroform_fields.fdf";
    FileOutputStream fos = new FileOutputStream(outFdf);
    FdfWriter fdfWriter = new FdfWriter(fos);
    fdfWriter.setFile(new File(acroform_pdf).getAbsolutePath());
    fdfWriter.setFieldAsString("FirstName", "Alexander");
    fdfWriter.setFieldAsString("LastName", "Chingarev");
    //Add signature from external PDF.
    PdfReader signatureReader = new PdfReader(signature_pdf);
    fdfWriter.setFieldAsTemplate("Signature", fdfWriter.getImportedPage(signatureReader, 1));
    //Add barcode image
    Image img = Image.getInstance(barcode_jpg);
    fdfWriter.setFieldAsImage("Barcode", img);
    fdfWriter.write();
    //Close signature PDF reader.
    signatureReader.close();
    FdfReader fdfReader = new FdfReader(outFdf);
    HashMap<String, PdfDictionary> fields = fdfReader.getFields();
    PdfDictionary barcode = fields.get("Barcode");
    PdfStream n = barcode.getAsDict(PdfName.AP).getAsStream(PdfName.N);
    Assert.assertNotNull(n);
    byte[] b = FdfReader.getStreamBytes((PRStream) n);
    Assert.assertEquals(32, b.length);
    PdfStream img0 = n.getAsDict(PdfName.RESOURCES).getAsDict(PdfName.XOBJECT).getAsStream(new PdfName("img0"));
    Assert.assertNotNull(img0);
    PdfDictionary signature = fields.get("Signature");
    n = signature.getAsDict(PdfName.AP).getAsStream(PdfName.N);
    Assert.assertNotNull(n);
    b = FdfReader.getStreamBytes((PRStream) n);
    Assert.assertEquals(24410, b.length);
    fdfReader.close();
}