Word based form field extraction and Filling

Hi,

From different forum responses and website pages I can see that form fields inside a MS Word document can be extracted programmaticaly but can I fill them in programmatically as well?

Regards,
Wahaj

Hi Wahaj,

Thanks for your inquiry. The FormField class represents a single form field. FormField is an inline-node and can only be a child of Paragraph. FormField is represented in a document by a special character and positioned as a character within a line of text.

Please use the following code snippet to fill the form filed.

Document doc = new Document(MyDir + "in.doc");
for (int i = 0; i < doc.Range.FormFields.Count; i++)
{
    FormField field = doc.Range.FormFields[i];
    switch (field.Type)
    {
        case FieldType.FieldFormTextInput:
            field.SetTextInputValue("This is text input");
            break;
        case FieldType.FieldFormCheckBox:
            field.Checked = true;
            break;
        case FieldType.FieldFormDropDown:
            field.DropDownSelectedIndex = 0;
            break;
        default:
            // Do nothing here.
            break;
    }
}
doc.Save(MyDir + "out.doc");

Moreover, I suggest you to read following forum/documentation links for your kind reference.
https://forum.aspose.com/t/62154
https://forum.aspose.com/t/50251

https://docs.aspose.com/words/net/working-with-form-fields/
https://reference.aspose.com/words/net/aspose.words.fields/formfield

OK Thanks,

Just checked and couldn’t find how to get X,Y, size of the form field. Is it possible to find?

Also can I find details about the Signature Line if it is present e.g. Identifier, field size (X,Y, size) etc. I believe this is also treated as a form field.

Regards,
Wahaj

Hi Wahaj,

Thanks for your inquiry. Unfortunately, there is no way to change size of these controls (except check box). Please use the FormField.CheckBoxSize property along with FormField.IsCheckBoxExactSize to change the size of check box.

You can change the size of text input and drop down by changing the size of font as shown below:

Document doc = new Document(MyDir + "forms.docx");
for (int i = 0; i < doc.Range.FormFields.Count; i++)
{
    FormField field = doc.Range.FormFields[i];
    switch (field.Type)
    {
        case FieldType.FieldFormTextInput:
            field.SetTextInputValue("This is text input");
            foreach (Run run in field.ParentNode.GetChildNodes(NodeType.Run, true))
            {
                run.Font.Size = 20;
            }
            break;
        case FieldType.FieldFormCheckBox:
            field.Checked = true;
            field.CheckBoxSize = 100.0;
            field.IsCheckBoxExactSize = true;
            break;
        case FieldType.FieldFormDropDown:
            field.DropDownSelectedIndex = 0;
            break;
        default:
            // Do nothing here.
            break;
    }
}
doc.UpdateFields();
doc.Save(MyDir + "out.docx");

Please note that MS Word document is a flow document and does not contain any information about its layout into lines and pages so there is no way to determine the position of nodes on the page.

Wahaj Khan:
Also can I find details about the Signature Line if it is present e.g. Identifier, field size (X,Y, size) etc.

It would be great if you please share some more information about your query related to Signature Line. You can work with Digital Signatures by using Aspose.Words. Please read the following documentation link for your kind reference.
https://docs.aspose.com/words/net/working-with-digital-signatures/

Thanks. My query was not to change form field sizes rather to get information about them e.g. Form Field is available at particular X,Y coordinates but you have answered that point i.e. “MS Word document is a flow document and does not contain any information about its layout into lines and pages so there is no way to determine the position of nodes on the page.”

See attached a sample document which contains a signature line. I want to know where is this located in the word document i.e. X,Y coordinates and page number. Is the answer same for SignatureLine?

Also does Aspose support creation of SignatureLines on pages. If yes can I mention page, and coordinates?

Hi Wahaj,

Please accept my apologies for late response.

Thanks for your inquiry. When you load a Word document into Aspose.Words, it builds a DOM (Document Object Model) in memory which allows you to programmatically read, manipulate and modify content and formatting of a Word document.

The SignatureLine is Shape node in your document with Text Wrapping as “In Line with Text”. So, there is no X, Y position of SignatureLine. Please see the attached image for detail.

If Text Wrapping of SignatureLine (Shape) is Square, you can get the position of SignatureLine (X, Y) by using Shape.Left and Shape.Top properties as shown in following code snippet.

Shape.WrapType defines whether the shape is inline or floating. For floating shapes defines the wrapping mode for text around the shape.

Document doc = new Document(MyDir + "GlobalMeetRD.docx");
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
double x = shape.Left;
double y = shape.Top;

Regarding creation of SignatureLines, Unfortunately, currently, there is no way to insert Signature Line into a document using Aspose.Words. Your request has been linked to the appropriate issue. You will be notified as soon as this feature is supported. The features IDs are WORDSNET-656 and WORDSNET-2852.

Thanks,

I believe I could create SignatureLine if the text wrapping is Square via MS Word. I don’t know the benefit between the two types. May be you can guide when one is better

We’ll be interested in buying Aspose.Word if it contains the following features:

  1. Able to create Signature Line at at particular page with X,Y, Width Height. The text wrapping can be ‘In Line with Text’ OR 'Square’
  2. Able to get Page and X,Y, Width Height for all form fields including SignatureLine
  3. Able to set values for form fields (if possible including Signature Line) - this is already supported
  4. All of above should work for OOXML (high priority i.e. docx, xlsx, pptx) and OpenOffice

Let me know when you guys have this feature so that I can start the test and then the buying process.
Regards,
Wahaj

Hi Wahaj,

Thanks for your inquiry.

Wahaj Khan:
Able to create Signature Line at at particular page with X,Y, Width Height. The text wrapping can be ‘In Line with Text’ OR 'Square’

Please read the about text wrap from here:
http://office.microsoft.com/en-001/word-help/wrap-text-around-a-picture-HA102479705.aspx
http://blogs.office.com/b/microsoft-word/archive/2011/05/26/figuring-out-images-part-2.aspx

Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
shape.WrapType = WrapType.Square;
shape.Left = 0;
shape.Top = 0;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;

I suggest you, please read the Shape class members from here:
https://reference.aspose.com/words/net/aspose.words.drawing/shape/

*Wahaj Khan:

Able to get Page and X,Y, Width Height for all form fields including SignatureLine*

Please check my reply from here:
https://forum.aspose.com/t/61795

*Wahaj Khan:

Able to set values for form fields (if possible including Signature Line) - this is already supported*

Regarding creation of SignatureLines, Unfortunately, currently, there is no way to insert Signature Line into a document using Aspose.Words. Your request has been linked to the appropriate issue. You will be notified as soon as this feature is supported. The features IDs are WORDSNET-656** and WORDSNET-2852.
Please check my reply from here:
https://forum.aspose.com/t/61795

The issues you have found earlier (filed as WORDSNET-2978) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(76)

The issues you have found earlier (filed as WORDSNET-2852;WORDSNET-656) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(22)