Not able to flatten form and radio button and place form dynamically

Hi Team,

I am having a PDF which have editable and non-editable part.
My requirement is that I should be able to make the editable part non editable depending on a configuration.
I have added controls like CheckboxField, RadioButtonOptionField, TextBoxField.
These controls I have added to a table which suits my requirement.

Is it necessary that I have to add the editable control to a Form?
If I add it to form the control shows up in two place. One in the table where I have placed according to the pdf design and also on the first page as I am adding to pdf.getForm().add(field, 1)

If I don’t place the fields in the form I would not be able to flatten the PDF to make it non editable.
I am also not able to add the table to the Form as it allows only field item.
As per requirement I would not be able to place the form in any static location like Rectangle as my data remains dynamic and can grow to any extend.

Please let me know if there is a way to make the form visibility false so that non of my fields are repeated twice, also please let me know if there any work around.

Code:
static void Create() throws FileNotFoundException
{
Booleam isPdfEditable = false;
pdfDoc = new Document();
pdfPageItem = pdfDoc.getPages().add();
pdfPageItem.getParagraphs().add(NonEditableContent()); — > Implementation not provided in code
pdfPageItem.getParagraphs().add(GetTable());
if(!isPdfEditable)
{
pdfDoc.flatten();
pdfDoc.getForm().flatten();
Field[] fields = pdfDoc.getForm().getFields();
for(Field field : fields)
{
field.flatten();
}
}
pdfDoc.save(“Sample.pdf”);

}

private static Table GetTable()
{
Table questionTable = new Table();
questionTable.setMargin(pdfDoc.getPageInfo().getMargin());
questionTable.setColumnWidths(“20 500”);
RadioButtonField rf = new RadioButtonField(pdfDoc);
rf = new RadioButtonField(pdfPageItem);
rf.setPartialName(“radio”);
Row r2 = questionTable.getRows().add();
Cell c2 = r2.getCells().add();
Cell c3 = r2.getCells().add();


c2.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);
c2.setWordWrapped(true);
c3.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Bottom);
c3.setWordWrapped(true);


RadioButtonOptionField opt1 = new RadioButtonOptionField();
opt1.setOptionName(“Name”);
opt1.setWidth(10);
opt1.setHeight(10);
rf.add(opt1);
pdfDoc.getForm().add(opt1, 1);

RadioButtonOptionField opt2 = new RadioButtonOptionField();
opt2.setOptionName(“Name”);
opt2.setWidth(10);
opt2.setHeight(10);
rf.add(opt2);
pdfDoc.getForm().add(opt2, 1);

c2.getParagraphs().add(opt1);
c3.getParagraphs().add(opt2);

CheckboxField chk1= new CheckboxField(pdfDoc);
chk1.setPartialName(“ch”);
chk1.setWidth(10);
chk1.setHeight(10);

Border border = new Border(chk1);
border.setWidth(2);
border.setStyle(BorderStyle.Solid);
chk1.setBorder(border);
chk1.getCharacteristics().setBorder(java.awt.Color.BLACK);
pdfDoc.getForm().add(chk1, 1);

TextBoxField textBoxField1 = new TextBoxField(pdfDoc);
textBoxField1.setPartialName(“tb”);
textBoxField1.setHeight(30);
textBoxField1.setWidth(500);
textBoxField1.setInLineParagraph(true);
textBoxField1.setMultiline(true);
pdfDoc.getForm().add(textBoxField1, 0);

Row r3 = questionTable.getRows().add();
Cell c4 = r3.getCells().add();
Cell c5 = r3.getCells().add();

c4.getParagraphs().add(chk1);
c5.getParagraphs().add(textBoxField1);

return questionTable;

}

Team,

Can you please reply back on this post.

Thanks,
Leena Robert

Hi Leena,


Thanks for contacting support.

I have tried replicating the issue and as per my observations, both RadioButtonFields, a checkbox and text field are appearing. However when setting the value for isPdfEditable to true, the form fields are not flattening. Please confirm you are also facing similar issue as from above description, I am unable to clearly understand the problem. Please share some further details, so that we can further look into this matter.

For your reference, I have also attached the output generated over my end. We are really sorry for this inconvenience.

Hi Shahbaz,

As you told the fields will get flattened when isPDFEditable is set to false.
But the issue here is when i add these fields to the form, the fields shows up in two places, one in the table where we have already added and other to the left bottom of the first page.
I don’t want to have the fields repeated in two places and only wants it in the table where I have placed it and if i don’t add the fields to the form I am not able to flatten those when isPDFEditable is set to false.

Thanks,
Leena Robert

Hi Leena,


Thanks for sharing the details.

I have logged above stated problem as PDFJAVA-36482 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

@LeenaRobert

Thanks for your patience.

Please use following modified method GetTable(), in order to obtain correct results.

private static Table GetTable()
{
 Table questionTable = new Table();
 questionTable.setMargin(pdfDoc.getPageInfo().getMargin());
 questionTable.setColumnWidths("20 500");
 RadioButtonField rf = new RadioButtonField(pdfDoc);
 rf = new RadioButtonField(pdfPageItem);
 rf.setPartialName("radio");
 Row r2 = questionTable.getRows().add();
 Cell c2 = r2.getCells().add();
 Cell c3 = r2.getCells().add();
          
 c2.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);
 c2.setWordWrapped(true);
 c3.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Bottom);
 c3.setWordWrapped(true);
            
 RadioButtonOptionField opt1 = new RadioButtonOptionField();
 opt1.setOptionName("Name");
 opt1.setWidth(10);
 opt1.setHeight(10);        
 rf.add(opt1);
 //pdfDoc.getForm().add(opt1, 1); - this line duplicates the form
         
 RadioButtonOptionField opt2 = new RadioButtonOptionField();
 opt2.setOptionName("Name");
 opt2.setWidth(10);
 opt2.setHeight(10);        
 rf.add(opt2);
 // pdfDoc.getForm().add(opt2, 1); - this line duplicates the form
         
 c2.getParagraphs().add(opt1);
 c3.getParagraphs().add(opt2);

 CheckboxField chk1=  new CheckboxField(pdfDoc);
 chk1.setPartialName("ch");
 chk1.setWidth(10);
 chk1.setHeight(10);
            
  Border border = new Border(chk1);
  border.setWidth(2);
  border.setStyle(BorderStyle.Solid);
  chk1.setBorder(border);
  chk1.getCharacteristics().setBorder(java.awt.Color.BLACK);
 // pdfDoc.getForm().add(chk1, 1);
  pdfPageItem.getParagraphs().add(chk1);
            
  TextBoxField textBoxField1 = new TextBoxField(pdfDoc);
   textBoxField1.setWidth(500);
   textBoxField1.setHeight(30);
   textBoxField1.setPartialName("tb");
   textBoxField1.setInLineParagraph(true);
   textBoxField1.setMultiline(true);
   // pdfDoc.getForm().add(textBoxField1, 1);
   pdfPageItem.getParagraphs().add(textBoxField1);
            
   Row r3 = questionTable.getRows().add();
   Cell c4 = r3.getCells().add();
   Cell c5 = r3.getCells().add();
        
    c4.getParagraphs().add(chk1);
    c5.getParagraphs().add(textBoxField1); 
        
    return questionTable;              
}

In case of any further assistance, please feel free to contact us.