We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding images in a template document during BuildReport

In .net we gather all sorts of data into a DataSet and call BuildReport with a word template … to generate the final word doc. This works great with simple data such as strings and numbers. But how do I get an image into the DataSet and then parse that information in the template?

Thanks

Hi Frank,

Thanks for your inquiry. I suggest you please read about inserting image using Linq reporting engine from here:
https://docs.aspose.com/words/net/working-with-images/

Please use the following code example to achieve your requirements. I have attached the input template document with this post for your kind reference. Hope this helps you. Please let us know if you have any more queries.

DataTable dt = new DataTable("Persons");
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("picture", typeof(Stream));
DataRow dr = dt.NewRow();
dr["Name"] = "Aspose.Words";
dr["picture"] = new MemoryStream(File.ReadAllBytes(MyDir + "in.jpg"));
dt.Rows.Add(dr);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "ds");
doc.Save(MyDir + "Out.docx");

Thanks! This makes more sense. What about inside of the word template to show the image? What is the specific syntax to show the image?
Using pseudo code would it be something like:
<<foreach [item in ds.Persons]>>Image:<<[item.picture]>><</foreach>>
???
Thanks again.

Hi Frank,

Thanks for your inquiry. The template syntax of image tag is explained in following documentation link.
https://docs.aspose.com/words/net/working-with-images/

Please let us know if you have any more queries.

Thanks for your help thus far. I am having another issue though with regards to the text box size. I have added a textbox, put the image tag in it, and the image displays. Unfortunately I can’t find how to have the textbox size be changed to fit the images. The images have different sizes, so the textbox needs to change size accordingly to show the image correctly. How do I tell the textbox what size to change to based on the image tag?
Thanks

Hi Frank,

Thanks for your inquiry. This feature has been implemented in next version of Aspose.Words 15.9.0. Your thread has been linked to the appropriate issue (WORDSNET-12077) and you will be notified as soon as it is available. We’re hopeful to release next version in next 2~3 days.

Please use the following templates with Aspose.Words 15.9.0 to meet this requirement:

  1. To get a textbox (filled with an image) whose width is kept, whereas its height is changed to keep a contained image ratio, a template should be as follows.

<<image [image_expression] -fitHeight>>

  1. To get a textbox (filled with an image) which height is kept, whereas its width is changed to keep a contained image ratio, a template should be as follows.

<<image [image_expression] -fitWidth>>

  1. To get a textbox (filled with an image) which size is equal to the size of a contained image, a template should be as follows.

<<image [image_expression] -fitSize>>

Hope, this helps.

Best regards,

Hi Frank,

Thanks for your inquiry. Please use the following highlighted code snippet to resize the image. Hope this helps you. Please let us know if you have any more queries.

DataTable dt = new DataTable("Persons");
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("picture", typeof(Stream));
DataRow dr = dt.NewRow();
dr["Name"] = "Aspose.Words";
dr["picture"] = new MemoryStream(File.ReadAllBytes(MyDir + "in.jpg"));
dt.Rows.Add(dr);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "ds");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.HasImage)
    {
        ImageSize imageSize = shape.ImageData.ImageSize;
        shape.Width = imageSize.WidthPoints;
        shape.Height = imageSize.HeightPoints;
    }
}
doc.Save(MyDir + "Out.docx");

The issues you have found earlier (filed as WORDSNET-12077) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.