Hi,
how is it possible to print a row of a table in the document based on a condition ?
I’m generating a pdf document from word with mail merging and would like to get one row of a table depending on a condition. I cannot use something like this { IF { MERGEFIELD xxx } = "true" "text 1". "text 2". }
, because i will loose the columns format of the row…
Thanks
Hi Simone,
Thanks
for your inquiry. In your case, I suggest you following solution.
- Implament IFieldMergingCallback Interface.
- In the FieldMergingCallback event, move the cursor to the position of mail merge field and get the row node by using Node.GetAncestor method.
- Do the appropriate action on current row
Hope this helps you. Please let me know if I can be of any further assistance.
Moreover, I suggest you please read following documentation links for your kind reference.
https://docs.aspose.com/words/java/mail-merge-and-reporting/
https://docs.aspose.com/words/java/how-to-apply-custom-formatting-during-mail-merge/
Thanks Tahir,
but i’m offering the creation of the pdf as a service through xml file and i cannot pass an IFieldMergingCallback object as parameter…
Isn’t there another way of doing it ? I think it’s a very common requirement in creating documents.
Infact i’m using a very old tool for creating documents with Delphi (ekrtf) and there is this very simple and clear approach:
\if(condition
…text or whatever…
\endif
I cannot think it’s not possible with aspose…
Thank you very much.
Simone
Hi Simone,
Thanks for your inquiry. Please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/supported-platforms/#com
https://reference.aspose.com/words/net/aspose.words.mailmerging/mailmerge/execute/
In
your case, I suggest you to create a wrapper class in C#/VB.NET and use
it in your Delphi code. Please check following wrapper class for your
kind reference. Hope this helps you.
namespace AsposeComWrapper
{
class ComWrapper
{
public void MailMerge(string input, string output, string xmlpath)
{
Document doc = new Document(input);
// Add a handler for the MergeField event.
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
DataSet ds = new DataSet();
ds.ReadXml(xmlpath);
// Execute mail merge.
doc.MailMerge.ExecuteWithRegions(ds);
// Save resulting document with a new name.
doc.Save(output);
}
private class HandleMergeField : IFieldMergingCallback
{
// /
// / This is called when merge field is actually merged with data in the document.
// /
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
if (e.FieldValue.ToString() == "True")
{
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToMergeField(e.DocumentFieldName);
Row row = (Row)builder.CurrentNode.GetAncestor(NodeType.Row);
// Your code...
// Your code...
e.Text = "";
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
// Do nothing.
}
}
}
}
Hi Tahir,
I dont’t want to use Aspose with Delphi. I mentioned Delphi only because i use a delphi tool which is called ekrtf which does something very similar of mail merging done with Aspose and I liked to share an example using IF.
Please refer to this post about using IF in templates: https://forum.aspose.com/t/46058
Hi Simone,
Thanks for sharing the detail. I have answered your query here. Please follow that thread for further proceedings.
https://docs.aspose.com/words/net/supported-platforms/#com