AndroidDev
AndroidDev

Reputation: 2647

Android fill PDF form

I have .pdf file and multiple forms are there. I want to open my .pdf file, fill the forms and save it from Android development.

Is there any API for Android Rendering. I found iText but I just manage to create new pdf and than i can fill form. means which .pdf file i created that will be filled out. I need to fill my form in my own .pdf.

Thanks in Advance...any help will be appreciated...

Upvotes: 13

Views: 7913

Answers (2)

steipete
steipete

Reputation: 7641

The native PDF support on current Android platforms (including Android P) doesn't expose any controls for filling forms. 3rd-party PDF SDKs such as PSPDFKit fill this gap and allow programmatic PDF form filling:

List<FormField> formFields = document.getFormProvider().getFormFields();
for (FormField formField : formFields) {
    if (formField.getType() == FormType.TEXT) {
        TextFormElement textFormElement = (TextFormElement) formField.getFormElement();
        textFormElement.setText("Test " + textFormElement.getName());
    } else if (formField.getType() == FormType.CHECKBOX) {
        CheckBoxFormElement checkBoxFormElement = (CheckBoxFormElement)formField.getFormElement();
        checkBoxFormElement.toggleSelection();
    }
}

(If you click on above link there's also a Kotlin PDF form filling example.)

Note that most SDKs on the market focus on PDF AcroForms, and not the XFA specification, which has been deprecated in the PDF 2.0 spec.

Upvotes: 1

Robbie
Robbie

Reputation: 51

DynamicPDF Merger for Java allows you to do just that. You can take an existing PDF document, fill out the form field values and then output that newly filled PDF.

There was a recent blog post on dynamicpdf.com on setting up DynamicPDF for Java in an Android application and creating a simple PDF from it, http://www.dynamicpdf.com/Blog/post/2012/06/15/Generating-PDFs-Dynamically-on-Android.aspx.

You can easily take that example one step further and use it to accomplish your task of form filling. The following (untested) code is an example of what it would take to form fill an existing PDF on an Android device using DynamicPDF Merger for Java:

InputStream inputStream = this.getAssets().open("PDFToFill.pdf");
long avail = inputStream.available();
byte[] samplePDF = new byte[(int) avail];
inputStream.read(samplePDF , 0, (int) avail);
inputStream.close();
PdfDocument objPDF = new PdfDocument(samplePDF);    

MergeDocument document = new MergeDocument(objPDF);
document.getForm().getFields().getFormField("FormField1").setValue("My Text");
document.draw("[PhysicalPath]/FilledPDF.pdf");

Upvotes: 4

Related Questions