It’s impossible to remove a column from an existing table. You can try to build the table using DocumentBuilder prior to running mail merge; it should look if there is any non-empty fields in the description field of the data source and add the corresponding column or not based on this condition.
If some columns in your table are optional, you should implement a method that creates it from scratch using DocumentBuilder. There’s a couple of examples in the forums of how to build tables dynamically, for instance see here:
Don’t forget that you just need a table with the only row for further mail merge.
Since DataSet contains multiple tables, you should repeat this process (checking if a column has no data and building the table in the template in a corresponding way) for every single table.
Basically, you can place a bookmark to specify where you want to insert a table and move there first using DocumentBuilder.MoveToBookmark. Please indicate the desired position in your document template and attach it to let us take a look.
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImageField);
doc.MailMerge.MergeField += new MergeFieldEventHandler(HandlerGauthier);
And to insert image by mailmerge we define HandleMergeImageField like this :
As I said above, it seems that in your case it’s easier to build the table (the table row in the template) completely from scratch inserting there the merge fields prior to running mail merge. Can you check your data source object that you pass to ExecuteWithRegions preliminarily? It could look like in the following pseudocode:
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("TableGauthier");
builder.StartTable();
builder.InsertCell();
builder.InsertField(@"MERGEFIELD Image:Photo * MERGEFORMAT", "");
if (data source has at least one value for FirstName)
{
Insert the new cell and put there FirstName MERGEFIELD
}
if (data source has at least one value for LastName)
{
Insert the new cell and put there LastName MERGEFIELD
}
builder.InsertCell();
builder.InsertField(@"MERGEFIELD Address * MERGEFORMAT", "");
builder.EndRow();
builder.EndTable();
The above example simply shows how to prepare a template programmatically for further mail merge. Please attach the resulting source code that causes the error, I will take a look, try to make some necessary corrections and send it back to you.
Your task seems to be very custom so use of regular mail merge is probably not the best way. I suppose to just iterate through the rows of your data source and insert new table into the document for each row as required. Attached is a working project that performs it. Please use it as an example and add all necessary table formatting to the code yourself. I have also applied some appropriate changes to your document in order to use DocumentBuilder instead of mail merge.