We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Form Feild Disable

Hi,

I am trying to disable FormFields in the document. I tried doing it by using setEnabled(false)
This is not giving the expected results.

Please find attached document and source.

public class Test
{
    public static void main(String args[])
    {
        try
        {
            Document doc = new Document();
            String[] items = new String[] { "-- Select your favorite footwear --", "Sneakers", "Oxfords",
                    "Flip-flops", "Other", "I prefer to be barefoot" };

            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.insertComboBox("combo1", items, 0);
            builder.insertBreak(BreakType.LINE_BREAK);
            builder.insertTextInput("text1", TextFormFieldType.REGULAR_TEXT, "", "This is in text input box",
            0);
            builder.insertBreak(BreakType.LINE_BREAK);
            builder.writeln("Protected text");
            builder
            .writeln("Supplier shall maintain adequate and accurate records pertaining to any Goods purchased by");
            builder.insertTextInput("text2", TextFormFieldType.REGULAR_TEXT, "", "More Text", 0);

            FormFieldCollection formFieldCollection = doc.getRange().getFormFields();

            for (Node formField : formFieldCollection)
            {
                System.out.println("formField.getNodeType() : " + formField.getNodeType());

                if (formField.getNodeType() == NodeType.FORM_FIELD)
                {
                    FormField ff = (FormField)formField;
                    System.out.println("ff " + ff.getName());
                    ff.setEnabled(false);
                }
            }

            doc.save("C:/test.doc");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Thanks in advance.

Regards,
Tarul

Hi Tarul,
Thanks for your inquiry. As I can see fields are disabled in Ms Word, please see the attached screenshot.
Also, I suppose this option has effect only is document is protected with AllowOnlyFormFields protection type. Please see the following code:

doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS);
doc.save("C:\\Temp\\out.doc");

Best regards,