Filling XFA form

Hi,


I’m looking to fill a XFA based PDF that was created with LiveCycle Designer. I’ve done some searching and can find many .NET examples, but I need to do this in Java. Please provide a pointer to some sample code that does this.

thanks

Should also note the XML data will be coming from a String and not a file.

Hi Steve,


Thanks for contacting support.

Aspose.Pdf for .NET supports the feature to fill form fields in Dynamic XFA form or in normal AcroForm. However when filling the form fields, you need to provide the correct field name when filling data inside it. Please visit the following link for further information on how to Fill Form Field in a PDF Document

Aspose.Pdf for .NET also supports the feature to convert XFA form to normal AcroForm. For further details, you may check Convert Dynamic XFA form to Standard AcroForm.

In case you encounter any issue or you have any further query, please feel free to contact.

Hi Steve,

You may also try using the following code to fill data inside XFA form fields.

C#

FileStream fs = new FileStream(@"D:\pdftest\OrderReceived.pdf", FileMode.Open, FileAccess.ReadWrite);

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(fs);

// Fill out the Person 1 (Sol-licitant) form fields
pdfDocument.Form.XFA["lblStorePhone"] = 0092;

pdfDocument.Save();

fs.Close();

As originally mentioned, I need to fill the XFA from Java and not C#. So this example does not help me.


Please provide a JAVA example.

thanks

Hi Steve,

Please accept my apologies for any confusion caused. The following code snippet can be used to fill data inside a PDF form. You can download the latest Aspose.Pdf for Java 4.2.1 and evaluate our component. For further information, you can check Fill Form Field in a PDF Document.

Java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.aspose.pdf.Document;

public class FillPdfForm {
    public static void main(String[] args) {
        try {
            // Load the PDF document
            FileInputStream fs = new FileInputStream("D:/pdftest/OrderReceived.pdf");
            Document pdfDocument = new Document(fs);

            // Fill out the form field
            pdfDocument.getForm().get_Item("lblStorePhone").setValue("0092");

            // Save the filled out document
            FileOutputStream fos = new FileOutputStream("c:/pdftest/FilledForm.pdf");
            pdfDocument.save(fos);

            // Close the input and output streams
            fs.close();
            fos.close();

            System.out.println("Form filled and saved successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Thanks for the Java sample.


However, we don’t want to fill individual fields but the entire form. Looking around we found the importXml method. Will this meet our needs? If so, please provide an example.

thanks

Hi Steve,

In order to fill the information inside all the fields, you may consider traversing all the fields and fill the data inside them. Besides this, if the data to be filled inside PDF form is present in XFDF file, you may consider using the following code to import the data inside the Form.

In case the data is saved in XML file, you may consider using form.importXml(..) method.

[Java]

// Assign input and output PDF documents
com.aspose.pdf.facades.Form form = new com.aspose.pdf.facades.Form("c:/pdftest/InputTemplate.pdf", "c:/pdftest/InputTemplate_Form_ImportXml.pdf");

// Select a xml file with contents of the form
String xfdfFile = "c:/pdftest/InputXFDF.xfdf";

// Import the values from the XML to the output PDF form
form.importXfdf(new java.io.FileInputStream(xfdfFile));

// save the output PDF document and release the object
form.save();

[quote user=“codewarior”]


In case the data is saved in XML fie, you may consider using form.importXml(…) method.

[/quote]

The importXml(…) method indicates it requires an object of type p when calling it. How do we get this properly created/filled with the XML data ?


Hi Steve,


I have also observed that arguments for importXml(…) method are not properly defined. For the sake of correction, I have logged this
issue as
PDFNEWNET-33758 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.