Hiding blank fields

In order to be compatible with our in house system, I am using text form fields in my word document. I use aspose.word to fill these fields in, but if there is no text for a given field (eg a line of an address), I want to be able to move the other fields up. Is there any way of doing this with aspose? I attach an example of my document template.

Many thanks

Shellie

Hi,

If I correctly understood your task, you want to remove empty form fields as well as the containing paragraphs, don't you? If so, then the following code would accomplish that:

ArrayList formFieldsToRemove = new ArrayList();

foreach (FormField formField in doc.Range.FormFields)

{

if (formField.Result.Length == 0)

formFieldsToRemove.Add(formField);

}

foreach (FormField formField in formFieldsToRemove)

{

if (formField.ParentNode.ParentNode != null)

formField.ParentNode.Remove();

}

If your task is different, just let me know.

Many thanks for the response. I am writing in VB.net, so tried to replicate your code, using the following:

Private Sub RemoveEmptyFields(ByVal NewDoc)

'Dim formFieldsToRemove As New ArrayList

'Dim DocFormFields As Aspose.Word.FormFields

'Dim FCount As Integer

'DocFormFields = newdoc.Range.FormFields

'FCount = newdoc.Range.FormFields.Count

' For i = 0 To (FCount - 1)

'If DocFormFields.Item(i).Result.Length = 0 Then

' formFieldsToRemove.Add(DocFormFields.Item(i))

' End If

' Next

'For Each DocFormFields In formFieldsToRemove

' If DocFormFields.Item(i).ParentNode Is Nothing Then

'DocFormFields.Item(i).ParentNode.Remove()

'End If

'Next

Dim DocFormFields As Aspose.Word.FormFields

Dim FCount As Integer

DocFormFields = NewDoc.Range.FormFields

FCount = NewDoc.Range.FormFields.Count

For i = 0 To (FCount - 1)

If DocFormFields.Item(i).Result.Length = 0 Then

If DocFormFields.Item(i).ParentNode Is Nothing Then

DocFormFields.Item(i).ParentNode.Remove()

End If

End If

Next

End Sub

I couldnt get the first lot to work, so commented it out (green writing) and used the second bit of code. This runs through fine, but doesnt see any of the relevent parent nodes as being null. I am not sure if it is something I have got wrong in the code or if the template document needs amending. I attach the template document (standard pharm sics.doc) and an example of a filled in document with the blank line in the address (example.doc).

Many thanks

Shellie

Shellie,

Here's the code in VB .NET. I used a tool for translation but it should work:

Dim formFieldsToRemove As ArrayList = New ArrayList()

For Each formField As FormField In doc.Range.FormFields

If formField.Result.Length = 0 Then

formFieldsToRemove.Add(formField)

End If

Next formField

For Each formField As FormField In formFieldsToRemove

If Not formField.ParentNode.ParentNode Is Nothing Then

formField.ParentNode.Remove()

End If

Next formField

Works perfectly - many thanks.

Can see what I did wrong mainly, but why do you test for parentnode.parentnode being nothing rather than just the parentnode?

Thanks once again

Shellie

Oh sorry for not providing a comment there. The point is the following. There is no need to check whether a form field has a parent because it is not removed directly. Instead, we remove its parent (paragraph). But if a paragraph contains more than one form field, then, when we encounter 2nd and subsequent form fields, calling Remove on the same parent paragraph will throw because it is already removed. That's why we should check if the parent paragraph has a parent (whether it is removed).