Nested if fields with line break

Hello

It seems that nested if fields with line break are not correctly evaluated :

DataSet ds = new DataSet();
ds.ReadXml(@"path\data.xml");
License lic = new License();
lic.SetLicense(@"path\Aspose.Total.lic");
Document doc = new Document(@"path\template.docx");
doc.MailMerge.Execute(ds.Tables[0]);
doc.Save(@"C:\Users\Max\Desktop\out.docx");

I attached the files to reproduce this issue (tested with the last version of aspose word)

{ IF "A" = "A" "SOME
TEXT" "TEXT" }
=> ok ("SOME
TEXT")

{ IF "B" = "B" "{ IF A = A "SOME

TEXT" "TEXT" }" "TEXT" }
=> ko (empty result)

{ IF "B" = "B" "{ IF A = A "SOME TEXT" "TEXT" }" "TEXT" }

=> ok ("SOME TEXT")

I made a mistake in template.docx :
“If
fields with mergefield (line break in data)” is ok

only “Nested If fields with mergefield (line break in data)” is ko

Hi
Thank you for reporting this problem to us. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.
Best regards,

My customers can’t wait the next version, I have to find a workaround.

Something like this should work :

  • mailmerge.execute()
  • replace paragraph break with line break in nested if fields
  • updatefields()

could you help me to make a visitor that replace paragraph break with line break in nested if fields ?

Hi
Thanks for your request. I think it would be easier to replace paragraph breaks with line breaks in your data source before executing mail merge. In this case, you should simply replace ‘\n’ character with ‘\v’ character.
Best regards,

ok, thanks

Will this issue be resolved quickly ?

Hi
Thanks for your request. Unfortunately, I cannot provide you an estimate at the moment. Our development team will analyze the issues and then we will provide you more information.
Best regards,

Hello

Some of my customers have templates like this :

{ IF "A" = "A" "{IF "B" = "B" "some text[paragraph break]
other text" "ko" }" "ko" }

I can’t only replace ‘\n’ with ‘\v’ in my data, I have to replace paragraph breaks with line breaks in nested if fiels programaticaly (I can’t ask them to modify thousands of templates)

Could you please help me to do this ?

Hi
Thanks for your inquiry. You can try using code like the following to achieve this:

[Test]
public void Test001()
{
    Document doc = new Document(@"Test001\in.docx");
    doc.Accept(new ParagraphBreakHelper());
    doc.UpdateFields();
    doc.Save(@"Test001\out.docx");
}

private class ParagraphBreakHelper : DocumentVisitor
{
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        // Check whether paragraph is inside a fiedld. 
        // We can suppose that paragraph is inside field, if one of the previouse node is FieldStart or FieldSeparator.
        // In this case we copy content of the next paragraph into the curent paragraph and insert a line break.
        Node currentNode = paragraph.LastChild;
        while (currentNode != null)
        {
            if (currentNode.NodeType == NodeType.FieldEnd)
                break;
            if (currentNode.NodeType == NodeType.FieldStart || currentNode.NodeType == NodeType.FieldSeparator)
            {
                // Get next paragraph.
                Paragraph nextParagraph = (Paragraph)paragraph.NextSibling;
                // Stop processign if there is no next paragraph. This can occur if the current paragraph is in the table.
                if (nextParagraph == null)
                    break;
                // Create a run with line break.
                Run lineBreak = new Run(paragraph.Document, "\v");
                // Insert it at the end of the curent paregraph.
                paragraph.AppendChild(lineBreak);
                // copy content of the previouse paragraph into the cirrent.
                while (nextParagraph.HasChildNodes)
                    paragraph.AppendChild(nextParagraph.FirstChild);
                // Remove next paragraph.
                nextParagraph.Remove();
            }
            // Move to the next node.
            currentNode = currentNode.PreviousSibling;
        }
        return VisitorAction.Continue;
    }
}

Hope this helps.
Best regards,

Thanks for your quick reply, but it seems it don’t work in this case

I’m testing with this kind of template :

{ IF "A" = "A" "{ IF "B" = "B" "{ MERGEFIELD data1 }[paragraph break]
{ MERGEFIELD data2 }[paragraph break]
some texte" "ko" }" "ko" }

Hi
Thanks for your request. Please try removing this condition:

if(currentNode.NodeType==NodeType.FieldEnd)
    break;

Best regards,

Hello

I’m testing this code to replace paragraphe break with line break only in nested if fields.
It seems it work fine, it could help someone having the same issue :

do
{
    _ParagraphBreakHelper = new ParagraphBreakHelper();
    document.Accept(_ParagraphBreakHelper);
}
while (!_ParagraphBreakHelper.termine);

class ParagraphBreakHelper : DocumentVisitor
{
    List InIfFieldCode = new List();
    public bool termine = true;

    public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)
    {
        if (fieldStart.FieldType == Aspose.Words.Fields.FieldType.FieldIf)
            InIfFieldCode.Add(true);
        return VisitorAction.Continue;
    }

    public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)
    {
        if (fieldSeparator.FieldType == Aspose.Words.Fields.FieldType.FieldIf)
            InIfFieldCode[InIfFieldCode.Count - 1] = false;
        return VisitorAction.Continue;
    }

    public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)
    {
        if (fieldEnd.FieldType == Aspose.Words.Fields.FieldType.FieldIf)
            InIfFieldCode.RemoveAt(InIfFieldCode.Count - 1);
        return VisitorAction.Continue;
    }

    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        if (InIfFieldCode.Count > 1 && IsInIfFieldCode())
        {
            // Create a run with line break.
            Run lineBreak = new Run(paragraph.Document, "\v");

            // Insert it at the end of the curent paregraph.
            paragraph.AppendChild(lineBreak);

            // copy content of the previouse paragraph into the cirrent.
            Paragraph nextParagraph = (Paragraph)paragraph.NextSibling;
            if (nextParagraph != null)
            {
                while (nextParagraph.HasChildNodes)
                    paragraph.AppendChild(nextParagraph.FirstChild);

                // Remove next paragraph.
                nextParagraph.Remove();

                termine = false;
                return VisitorAction.Stop;
            }
        }

        return VisitorAction.Continue;
    }

    private bool IsInIfFieldCode()
    {
        bool tmp = InIfFieldCode.Count > 0 ? true : false;
        foreach (bool val in InIfFieldCode)
            tmp = tmp && val;
        return tmp;
    }
}

