Hide/Show table MergeField

Hello,

I want to hide table if there is no data to set in.
I have a table in a second table because i need to repeating headers in each page of the document and because i need to have two headers on my table.
I try to hide my table with the IF condition but the tag TableSta
rt on my second table don’t work. It print the tag value without binding the data.
Please find attached my template that i want to hide if BiensMaison has no data.
asposeTemplate.zip (54.7 KB)

Any help is welcome ! Thanks

@lschmitz,

Please ZIP and attach the following resources.

  • Your expected document which shows the correct output. Please create this document using Microsoft Word application.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

Thanks for your response.
I want to print the data in the first table.
asposeTemplate.zip (292.4 KB)

@lschmitz,

The following code will remove the nested Table from PDF.

var template = new Aspose.Words.Document(MyDir + @"asposeTemplate (2)\asposeTemplate\templateB.docx");

DataTable table = new DataTable("Test");
table.Columns.Add("CustomerName");
table.Columns.Add("Address");
table.Columns.Add("Country");
table.Columns.Add("PhoneNumber");
table.Rows.Add(new object[] { "Thomas Hardy", "120 Hanover Sq., London", "England", "566564656" });
table.Rows.Add(new object[] { "Paolo Accorti", "Via Monte Bianco 34, Torino", "Italy", "456657678" });
            
template.MailMerge.Execute(new string[] { "Count" }, new object[] { 0 });

template.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveEmptyParagraphs;
template.MailMerge.ExecuteWithRegions(table);

template.UpdateFields();

PdfSaveOptions opts = new PdfSaveOptions();
opts.UpdateFields = false;
template.Save(MyDir + @"asposeTemplate (2)\asposeTemplate\18.2.pdf", opts);

Output PDF: 18.2.pdf (19.8 KB)

It’s work !
Thanks you very much for your quick reply !

I was wondering if it was possible to have the same result (Table with two headers + display headers on each page when all data doesn’t display on one page) without using nested Table ?
Thanks in advance.

@lschmitz,

Generally, the operations that you can perform with MS Word can also be performed by using Aspose.Words. Please create your expected Word document which shows as to how you want your final Word output be generated like. Please create this document by using Microsoft Word application. We will then provide you code to achieve the same by using Aspose.Words.

Hi,

I have another problem with this solution.
The style (police, style custom, space between word) of the word document is not the same when i use conditionnal block (MERGEFIELD IF) and without this conditionnal block.
Aspose.zip (128.8 KB)
In my attached document : you can see in the “compareTable.png” the different between the two versions. (The police of Thomas hardy is not the good police on the second table)
I expected to have the style of the first table on to my second table.
Is it possible ?

Thanks in advance.

@lschmitz,

We are working over your query and will get back to you soon.

@lschmitz,

The problem occurs because the Run nodes of fields have different font formatting (e.g. Size). You can pre-process your template Word document and specify 10 as font size to all Run nodes:

Document template = new Document(MyDir + @"aspose\templateB.docx");

foreach (Run run in template.GetChildNodes(NodeType.Run, true))
{
    //run.Font.ClearFormatting();
    run.Font.Size = 10;
    run.Font.Name = "Raleway";
    run.Font.Color = Color.FromArgb(118, 113, 113);
}

DataTable table = new DataTable("Test");
table.Columns.Add("CustomerName");
table.Columns.Add("Address");
table.Columns.Add("Country");
table.Columns.Add("PhoneNumber");
table.Rows.Add(new object[] { "Thomas Hardy", "120 Hanover Sq., London", "England", "566564656" });
table.Rows.Add(new object[] { "Paolo Accorti", "Via Monte Bianco 34, Torino", "Italy", "456657678" });

template.MailMerge.Execute(new string[] { "Count" }, new object[] { 1 });

template.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveEmptyParagraphs;
template.MailMerge.ExecuteWithRegions(table);

template.UpdateFields();

PdfSaveOptions opts = new PdfSaveOptions();
opts.UpdateFields = false;

template.Save(MyDir + @"aspose\18.3.pdf", opts);

Hi,
Unfortunatly, it didn’t work. The styling of the table is not correctly print (Text is not center in all cell, text in bold style in certain cell )
In the file behind (result.pdf), you can see that the two first rows did’nt correctly print. I expect to have the style like the thirs row.
AsposeStyling.zip (40.4 KB)

