Align All images from left in a doc cell c#

Hello Team,
I am trying to put the images into a cell , all to be aligned from left. But unfortunately this dosen’t help.
image.png (124.9 KB)

here i get an space, i am not sure why.
here is the code block where i am putting images into the cell.

   if (siteAssessment.Images.Count > 0)
                {
                    docBuilder.CellFormat.ClearFormatting();
                    docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100);
                    docBuilder.InsertCell();
                    docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
                    docBuilder.Font.Bold = true;
                    docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
                    docBuilder.CellFormat.Shading.BackgroundPatternColor = Color.White;
                    docBuilder.Write("Photos:");
                    InsertPaddingRow(docBuilder, 1, numberOfCols);
                    docBuilder.EndRow();

                    docBuilder.InsertCell();
                    docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
                    docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single;
                    docBuilder.CellFormat.Borders.Top.LineStyle = LineStyle.None;
                    docBuilder.StartTable();
                    docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
                    docBuilder.CellFormat.Borders.Top.LineStyle = LineStyle.None;
                    docBuilder.CellFormat.Borders.Left.LineStyle = LineStyle.None;
                    docBuilder.CellFormat.Borders.Right.LineStyle = LineStyle.None;
                    docBuilder.CellFormat.BottomPadding = 0;
                    docBuilder.CellFormat.TopPadding = 0;
                    docBuilder.CellFormat.LeftPadding = 0;
                    docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                    docBuilder.CellFormat.RightPadding = 0;

                    for (int i = 0; i < siteAssessment.Images.Count; i++)
                    {

                        var image = ImageUtility.ResizeImage(new Bitmap(new MemoryStream(siteAssessment.Images[i])), 210, 400);
                        Shape shape = new Shape(docBuilder.Document, ShapeType.Image);
                        shape.ImageData.SetImage(image);
                        shape.Top = 0;
                        shape.Left = 0;
                   
                        shape.WrapType = WrapType.Inline;
                      shape.HorizontalAlignment = HorizontalAlignment.Left;
                        shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Default;
                        shape.RelativeVerticalPosition = RelativeVerticalPosition.TextFrameDefault;
                        docBuilder.InsertCell();
                        docBuilder.InsertNode(shape);
                        // Set borders for a shape.
                        shape.ImageData.Borders.Color = Color.White;
                        shape.ImageData.Borders.LineStyle = LineStyle.Single;
                        shape.ImageData.Borders.LineWidth = 2;
                    }
                    docBuilder.EndTable();
                    InsertPaddingRow(docBuilder, 1, numberOfCols);
                    docBuilder.EndRow();
                }

===================================================================================
Please find the zip to get the generated doc.doc.zip (183.6 KB)

Thanks

@Gauravmiri,

Thanks for your inquiry. You are inserting the images in the nested table. Please get the parent Cell node of table and set its left and right padding to 0. Please check following modified code between comments. Hope this helps you.

docBuilder.CellFormat.ClearFormatting();
docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100);
docBuilder.InsertCell();
docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
docBuilder.Font.Bold = true;
docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
docBuilder.CellFormat.Shading.BackgroundPatternColor = Color.White;
docBuilder.Write("Photos:");
InsertPaddingRow(docBuilder, 1, numberOfCols);
docBuilder.EndRow();


//Modified code start
Cell cell = docBuilder.InsertCell();
cell.CellFormat.LeftPadding = 0;
cell.CellFormat.RightPadding = 0;
//Modified code end


docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single;
docBuilder.CellFormat.Borders.Top.LineStyle = LineStyle.None;
docBuilder.StartTable();
docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
docBuilder.CellFormat.Borders.Top.LineStyle = LineStyle.None;
docBuilder.CellFormat.Borders.Left.LineStyle = LineStyle.None;
docBuilder.CellFormat.Borders.Right.LineStyle = LineStyle.None;
docBuilder.CellFormat.BottomPadding = 0;
docBuilder.CellFormat.TopPadding = 0;
docBuilder.CellFormat.LeftPadding = 0;
docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
docBuilder.CellFormat.RightPadding = 0;

@tahir.manzoor
Thanks for the response. but this did not work for me anymore.
I am getting the the same doc as before.

I have managed to set alignment but could not implement the property to set margin between images.
See the screenshot. image.jpg (106.6 KB)
i tried this but didnt work.
var image = ImageUtility.ResizeImage(new Bitmap(new MemoryStream(siteAssessment.Images[i])), 210, 400);

                        Shape shape = new Shape(docBuilder.Document, ShapeType.Image);
                        shape.ImageData.SetImage(image);
                        shape.DistanceLeft = 4;
                        shape.DistanceRight = 4;
                        shape.WrapType = WrapType.Inline;
                        shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Default;
                        shape.RelativeVerticalPosition = RelativeVerticalPosition.TextFrameDefault;
                        docBuilder.InsertNode(shape);

