Linq Reporting Engine - Migration from MailMerge with IFieldMergingCallback to Reporting Engine

Hi,

I’m trying to create a document from dynamic xml source and associated template document with LINQ Reporting Engine in Java

The scenario is: Given folder with data.xml, images, barcodes, qr's and templates in docx (with fields mapped to xml tags) to generate a final document with all components

The current implementation is using MailMerge Fields with IFieldMergingCallback:

var dataDir = "src/main/resources/";

Document doc = new Document(dataDir + "template1.docx");

var ds = new DataSet();

ds.readXml(xmlNode); //Xml node of first "ID" element as bytearray

doc.getMailMerge().setFieldMergingCallback(mergeFieldCustom);

doc.getMailMerge().executeWithRegions(ds);

doc.save(dataDir + "generated.docx");

IFieldMergingCallback is used with a scope to implement custom functions defined inside the template document (ex custom text formats, number formats, replace functions, required fields etc…)

data.xml content has dynamic structure and generated from “outside”:

<General>
    <Data>
        <ID templateName="template1.docx">
            <documentId>documentId1</documentId>
            <text>Some text field</text>
            <image1>images/image1.png</image1> #Image loaded using IFieldMergingCallback
            <image2>images/image2.png</image2> #Image loaded using IFieldMergingCallback
            <qrcode>BARCODE CONTENT IN TEXT</qrcode> #QR code rendered as image in template using IFieldMergingCallback
        </ID>
        <ID templateName="template2.docx">
            <documentId>documentId2</documentId>
            <text>Some text field</text>
            <image1>images/image3.png</image1>
            <image2>images/image4.png</image2>
            <qrcode>BARCODE CONTENT IN TEXT</qrcode>
        </ID>
    </Data>
</General>

Template content:


Text:
 {{ MERGEFIELD text }}
Expected: Some text field
Image1:
 {{ MERGEFIELD IMAGE_image1 }}
Expected: [image]
Image2:
 {{ MERGEFIELD IMAGE_image2 }}
Expected: [image]
Qr:
 {{ MERGEFIELD QR_qr_code}}
Expected: [barcode image]
Formatted Text in uppercase:
 {{ MERGEFIELD $FF(text, "UPPERCASE") }}
Expected: SOME TEXT FIELD
Required field:
 {{ MERGEFIELD REQUIRED_not_existing_field }}
Expected: *EXCEPTION*

Now i’m trying to migrate this functionality to Reporting Engine and my questions are:

  1. Is it possible to use someting similar to IFieldMergingCallback?
  2. Is it possible to extend custom tag inside a document template (something like <<qrcode [expression] -params>>)?
  3. Any example of using custom types methioned in this answer: How to use custom tag for LINQ Reporting Engine using .NET - #2 by tahir.manzoor

Thanks.

@nhnatyshyn

There is no analogue of IFieldMergingCallback for LINQ Reporting Engine, nor is there a possibility to use custom tags. As you have found by yourself, there is another way of extending and customizing the engine’s behavior - using of custom types. You may find a relevant example in this reply.

Please note that a custom type which members are going to be accessed through a template should meet certain requirements. See Setting up Known External Types for details.

Regarding your mentioned use cases, all those are achievable using the engine and custom types’ functionality.

Regarding outputting images, please refer to Inserting Images Dynamically. With some extra steps, insertion of barcodes can be implemented the same way. Here are the steps:

  1. Introduce a custom class in your application that generates a barcode image and saves it into a byte array (or another object supported by an image tag) as follows:
public class Util
{
    public static byte[] getBarcodeImage(String text)
    {
        // Your code for barcode image generation copied from your IFieldMergingCallback implementation.
    }
}
  1. Make the engine be aware of this class as follows:
ReportingEngine engine = new ReportingEngine();
engine.getKnownTypes().add(Util.class);
engine.buildReport(...);
  1. Use an image tag to insert a barcode image using the custom class as follows:
<<image [Util.getBarcodeImage(qrcode)] -fitSize>>

Custom text formatting (such as applying a bold font) can be achieved by using of an html switch as per Outputting Expression Results. (Built-in formats for numeric and string values listed at the end of this article may be also useful for your purposes).

All other custom functionality mentioned by you can be achieved using a similar approach by adding more static members to the Util class, for example.

Please feel free to ask if more help is needed.

1 Like

Thanks for your response, that’s really helpful.

1 Like