Free Support Forum - aspose.com

Populate missing fields in Linq Templates

We are using the Linq reporting engine to create a Document Generation Template engine.
The users can create a Word file, and where ever they need to fill in a phrase, or section from the database, they simply type in <<[DataSource.First().FieldName]>> in the word file.

This is great! We are loving it.

However, if that particular field is empty in the database, there is no way to inform the user of this in the completed document.

For example, if we have the following:
Hello Mr. <<[DataSource.First().LastName]>>
And the Last name field is empty or null in the database, then when we print out the results in the Word Document, it shows as:
Hello Mr.

I would like it to show like this:
Hello Mr. LastName
Where LastName is Bold, Italics, and red (or some other set of criteria).
Basically, if the LastName is empty or null, print the field it is tied to in the spot that is missing.

I realize that we can do something like this (where we wrap with an if condition):
Hello Mr. <<if[DataSource.First().LastName==null]>>LastName<><<[DataSource.First().LastName]>><>

The problem I have with this, is that it is so long, and it makes it difficult to maintain our templates. This can be wrapped inside other if conditions, and other tags, and it makes it really difficult to maintain everything.

What I am hoping for is some way to override the Aspose functionality that inserts text, so I can do something custom.

Some thoughts I had were to override Aspose’s Linq template engine. So when the user uses <<[Datasource.First().DateField]-“mm/dd/yy”>>, I can let Aspose do its thing. Then evaluate what Aspose prints out. If it prints out null or empty, then I can get access to the property it was trying to retrieve, and print the words “Missing DateField” in the template.

Do you have any thoughts on how I might be able to do this, or any other alternatives?

@jepthy,

We have logged your requirements in our issue tracking system. Your ticket number is WORDSNET-17435. We will look into the details of these requirements and will keep you posted on further updates. We apologize for any inconvenience.

@jepthy,

This requirement can be achieved by using the current version of Aspose.Words as shown in the following code snippet:

public class Util
{
    public static string HighlightIfNull(string value, string defaultValue = "Unspecified")
    {
        if (!string.IsNullOrEmpty(value))
            return value;

        // Apply custom formatting for a missing value using HTML.
        return string.Format("<b><i>{0}</i></b>", defaultValue);
    }
}
////////////////////////////////
// Init a test data table.
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
dataTable.Columns.Add("LastName");

// Init a data row with only one of the fields specified, so the other one is null.
DataRow dataRow = dataTable.NewRow();
dataRow["Name"] = "John";

// Init a template.
DocumentBuilder builder = new DocumentBuilder();

// This will print "John" without any special formatting, since the value is not null.
builder.Writeln("Dear <<[Util.HighlightIfNull(Name)] -html>>!");

// This will print "Unspecified" using bold and italic font settings, since the value is null.
builder.Writeln("Dear <<[Util.HighlightIfNull(LastName)] -html>>!");

// The same as above, but this time a custom value to be used for highlighting (i.e. "Last Name") is provided.
builder.Writeln("Dear <<[Util.HighlightIfNull(LastName, \"Last Name\")] -html>>!");

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(Util)); // Need this, since we are going to access static members of the Util class.

engine.BuildReport(builder.Document, dataRow);

builder.Document.Save(@"out.docx");

Hope, this helps.

@jepthy,

Regarding WORDSNET-17435, it is to update you that we have completed the work on this issue and come to a conclusion that we will not be able to implement the fix to this issue. Your issue (WORDSNET-17435) has now been closed with ‘Won’t Fix’ resolution. Please use the code from my previous post to meet this requirement.