Free Support Forum - aspose.com

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”)

<!–[if gte mso 10]> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Tableau Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;}

<![endif]–><span style=“font-size: 11pt; font-family: “Arial”,“sans-serif”;” lang=“EN-US”>I made a mistake in template.docx :
“If
fields with mergefield (line break in data)” is ok
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>FR</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Tableau Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;}

<![endif]–>

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.

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> doc = new Document(MyDir

  • “in.docx”);<o:p></o:p>

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");