Create editable form in aspose-pdf-java

Hi,

I am trying to create a simple editable pdf form using aspose-pdf-java. Below is the code snippet i am using. But I am not getting my expected outcome.

Document pdfDoc = new Document();
Page page = pdfDoc.getPages().add().

page.getParagraphs().add(new TextFragment(“First Name:”);

TextBoxField textBoxField = new TextBoxField(pdfDocument.getPages().get_Item(1), new Rectangle(100, 200, 300, 300);

textBoxField .setPartialName(“Partial Name);
textBoxField.setValue(”);

pdfDoc .getForm().add(textBoxField, 1);

page.getParagraphs().add(new TextFragment(“Last Name:”));

TextBoxField textBoxField1= new TextBoxField(pdfDocument.getPages().get_Item(1), new Rectangle(100, 200, 300, 300);

textBoxField1.setPartialName(“Partial Name);
textBoxField1.setValue(”);

pdfDoc .getForm().add(textBoxField1, 1);

pdfDoc.save(“simple_form.pdf”);

My Expected Output:

First Name:
--------(Editable field)--------------------------------
Last Name:
--------(Editable Field)-------------------------------

Note: I am using only evaluation version only.

Appreciate your earlier response.

@hariselvam1991

We tried to obtain your expected output using the following code snippet but we faced an exception i.e. java.lang.StringIndexOutOfBoundsException: String index out of range: .

Document pdfDocument = new Document();
Page page = pdfDocument.getPages().add();

Table table = new Table();
table.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);
table.setColumnWidths("100");
page.getParagraphs().add(table);

Row row1 = table.getRows().add();
Cell cell1 = row1.getCells().add("First Name:");
TextBoxField Firstnamefield = new TextBoxField();
Firstnamefield.setPartialName("First_Name");
Firstnamefield.setValue("");
Firstnamefield.setWidth(100);
Firstnamefield.setHeight(15);
TextBoxField Lastnamefield = new TextBoxField();
Firstnamefield.setPartialName("Last_Name");
Firstnamefield.setValue("");
Firstnamefield.setWidth(100);
Firstnamefield.setHeight(15);
Row row2 = table.getRows().add();
Cell cell2 = row2.getCells().add();
cell2.getParagraphs().add(Firstnamefield);
Row row3 = table.getRows().add();
Cell cell3 = row3.getCells().add("Last Name:");
Row row4 = table.getRows().add();
Cell cell4 = row4.getCells().add();
cell4.getParagraphs().add(Lastnamefield);
pdfDocument.save(dataDir + "simple_form.pdf");

Therefore, we have logged an issue as PDFJAVA-40059 in our issue tracking system for the sake of further investigation and its correction. We will look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

Thanks for the response Asad. Ok. Hope this post will be update once the issue is resolved. Any idea on ETA to fix the same ?

@hariselvam1991

The ticket has recently been logged in our issue tracking system and we will surely investigate it on a first come first serve basis. As soon as we make some definite progress towards resolution of it, we will update you within this forum thread. Please be patient and spare us little time.

We apologize for the inconvenience.

@hariselvam1991

We have reviewed the ticket and made some modifications to the code snippet which was shared by us in our previous response. Please use below code snippet in order to achieve your desired output:

Document pdfDocument = new Document();
Page page = pdfDocument.getPages().add();

Table table = new Table();
table.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);
table.setColumnWidths("100");
page.getParagraphs().add(table);

Row row1 = table.getRows().add();
Cell cell1 = row1.getCells().add("First Name:");

TextBoxField firstnamefield = new TextBoxField();
firstnamefield.setPartialName("First_Name");   

firstnamefield.setWidth(100);
firstnamefield.setHeight(15);

Row row2 = table.getRows().add();
Cell cell2 = row2.getCells().add();    
cell2.getParagraphs().add(firstnamefield);

TextBoxField lastnamefield = new TextBoxField();
lastnamefield.setPartialName("Last_Name");
lastnamefield.setWidth(100);
lastnamefield.setHeight(15);

Row row3 = table.getRows().add();
Cell cell3 = row3.getCells().add("Last Name:");
Row row4 = table.getRows().add();
Cell cell4 = row4.getCells().add();
cell4.getParagraphs().add(lastnamefield);
pdfDocument.save(dataDir + "simple_form.pdf");

simple_form.pdf (4.5 KB)

Thanks @asad.ali . This works. !

1 Like

@hariselvam1991

It is good to know that suggested code snippet worked for you. Please keep using our API and let us know in case you need further assistance.

The issues you have found earlier (filed as PDFJAVA-40059) have been fixed in Aspose.PDF for Java 21.1.