=======================================================================
@tahir.manzoor Do you know how to set margin to each image ?
Thanks

@Gauravmiri,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Thanks @tahir.manzoor.
Here is the source code of that module…

public static void CreateSectionCTable(DocumentBuilder docBuilder,
                                                string sectionName,
                                                List<FsaWordModel> siteAssessments)
        {
            //Get the number of columns per row
            var numberOfCols = 2; //siteAssessments.Select(a => a.Images.Count).Max();
            if (numberOfCols < 2)
            {
                numberOfCols = 2;
            }
            var table = docBuilder.StartTable();
            docBuilder.RowFormat.AllowBreakAcrossPages = false;
            docBuilder.RowFormat.HeadingFormat = true;
            docBuilder.InsertCell();
            table.AutoFit(AutoFitBehavior.AutoFitToWindow);
            Stylings.DefaultBorder(docBuilder);        
            table.PreferredWidth = PreferredWidth.FromPercent(100);
            docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
            docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            docBuilder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
            docBuilder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#D9D9D9"); 
            docBuilder.Font.Bold = true;
            docBuilder.Font.Name = Stylings.TITLEFONT;
            docBuilder.Font.Size = Stylings.TITLESIZE1;
            docBuilder.Font.Color = Color.Black;
           
            docBuilder.ParagraphFormat.LeftIndent = 5.0;
            docBuilder.ParagraphFormat.SpaceAfter = 1;
            docBuilder.ParagraphFormat.SpaceBefore = 1;
            docBuilder.Write(sectionName);
            InsertPaddingRow(docBuilder, 1, numberOfCols);
            docBuilder.EndRow();

            foreach (var siteAssessment in siteAssessments)
            {
                //if its NO then we show everything 
                //if its YES we only show the title and photos(if there are photos) 
                //if its N / A we just show the titel and N / A and 
                //if its HIDE we dont show at all on docx
                //
              
                docBuilder.InsertCell();
                //Clear Center, It should only be centered for STATUS
                docBuilder.CellFormat.ClearFormatting();
                docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
                docBuilder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
                docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                docBuilder.ParagraphFormat.LeftIndent = 0;
                docBuilder.ParagraphFormat.SpaceAfter = 1;
                docBuilder.ParagraphFormat.SpaceBefore = 1;             
                docBuilder.Font.Name = Stylings.NORMALFONT;
                docBuilder.Font.Size = Stylings.NORMALSIZE1;
                docBuilder.Font.Bold = false;
                docBuilder.CellFormat.Shading.BackgroundPatternColor = Color.White;
                docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(80);
                docBuilder.Font.Color = System.Drawing.Color.Black;
                docBuilder.Write(string.Format("{0}) {1}", siteAssessment.ItemNumber, siteAssessment.ItemTitle));
                InsertPaddingRow(docBuilder, 2, numberOfCols);

                docBuilder.InsertCell();
                docBuilder.ParagraphFormat.ClearFormatting();
                //Reset Formating and centre status column
                docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                docBuilder.CellFormat.HorizontalMerge = CellMerge.None;
                docBuilder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
                docBuilder.Font.Bold = true;
                docBuilder.ParagraphFormat.SpaceAfter = 1;
                docBuilder.ParagraphFormat.SpaceBefore = 1;
                docBuilder.CellFormat.Width = 20;
                if (siteAssessment.Status.ToLower() == "no")
                {             
                    docBuilder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#D99594");
                    docBuilder.Font.Color = System.Drawing.Color.Black;
                }
                else
                {
                    docBuilder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.ColorTranslator.FromHtml("#255077");
                    docBuilder.Font.Color = System.Drawing.Color.White;
                }
                docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(20);
                docBuilder.Write(siteAssessment.Status.ToUpper());
                docBuilder.EndRow();
                //only show observation and recommendation, if no
                if (siteAssessment.Status.ToLower() == "no")
                {
                    docBuilder.InsertCell();
                    //Clear Center, It should only be centered for STATUS
                    docBuilder.CellFormat.ClearFormatting();
                    docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                    docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100);
                    docBuilder.CellFormat.Shading.BackgroundPatternColor = Color.White;
                    docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
                    docBuilder.Font.Bold = true;
                    docBuilder.Font.Color = System.Drawing.Color.Black;
                   
                    docBuilder.ParagraphFormat.SpaceAfter = 1;
                    docBuilder.ParagraphFormat.SpaceBefore = 1;
                    docBuilder.Write("Observation: ");
                    docBuilder.Font.Bold = false;
                    if (siteAssessment.Observation != null)
                    {
                        docBuilder.Write(siteAssessment.Observation);
                    }
                    InsertPaddingRow(docBuilder, 1, numberOfCols);
                    docBuilder.EndRow();


                    docBuilder.InsertCell();
                    docBuilder.ParagraphFormat.ClearFormatting();
                    docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100);
                    docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
                    docBuilder.ParagraphFormat.SpaceAfter = 1;
                    docBuilder.ParagraphFormat.SpaceBefore = 1;
                    docBuilder.Font.Bold = true;
                    docBuilder.Write("Recommendation: ");
                    docBuilder.Font.Bold = false;
                    if (siteAssessment.Recommendation != null)
                    {
                        docBuilder.Write(siteAssessment.Recommendation);
                    }
                    InsertPaddingRow(docBuilder, 1, numberOfCols);
                    docBuilder.EndRow();
                }

                if (siteAssessment.Status.ToLower() == "no" || siteAssessment.Status.ToLower() == "yes")
                {
                    //Add Images
                    if (siteAssessment.Images.Count > 0)
                    {
                        docBuilder.CellFormat.ClearFormatting();
                        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100);
                        docBuilder.InsertCell();
                        docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
                        docBuilder.Font.Bold = true;
                        docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
                        docBuilder.CellFormat.Shading.BackgroundPatternColor = Color.White;
                        docBuilder.Write("Photos:");
                        InsertPaddingRow(docBuilder, 1, numberOfCols);
                        docBuilder.EndRow();

                        docBuilder.InsertCell();
                        docBuilder.CellFormat.HorizontalMerge = CellMerge.First;
                        docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single;
                        docBuilder.CellFormat.Borders.Top.LineStyle = LineStyle.None;
                        docBuilder.StartTable();
                        docBuilder.InsertCell();
                        docBuilder.CellFormat.Borders.Bottom.LineStyle = LineStyle.None;
                        docBuilder.CellFormat.Borders.Top.LineStyle = LineStyle.None;
                        docBuilder.CellFormat.Borders.Left.LineStyle = LineStyle.None;
                        docBuilder.CellFormat.Borders.Right.LineStyle = LineStyle.None;
                        docBuilder.CellFormat.BottomPadding = 0;
                        docBuilder.CellFormat.TopPadding = 0;
                        docBuilder.CellFormat.LeftPadding = 0;
                        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                        docBuilder.CellFormat.RightPadding = 0;

                        for (int i = 0; i < siteAssessment.Images.Count; i++)
                        {

                            var image = ImageUtility.ResizeImage(new Bitmap(new MemoryStream(siteAssessment.Images[i])), 210, 400);
                            Shape shape = new Shape(docBuilder.Document, ShapeType.Image);
                            shape.ImageData.SetImage(image);
                            shape.Top = 0;
                            shape.Left = 0;
                       
                            shape.WrapType = WrapType.Inline;
                            shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Default;
                            shape.RelativeVerticalPosition = RelativeVerticalPosition.TextFrameDefault;
                           
                            docBuilder.InsertNode(shape);
                            // Set borders for a shape.
                            shape.ImageData.Borders.Color = Color.White;
                            shape.ImageData.Borders.LineStyle = LineStyle.Single;
                            shape.ImageData.Borders.LineWidth = 2;
                        }
                        docBuilder.EndTable();
                        InsertPaddingRow(docBuilder, 1, numberOfCols);
                        docBuilder.EndRow();
                    }
                }
            }
            docBuilder.EndTable();
            docBuilder.ParagraphFormat.KeepWithNext = true;
        }

