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.
@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:
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…
@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)
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)
@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.
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(...);
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,