Word linq reporting some general questions

Hi guys,

We have been using mail merging for very long time. I am doing the initial discovery work to enable the linq reporting for better templating experience.

I’ve got a few questions

  • Is it safe to use mail merge and linq reporting together? ie) a template file containing mail merge & linq reporting syntax
    I’ve done some initial testings. It seems to be working fine calling ExecuteWithRegions and BuildReport in order. This way I can keep the compatibility with existing templates created with mail merge, yet enables new linq reporting feature. Do you see any problems with this approach?

  • In mail merge, we used FieldMerge event to output images. In linq reporting, it’s using image tag with expression that either returns Image, Stream, Base64 or path to image. If we happen to have some images used repeatedly in the template, what method do you recommend for performance? ie) image tag used inside foreach tag, and image outputs Red signal image XX number of times.
    I got the sample code working by passing image path. Would it be still okay to use the path or should it be better with base64?

  • About the bookmark handling, I can see there is bookmark tag. This seems to be the tag used along with DocumentBuilder to create a template dynamically. If I create a dotx file with bookmark tag in it, Document.Range.Bookmark collection would not recognize this tag. I cannot populate the value with bookmark this way. Am I understanding it correct?

Template example:


Sample code to push value to the bookmark which wouldn’t work

  • Can I please get some samples of registering utility class and consuming it within template? I created a sample code that has Utils class. It has a method that returns color code. Engine doesn’t recognize the method name. I was trying to use below documented method
    Setting up Known External Types in C#|Aspose.Words for .NET

I am getting below error
An error has been encountered at the end of expression ‘Utils.GetColorCode([“red”])]>’. Can not resolve method ‘GetColorCode’ on type ‘LinqReporting.Utils’

LinqReporting.zip (121.4 KB)

Sorry for stacking multiple questions.

Thanks

@sentientsoft

  1. Yes, it is safe to use both Mail Merge and LINQ Reporting Engine in one template, because they use different placeholders. Mail Merge uses mergefields, and LINQ Reporting Engine uses tags, like <<...>>. But you should make sure that executing mail merge does not break the LINQ template syntax.

  2. I do not think there will be any significant performance difference when use image from path or from base64. In both case the image should be read/decoded into a stream.

  3. The syntax like the following

<<bookmark [bookmark_expression]>>
bookmarked_content
<</bookmark>>

is for for creating bookmarks dynamically upon building the report.
https://docs.aspose.com/words/net/inserting-bookmarks-dynamically/
i.e. the actual bookmark is not there in the document before building the report.
For example the following code creates 3 bookmarks in the output document:

string[] bookmarkNames = new string[] { "First", "Second", "Third" };

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<foreach [bk in bookmarks]>><<bookmark [bk]>><<[bk]>><</bookmark>><</foreach>>");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, bookmarkNames, "bookmarks");

doc.Save(@"C:\Temp\out.docx");
  1. Sure here is simple example.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<[MyUtil.GetColorCode(someColor)]>>");

ReportingEngine engine = new ReportingEngine();
// Register the util class in the known types
engine.KnownTypes.Add(typeof(MyUtil));
// Build the report.
engine.BuildReport(doc, Color.Red, "someColor");

doc.Save(@"C:\Temp\out.docx");
public class MyUtil
{
    public static string GetColorCode(System.Drawing.Color color)
    {
        return $"R={color.R}, G={color.G}, B={color.B}, A={color.A}";
    }
}

Please see our documentation to lean more:
https://docs.aspose.com/words/net/setting-up-known-external-types/

in your example, the syntax is not correct. The syntax should look like this:

<<backColor [Utils.GetColorCode(Flag)]>><<[Flag]>><</backColor>>

Here is the modified template and output:
in.docx (26.0 KB)
TestResult.docx (111.1 KB)