PLease find the attachment as well.
.demo.zip (183.9 KB)
Thank you

@Gauravmiri,

Thanks for your inquiry.

Have you tried the above modified code? This will fix the issue that you are facing.

This code contains compilation errors. Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

@tahir.manzoor Thank you for the response. :slight_smile:
Yes I tried this but it breaks the layout of doc , instead of fixing image alignment.
So it did not work for me.
Could you tell me how to add left margin(say 5px) to each image inside this loop.???
If i could add a margin inside this loop. i think it would get solved then .

for (int i = 0; i < siteAssessment.Images.Count; i++)
               {
                            var image = ImageUtility.ResizeImage(new Bitmap(new MemoryStream(siteAssessment.Images[i])), 210, 400);
                            Shape shape = new Shape(docBuilder.Document, ShapeType.Image);
                            shape.ImageData.SetImage(image);
                            shape.Top = 0;
                            shape.Left = 0;
                       
                            shape.WrapType = WrapType.Inline;
                            shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Default;
                            shape.RelativeVerticalPosition = RelativeVerticalPosition.TextFrameDefault;
                           
                            docBuilder.InsertNode(shape);
                            // Set borders for a shape.
                            shape.ImageData.Borders.Color = Color.White;
                            shape.ImageData.Borders.LineStyle = LineStyle.Single;
                            shape.ImageData.Borders.LineWidth = 2;
                        }

@Gauravmiri,

Thanks for your inquiry. You are inserting the images in table’s cell. We suggest you please insert each image in separate cell and set left padding of cell using CellFormat.LeftPadding property.

@tahir.manzoor Thank you.
This worked for me. :slight_smile:

@Gauravmiri,

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