Replacement of tags into tables

I’m evaluating Aspose.Words for our application. It has to do the following:
We have documents, in wich the user can place tags, like <%client%> or <%address%>; Those tags are common text in the document, we can’t use mailmerge. Then i have to read those tags and replace by some values that are registered in the database. I accomplished this very easily with aspose (it would be a lot easier if i used mailmerge, but i could get it to work, reading the tags like this:

public List GetAllTags(Regex padraoTag)
{
    if (doc == null)
        throw new Exception("Documento nπo carregado.");

    List tagList = new List();
    String txt = doc.GetText();
    MatchCollection mc = padraoTag.Matches(txt);

    foreach(Match m in mc)
    {
        tagList.Add(m.Value);
    }

    return tagList;
}

and replacing them like this:

public void ReplaceTag(String Tag, String Valor)
{
    doc.Range.Replace(new Regex(Tag), Valor);
}

Now i have to do the following: some values will be registered in the database with pipes, like “value1|value2|value3”. If i find a value like this, i have to split it and replace the associated tag, inserting a line break after each value like:
value1
value2
value3

That also was easy to do using ReplaceEvaluator. The problem i’m having is if the tag is inside of a table. The user wants the values to be inserted one at a row… I could make it work for one value, but it doesn’t work right if the table has 2 tags in the same row, like:

row1:
cell1 content: <%tag1%>
cell2 content: <%tag2%>

and then <%tag1%> would be associated to “val1|val2|val3”, each value into a new row, and <%tag2%> with “val4|val5|val6”, each value into a new row also.

Please, tell me if there is a way to do it. Also if there is an easier way to do everything i accomplished so far… The documentation is very good, but i’m having a real hard time with this table issue…

I’m sending the code from the last time i tried attached.

Thanks,
Samuel

This message was posted using Page2Forum (attachment) from Aspose.Words.Tables - Aspose.Words for .NET and Java

Hi

Thanks for your request. Maybe you should try using code like the following:

// Create dummy data source
DataTable data = new DataTable();
data.Columns.Add("FirstName");
data.Columns.Add("LastName");
data.Columns.Add("City");
data.Rows.Add(new object[]
{
    "Ben",
    "Smith",
    "London"
});
data.Rows.Add(new object[]
{
    "Den",
    "Martin",
    "London"
});
data.Rows.Add(new object[]
{
    "Ann",
    "Smith",
    "London"
});
// Open template
Document doc = new Document(@"Test068\in.doc");
// Get template row
Row tempRow = doc.FirstSection.Body.Tables[0].Rows[1];
// Insert data form datatable into the template row
Row prevRow = tempRow;
foreach(DataRow row in data.Rows)
{
    // Clone template row
    Row tempClone = (Row) tempRow.Clone(true);
    // Insert clone into the table
    prevRow.ParentNode.InsertAfter(tempClone, prevRow);
    prevRow = tempClone;
    // Replace placeholders with data
    foreach(DataColumn col in data.Columns)
    {
        tempClone.Range.Replace(new Regex(string.Format("<%{0}%>", col.ColumnName)), row[col].ToString());
    }
}
// Remove template row
tempRow.Remove();
// Save output document
doc.Save(@"Test068\out.doc");

I attached template and output documents. Hope this helps.
Best regards.