Hi
It is perfect that you managed to achieve what you need.
Best regards,

Hello

I think there is another bug with paragraph break in nested if fields.

Please see attached files

DataSet ds = new DataSet();
ds.ReadXml(@"C:\Users\Max\Desktop\data.xml");
License lic = new License();
lic.SetLicense(@"C:\Users\Max\Desktop\Aspose.Total.lic");
Document doc = new Document(@"C:\Users\Max\Desktop\template.docx");
doc.MailMerge.Execute(ds.Tables[0]);
doc.UpdateFields();
doc.Save(@"C:\Users\Max\Desktop\out.docx");

The content of the if fields is repeated on mail merge.

Hi
Thank you for reporting this problem to us. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.
Best regards,

These issues on if fields are very problematic regressions of Aspose.Words, I hope that it will be resolved quickly

We will let you know once the problem is fixed. I apologize for inconvenience.
Best regards,

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


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

Issues with nested IF’s still linger. It has been driving me crazy. I see somewhat of a pattern. It seems to be an issue when an internal IF ends at the end of its parent IF. The parent IF does not like to end with another IF. It wants something else, and that “something else” is a new paragraph.

{IF {MERGEFIELD Q1} = true “{IF {MERGEFIELD Q2} = true “this text
with new paragraphs
and more paragraphs” “that text
with some new paragraphs
and more paragraphs”}” “”} => no go

But if you press enter after the second-to-last bracket, it works:

{IF {MERGEFIELD Q1} = true "{IF {MERGEFIELD Q2} = true “this text
with new paragraphs
and more paragraphs” “that text
with some new paragraphs
and more paragraphs”}
" “”} <= this new paragraph corrects the problem

Of course, you wind up with an unwanted line. Worse, however, is the problem of having to figure when you need to press enter for a new paragraph. It gets crazy and frustrating when you have sub-documents you are importing into the main document. The result is sometimes you see large chunks of missing output, and in other cases, it acts as if the IF statement evaluated to false when you know for a fact it is true.

I have a handful of documents I would be happy to review with you. The problem with just posting them is that they reference data which is fed from a database, and the code to run all this is kind of complex. I don’t know that giving them to you would help or not.

I have attached one so you can see anyway. Notice the seemingly unnecessary new paragraph at the bottom? It is necessary, or it will not work.

Anything you could do to confirm and find the bug would be very helpful. We waste far too many hours running into this issue and trying to know “where” we need to press enter and add an unwanted line. I will be happy to work with you and share anything. You can log onto my machine remotely and watch the process if it helps. I can recreate the issue with ease in only seconds.

Please remove the attachment from public view once you get it.

Thanks.

Hi Jeff,

Thanks for your inquiry. Please accept my apologies for late response.

I have made this thread as private and now only users in this thread and Aspose members can access this thread.

I have tested the scenario and have not faced the shared issue. I have used the following code snippet to test this issue. It would be great if you please modify the data in following code snippet and share it with us to reproduce this issue.

Please take screen shots of input and output document which describe the problem and share those screen shots with us.

Document doc = new Document(MyDir + "in.docx");
doc.MailMerge.Execute(
new string[] { "Sub_7" , "Q8", "Q1", "Q70 ", "Q71", "Q7" },
new object[] { "Sub_7 value", "true", "A1a", "true", "A47", "Q7 value" });
doc.UpdateFields();
doc.Save(MyDir + @"out.docx");