ReportingEngine sourcing image data externally from data source

Hi all,

I would like to use XML as a data source for the ReportingEngine.

I’ve seen plenty of examples where we supply POCOs with image properties which can then appear in the document but I can’t find an example where we start with XML, load up a dataset and supply image data separately.

I tried using a LoadOptions.ResourceLoadingCallback with the Document constructor, but the ResourceLoading method is not called when BuildReport is called.

Can someone point me to a sample or the correct documentation if this is doable?

Regards,
Christian

Hi Christian,

Thanks for your inquiry. Please us DataSet.ReadXml method to read the xml into DataSet. Please refer to the following article:

Working with DataSet objects

Please check the following code snippet. Given a template to be populated with a data from a DataSet instance that is identified as “ds” within the template, you can use the following code to build the corresponding report.

Document doc = ... // Loading a template document.
DataSet dataSet = ... // Setting up a data set.
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dataSet, "ds");

Hi Tahir,

Thanks for the reply. I understand how to load a dataset from XML and use with the reporting engine, but what I can’t find is how to reference images from a template that loads its data source in this manner.

Regards,

Christian

Hi Christian,

Thanks for your inquiry. Unfortunately, your question isn’t clear enough therefore we request you to please elaborate your inquiry further. Also, please share your XML, input and expected output documents. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

Hi Tahir,

I think I have the solution. I just need to wrap my call to the image source in a static class and add this class as a known type for the reporting engine.
https://docs.aspose.com/words/java/linq-reporting-engine-api/

In my template I just reference the method using the class name and method. I can then pass arguments to the method from the current scope. Here’s a modified version of the CommonMasterDetail example that can be downloaded from GitHub:
https://github.com/aspose-words/Aspose.Words-for-.NET

public class CommonMasterDetailWithExternalImage
{
    public static void Run()
    {
        //ExStart:CommonMasterDetailWithExternalImage
        // The path to the documents directory.
        string dataDir = RunExamples.GetDataDir_LINQ();
        string fileName = "CommonMasterDetailWithExternalImage.doc";


        // Load the template document.
        Document doc = new Document(dataDir + fileName);


        // Create a Reporting Engine.
        ReportingEngine engine = new ReportingEngine();
        engine.KnownTypes.Add(typeof(ImageUtil));


        // Execute the build report.
        engine.BuildReport(doc, Common.GetManagers(), "managers");


        dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);


        // Save the finished document to disk.
        doc.Save(dataDir);
        //ExEnd:CommonMasterDetailWithExternalImage
        Console.WriteLine("\nCommon master detail template document is populated with the data about managers and it's contracts. Images sourced from ImageUtil calls.\nFile saved at " + dataDir);


    }
}


public class ImageUtil
{
    public static byte[] GetImageByName(string imageName)
    {
        return Common.Photo();
    }
}

And this is the Word markup:

<<foreach [in managers]>>
<<image [ImageUtil.GetImageByName(Name)]>>
<<[Name]>>
Clients: <<foreach [in Contracts]>><<[IndexOf() != 0 ? “, ” : “”]>><<[Client.Name]>><>
<>

Hi Christian,

Thanks for sharing the detail. Yes, you are using the correct approach to insert the image dynamically into Document. You just need to pass image’s path to Common.Photo method that returns image’s bytes.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

public class ImageUtil
{
    public static byte[] GetImageByName(string imageName)
    {
        return Common.Photo(imageName);
    }
}