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

Free Support Forum - aspose.com

Inner Merge Problem

Hello,

my task is to change some text to mergefield and later replace it using mail merge.

But i have got the problem. When mergefield is located in IF field after executing update

nothing happens to merge field ( in IF field,outside seems ok) . I attached document to try, and code is here:

public static void main(String[] args)
{
    try
    {
        Document doc = new Document("C:\sample2.docx");
        DocumentBuilder docbuilder = new DocumentBuilder(doc);
        Range range = doc.getRange();
        ReplaceEvaluatorFindAndHighlight replaceEvaluator = new ReplaceEvaluatorFindAndHighlight(doc);
        int find = range.replace(Pattern.compile("#\\w+?\\.\\w+?#"), replaceEvaluator, false);
        doc.getMailMerge().execute(new String[] { "#Pracownik.Pensja#", "#Imie.Nazwisko#" }, new Object[] { new Integer(1234), "Peter Pan" }); doc.updateFields();
        doc.save("C:\sampleMerge.docx");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
class ReplaceEvaluatorFindAndHighlight implements IReplacingCallback
{
    /**
     * This method is called by the Aspose.Words find and replace engine for each match.
     * This method highlights the match string, even if it spans multiple runs.
     *
     */
    Document doc;
    public int i=0;
    public ReplaceEvaluatorFindAndHighlight(Document doc) {
        this.doc = doc;
        i=0;
    }

    public int replacing( ReplacingArgs e) throws Exception
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();
        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.getMatchOffset() > 0) {
            currentNode = splitRun((Run)currentNode, e.getMatchOffset());
        }
        // This array is used to store all nodes of the match for further highlighting.
        ArrayList runs = new ArrayList();
        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().length();
        while (
                (remainingLength > 0) &&
                        (currentNode != null) &&
                        (currentNode.getText().length() <= remainingLength))
        {
            runs.add(currentNode);
            remainingLength = remainingLength - currentNode.getText().length();
            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.getNextSibling();
            }
            while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
        }
        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {
            splitRun((Run)currentNode, remainingLength);
            runs.add(currentNode);
        }
        //String text = e.getMatch().group(0);
        //for (int i=1;i< runs.size() ; i++) {
        // ((Run) runs.get(i)).remove() ;
        //}
        //((Run)runs.get(0)).setText(text);
        DocumentBuilder builder = new DocumentBuilder( doc );
        builder.moveTo((Run)runs.get( runs.size() - 1) );
        String fieldName = e.getMatch().group(0);
        builder.insertField( "MERGEFIELD " + fieldName + " \\* MERGEFORMAT");
        // Now remove all runs in the sequence.
        for (Node run : runs)
            run.remove();
        //DocumentBuilder docbuild = new DocumentBuilder(doc);
        // docbuild.moveTo( runs.get(0) );
        // docbuild.insertField("MERGEFIELD " + e.getMatch().group(0) );
        i++;
        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.SKIP;
    }

    /**
     * Splits text of the specified run into two runs.
     * Inserts the new run just after the specified run.
     */
    private static Run splitRun(Run run, int position) throws Exception
    {
        Run afterRun = (Run)run.deepClone(true);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}

Hi Pio,

Thanks for your inquiry. I tested your scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-8365. We will further look into the details of this problem and keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi Pio,

Thanks for your patience.

I’m very sorry to share with you that on further investigation our development team came to know that they won’t be able to implement the fix to your issue. Your issue is closed with ‘Won’t Fix’ resolution. This means the issue is actually not fixed but closed because we can not fix it. This is because Aspose.Words doesn’t support updating of nested fields in IF field’s false arguments any more. Moreover, such an argument does not affect IF field’s result; therefore, it is safe and there is nothing to worry about. If we can help you with anything else, please feel free to ask.

Best regards,