Thanks in advance.
A new version of the code to get my file (result.pdf).

foreach (Run run in template.GetChildNodes(NodeType.Run, true))
{
    //run.Font.ClearFormatting();
    run.Font.Size = 10;
    run.Font.Name = "Raleway";
    run.Font.Color = Color.FromArgb(118, 113, 113);
}
DataTable table = new DataTable("Test");
table.Columns.Add("Date");
table.Columns.Add("Type");
table.Columns.Add("Occupation");
table.Columns.Add("Pieces");
table.Columns.Add("Niveaux");
table.Columns.Add("SurfaceF");
table.Columns.Add("SurfaceTerrainF");
table.Columns.Add("EpoqueConst");
table.Columns.Add("Adresse");
table.Columns.Add("CodePostal");
table.Columns.Add("Commune");
table.Columns.Add("PrixM2");
table.Columns.Add("MontantF");
table.Rows.Add(new object[] { "02/12/2015", "Villa", "Bien Libre", "4", "3", "84 m2", "144m2", "De 1981 à 2012", "14 RUE DE RIVOLI", "75015", "Paris", "7982€", "1 000 000€" });
table.Rows.Add(new object[] { "02/12/2015", "Villa", "Bien Libre", "5", "3", "84 m2", "144m2", "De 1981 à 2012", "14 RUE DE RIVOLI", "75015", "Paris", "7982€", "1 000 000€" });
table.Rows.Add(new object[] { "02/12/2015", "Villa", "Bien Libre", "5", "3", "84 m2", "144m2", "De 1981 à 2012", "14 RUE DE RIVOLI", "75015", "Paris", "7982€", "1 000 000€" });


template.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveUnusedRegions;
template.MailMerge.Execute(new string[] { "Count" }, new object[] { 1 });
template.MailMerge.RemoveEmptyParagraphs = true;
template.MailMerge.ExecuteWithRegions(table);
template.UpdateFields();

@lschmitz,

You can fix this issue by using the following workaround:

Document template = new Document(MyDir + @"AsposeStyling\templateB.docx");

foreach (Run run in template.GetChildNodes(NodeType.Run, true))
{
    //run.Font.ClearFormatting();
    run.Font.Size = 10;
    run.Font.Name = "Raleway";
    run.Font.Color = Color.FromArgb(118, 113, 113);
    run.Font.Bold = false;
    Cell cell = (Cell)run.GetAncestor(NodeType.Cell);
    if (cell != null)
    {
        foreach (Paragraph para in cell.GetChildNodes(NodeType.Paragraph, true))
        {
            para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        }
    }
}

DataTable table = new DataTable("Test");
table.Columns.Add("Date");
table.Columns.Add("Type");
table.Columns.Add("Occupation");
table.Columns.Add("Pieces");
table.Columns.Add("Niveaux");
table.Columns.Add("SurfaceF");
table.Columns.Add("SurfaceTerrainF");
table.Columns.Add("EpoqueConst");
table.Columns.Add("Adresse");
table.Columns.Add("CodePostal");
table.Columns.Add("Commune");
table.Columns.Add("PrixM2");
table.Columns.Add("MontantF");
table.Rows.Add(new object[] { "02 / 12 / 2015", "Villa", "Bien Libre", "4", "3", "84 m2", "144m2", "De 1981 à 2012","14 RUE DE RIVOLI","75015", "Paris", "7982€", "1 000 000€"});
table.Rows.Add(new object[] { "02 / 12 / 2015", "Villa", "Bien Libre", "5", "3", "84 m2", "144m2", "De 1981 à 2012","14 RUE DE RIVOLI","75015","Paris", "7982€", "1 000 000€"});
table.Rows.Add(new object[] { "02 / 12 / 2015", "Villa", "Bien Libre", "5", "3", "84 m2", "144m2", "De 1981 à 2012","14 RUE DE RIVOLI","75015","Paris", "7982€", "1 000 000€"});

template.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveEmptyParagraphs;
template.MailMerge.Execute(new string[] { "Count" }, new object[] { 1 });

template.MailMerge.ExecuteWithRegions(table);
template.UpdateFields();

template.Save(MyDir + @"AsposeStyling\18.3.pdf");