Merge nested field with no code

One of our users has word templates with nested field codes with have no associated code and are completely redundant, e.g. {{ INCLUDETEXT “D:\Work\TemplateMerging\TestRunner\SignatureBlock.doc” }}.
We were previously replacing field codes with values using a DocumentVisitor.
We have moved to Field.unlink instead to help us with problems with INCLUDETEXT and other types being included multiple times.
This worked a treat, thank you, apart from the {{ … }} fields that aspose doesn’t see.
doc.getFirstSection().getBody().getRange().getFields() is empty as is mailMerge.getFieldNames().
The document visitor doesn’t fire visitField methods either.

As this is entirely consist I assume its intended behavior and our users will need to review all 1800 document templates.

I have a test harness if that would help.

@mleake

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

TemplateMerger
ADM-3586-DoubleBrace.doc produces output-DoubleBrace.docx
ADM-3586.doc produces output.docx

ADM-3586.doc is desired behavior, ADM-3586-DoubleBrace.doc is what is happening.

I’m pretty sure this is correct behavior, i.e. double braces with no name is just wrong.

But I need confirmation for our clients.

Not sure I attached zip

@mleake

Unfortunately, we have not found the attachment with your post. Please attach it again. Thanks for your cooperation.

@mleake

You can attach the file size up to 10MB with your post. Please do not include the JAR file in the ZIP file. If the documents’ size is greater than 10MB, please ZIP and upload them on Dropbox or any other file hosting service and share the download link here for us to test this scenario.

TestRunner.zip (238.9 KB)
Sorry. jar removed.

@mleake

In your document there is a filed of type FieldUnknown and INCLUDETEXT field is nested field in it. In this case, Field.Unlink method does not work. You need to remove the field of FieldUnknown type by iterating over field’s nodes and remove them.

Thanks for the prompt reply.
I can see a FieldType.FIELD_NONE.
What is FieldUnknown?

I use doc.getFirstSection().getBody().getRange().getFields() to get fields
Gives me fields of type 0, 68 and 2 as I’d expect.
But removing the 0 type with field.remove also removes the nested field and I’m no further forward.

What am I doing wrong?

@mleake

The FieldUnknown class implements an unknown or unrecognized field (FIELD_NONE).

The INCLUDETEXT field is nested field. So, it will be deleted when you delete the field type FIELD_NONE.

Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "ADM-3586-DoubleBrace.doc");

Field field = doc.getRange().getFields().get(0);
Boolean deletefield = false;
ArrayList nodes = new ArrayList();
if(field.getType() == FieldType.FIELD_NONE)
{
    Node node = field.getStart();
    nodes.add(node);
    node = node.getNextSibling();
    while(node != null && field.getEnd() != node)
    {
        //is nested field
        if(node.getNodeType() == NodeType.FIELD_START)
        {
            deletefield = true;
            node = field.getSeparator();
        }
        nodes.add(node);
        node = node.getNextSibling();
    }
    nodes.add(field.getEnd());
}

if(deletefield)
for(Node node : (Iterable<Node>)nodes)
{
    node.remove();
}
doc.save(MyDir + "18.11.docx");

That’s brilliant - just the thing I was looking for.

I’ve created a standalone that corrects templates and also added it to the runtime to correct the document before the merge and it works great for me.

Great job.
Many thanks.

@mleake

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.