Problem when I try to build an image dynamically

Hello everyone,
I’m a beginner and I have a problem when I try to create an image dynamically.
I have a template in word (*.docx) like this :

I try to pass the ID param dynamically to the image like this :
<<image [SpatialGeometryScalarTimeSeriesDataSource.Create().WithAreaId(“<<[id]>>”)]>>
or like this :
<<image [SpatialGeometryScalarTimeSeriesDataSource.Create().WithAreaId(<<[id]>>)]>>
or like this :
<<image [SpatialGeometryScalarTimeSeriesDataSource.Create().WithAreaId(new GuId(<<[id]>>))]>>
but I get errors.
I can’t find some exemples on the net, can anyone help me please ?
Thanks so much.

@deid Look alike you have a custom method the will return the image by it’s ID. To achieve this you should register custom type with the required method and pass image id to the method. For example see the attached template and the following simple code:

Document doc = new Document(@"C:\Temp\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(MyCustomClass));
engine.BuildReport(doc, "12345", "id");
doc.Save(@"C:\Temp\out.docx");
public class MyCustomClass
{
    public static byte[] GetImage(string id)
    {
        // For demonstration purposes simply return image from file. 
        return File.ReadAllBytes(@"C:\Temp\test.png");
    }
}

in.docx (15.4 KB)
out.docx (18.3 KB)

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

Thank you Alexey for your reply, but I’m afraid I haven’t explained my problem properly.
In my template if I call :
<<image [SpatialGeometryScalarTimeSeriesDataSource.Create().WithAreaId(“9fa53ece-ffd4-40a8-8952-c7e42cac5438”)]>>
it works.

But, if I put this GuId in a var, like this :
<<var [id = “9fa53ece-ffd4-40a8-8952-c7e42cac5438”]>>
and then :
<<image [SpatialGeometryScalarTimeSeriesDataSource.Create().WithAreaId(id)]>>
it doesn’t work.

So, I suppose that the custom method already exist (I can’t see the backend) and probably is only a syntax error .
Thanks

@deid

Please check the following code:

DocumentBuilder builder = new DocumentBuilder();
builder.Write("<<[Util.ReturnString(\"1\")]>><<var [s = \"2\"]>><<[Util.ReturnString(s)]>>");
            
ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(Util));
engine.BuildReport(builder.Document, new object());

Console.WriteLine(builder.Document.GetText().Trim()); // Prints "12"

//-------------------------------------------------

public class Util
{
    public static string ReturnString(string value)
    {
        return value;
    }
}

As you can see, it does not matter whether a string argument is passed as a constant or as a variable. In addition to checking whether the custom class and method exist and the engine is made aware of them, the only thing that comes to my mind is that Entity["id"] may return a value of type Object, but SpatialGeometryScalarTimeSeriesDataSource.Create().WithAreaId(...) expects a string as an argument. If this is the case, then you can try changing the var tag in your template like so:

<<var [id = (string)Entity["Id"]]>>

If none of these helps, please share your sample template, data, and code reproducing the issue for further analysis on our end.

Thank you so much Ivan for your reply. I check your code and try the cast with (string).

1 Like