How to add form field in a pdf

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for Java (v8.x)  /  Display Thread

DynamicPDF Merger for Java (v8.x) Forum

 Jul 26 2019 5:42 AM
Hi,
I want to set the form field in a pdf so I can use java code to set it's value.But I dont know how to set the form field and it's name.Please help.

Oliver
 Jul 26 2019 9:12 AM
Posted by a ceTe Software moderator
Hello Oliver,

You can add acro form fields to the PDF using DynamicPDF Generator/Merger for Java product. If you are creating PDF from scratch then below is the code sample. Also you can refer to the documentation on interactive forms here.

        Document document = new Document();
        Page page = new Page(PageSize.LETTER,PageOrientation.LANDSCAPE);
        document.getPages().add(page);
       
        TextField textField = new TextField( "Text Field Name", 50, 75, 150, 30 );
        textField.setBorderStyle(BorderStyle.SOLID);
        textField.setDefaultValue("DynamicPDF");
        textField.setMultiLine(true);
          
        page.getElements().add(textField);
        document.draw("C:/Temp/MyDocument.pdf");

If you are looking to add form field to the existing PDF then below is the code sample.

            PdfDocument pdf=new PdfDocument("File path for input PDF");
            MergeDocument mergeDoc=new MergeDocument(pdf);
            //Access the page using index.
            Page page=mergeDoc.getPages().getPage(0);
            //Add the form field to the page.
            TextField textField = new TextField( "txt_1", 50, 120, 150, 30 );
            textField.setBorderStyle(BorderStyle.SOLID);
            page.getElements().add(textField);
            mergeDoc.draw("C:/Temp/MyOutput.pdf");

You can fill the form fields dynamically using DynamicPDF Merger product. You will need to access the form filed using full name or index and fill them with required text. Below is the code sample. The documentation on filling the form can be found here.

            PdfDocument pdf=new PdfDocument("File path for a input PDF");
            MergeDocument mergeDoc=new MergeDocument(pdf);
            mergeDoc.getForm().getFields().getFormField("Text Field Name").setValue("My Value");
            mergeDoc.draw("C:/Temp/MyOutput.pdf");

Thanks,
ceTe Software Support Team

All times are US Eastern Standard time. The time now is 7:22 PM.