Render multiple images in a row

Hello,

I have a array of images. I want to render the all images in array two images in a row. I have attached my syntax. It throws an error.

It works when trying to render 1 image per row. Also, is there any way to fill (stretch) the last row if only one image in the last row

Regards,

@NaraSg

To render multiple images in a row using Aspose.Words, you can utilize a table to arrange the images effectively. Below is a C# example demonstrating how to achieve this:

using Aspose.Words;
using Aspose.Words.Drawing;

// Load your document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create a table with two columns
Table table = builder.StartTable();

// Assuming you have an array of image paths
string[] imagePaths = { "image1.png", "image2.png", "image3.png", "image4.png" };

for (int i = 0; i < imagePaths.Length; i++)
{
    // Insert the image
    builder.InsertImage(imagePaths[i]);

    // End the cell
    builder.EndRow();

    // Check if we need to start a new row
    if ((i + 1) % 2 == 0 && i + 1 < imagePaths.Length)
    {
        builder.InsertCell(); // Start a new cell for the next row
    }
}

// End the table
builder.EndTable();

// Save the document
doc.Save("output.docx");

In this example, images are inserted into a table with two columns. If there is an odd number of images, the last row will contain a single image, which may not stretch to fill the space. To ensure the last image fills the remaining space, you can adjust the width of the last cell accordingly.

If you encounter any errors, please provide the specific error message for further assistance.

Hello,

I am interested in achieving the above result using LINQ reporting syntax in template. Not from code-behind.

Regards,
Narayanan

@NaraSg Could you please attach your template document here for our reference? We will check the issue and provide you more information.

Hello,
Attached the template.
Sample.docx (53.6 KB)

Images(json datasource) is an array of object containing two properties

  1. , a string for filename
  2. byte array to hold the data (say png images)).

Thank you

Regards,
Narayanan

@NaraSg The problem occurs because floating shapes are used in your template. Two of shapes are visually inside foreach, but actually are placed outside foreach:

Please try sing the following modified template: modified.docx (53.1 KB)

Hello Alexey,
With the modified.docx in the previous message, each image is repeated twice on every row.
I am expecting that from the list of images, every row should display two distinct images (not the same). If there are odd number of images in the list, would it be possible to expand the last image to fill the row…

Regards,

@NaraSg You can use <<next>> tag to move to the next record. Please see the attached template and sample data:
in.docx (53.2 KB)
data.zip (853 Bytes)
Here is test code:

JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json");

Document doc = new Document(@"C:\Temp\in.docx");
        
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "Images");

doc.Save(@"C:\Temp\out.docx");

And here is the produced output document
out.docx (38.3 KB)

Hello Alexey,

Thanks for the solution.
Suppose I had odd number images for the above layout, I also wanted to know if anything can be done to stretch the last image to occupy the full row. e.g. 3 images, 2 images occupy the first row, the the image stretches to occupy the second row (fill feature)

Regards,

@NaraSg Unfortunately, I do not see any possibility to achieve this using LINQ Reporting Syntax only. I will consult with my colleague, who is superior in LINQ Reporting Engine, and we will get back to you shortly.

Hello Alexey,
Thank you for the update.

1 Like

@NaraSg

You can apply an if tag in this scenario to get expected output. Please check
in-modified.docx (55.5 KB) explaining the idea. You can further adjust the template as per your requirements.

As a note, if extra paragraphs are unwanted, please consider using ReportBuildOptions.RemoveEmptyParagraphs as follows:

ReportingEngine engine = new ReportingEngine();
engine.Options | = ReportBuildOptions.RemoveEmptyParagraphs;
engine.BuildReport(...);

See Removing Paragraphs with Template Syntax Tags for more information.

1 Like

Hello Ivan,
I tried the suggestion. Unfortunately, it throws an error in Image.Count(). I suspect, since Images.Count() is within the foreach loop, it is trying to look for Count() in the iterated item “x”.
Shared the error. Please advise.
Regards,

@NaraSg The template provide by Ivan works fine with the following code:

JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json");

Document doc = new Document(@"C:\Temp\in.docx");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "Images");

doc.Save(@"C:\Temp\out.docx");

in-modified.docx (55.5 KB)
data.zip (717 Bytes)
out.docx (38.0 KB)