How to add form field in a pdf

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for Java (v8)  /  How to add form field in a pdf

DynamicPDF Merger for Java (v8) 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
 Oct 20 2021 2:02 AM
Hi team,
i created form fields as follows:
TextField textField = new TextField(col.getHeader(), 0, yCoord, 150, 18);
textField.setReadOnly(true);
page.getElements().add(textField);
but i cant seem to populate as per the thread says by:
templateDoc.getForm().getFields().getFormField(col.getHeader()).setValue("-");
Please advise thanks.
 Oct 20 2021 4:25 PM
Posted by a ceTe Software moderator
Hi,

If you are merging a PDF and using a full name or form field, then the code sample you posted should work for you.

Are you getting any error? If so, please send over following information to  support@dynamicpdf.com so we can look into it further.

1.        Code sample which uses static data to recreate the error/behavior.
2.        Source PDF (if one is used).
3.        Full error message along with stack trace.
4.        Exact version and build number of DynamicPDF jar file used in your application.
5.        More information on the problem you are facing on your end.

Thanks,
ceTe Software Support Team
 Oct 20 2021 4:43 PM
Hi team
I am not at work right now so i can only give you a high level description so that i could continue work tomorrow because i am stuck here.
I am inserting text fields the same way hut text fields dont have setvalue but only set default value so all text fields have the same value.
Moreover i want to insert a text field after a paragraph in a prepared pdf ends. How do i get the y coordinate for that page? Is it get bottom margin? Please advise. Thanks
 Oct 21 2021 9:55 AM
Posted by a ceTe Software moderator
Hi,

If you are adding form fields from scratch, then there is an option to set a default value. Also, it looks like you are adding form fields with the same name and hence you are seeing all fields set with the same value.

You may want to add form fields (TextFields) with unique names and set values. This will have respective values set to the form fields in the output.

Regarding getting Y-position, if you are working with existing PDF then you can only get the location of form fields in the PDF. Please refer to the documentation on getting form field locations here

If you are creating a PDF from scratch, then you can keep track of the Y position based on the page elements Y position and height.

Page dimensions can be retrieved using Page class properties (getDimensions). Please refer to the documentation on Page class members here

Thanks,
ceTe Software Support Team
 Oct 21 2021 10:09 AM
Hi Team,
I am looping and adding text fields with unique names.
        TextField textField = new TextField(col.getHeader(), xCoord, yCoord, 150, 30);
As far as the default value goes it inserts last value in all text fields.
The text fields are not shown in the following:
getForm().getFields();
The form fields have setValue() but not the text fields.
Please advise
 Oct 21 2021 12:56 PM
Posted by a ceTe Software moderator
Hi,

Since you are adding new form fields, I'd recommend filling them using the DefaultValue property before adding them to Page.  Make sure you have unique names for text fields, and this will avoid the same value in all fields.

If you are adding new form fields to a PDF, then there is no option to get the list of form fields in the PDF.

Thanks,
ceTe Software Support Team

All times are US Eastern Standard time. The time now is 3:17 PM.