Can't add table after Shape in word file

Dears,
I am facing issue with one of the word files which content is ended by Shape and i am trying to use below code to insert table after this shape but nothing added to the document below is smaple of document and code.

infile.docx (35.3 KB)

 var dummyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"wwwroot\images\auth\path.jpg");
 DocumentBuilder builder = new DocumentBuilder(doc);
 builder.MoveToDocumentEnd();
 builder.Font.Hidden = false;
 builder.CurrentParagraph.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;

 builder.InsertBreak(BreakType.ParagraphBreak);
 builder.CurrentParagraph.ParagraphFormat.ClearFormatting();
 builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
 builder.CurrentParagraph.ParagraphFormat.SpaceAfter = 0;
 builder.Font.Underline = Underline.None;

 Table table = builder.StartTable();
 builder.InsertCell();
 builder.EndRow();
 builder.EndTable();
 table.ClearBorders();
 
          
 table.LeftPadding = 0;
 table.RightPadding = 0;
 table.LeftIndent = -50;
 // Get the first cell of the table
 Cell cell = table.FirstRow.FirstCell;
 // Set the cell formatting
 cell.CellFormat.ClearFormatting();
 // Set the cell border to none
 cell.CellFormat.Borders.ClearFormatting();
 cell.CellFormat.Width = 290;
 

 builder.MoveTo(cell.LastParagraph);
 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
 cell.CellFormat.FitText = true;
 // Insert the image into the cell
 Shape imageShape = builder.InsertImage(dummyPath ); // Provide path to your image file

 double scalePercentage = 50; // 50% scale

 // Set the width and height of the image based on the scale percentage
 imageShape.Width = cell.CellFormat.Width * scalePercentage / 100;
 imageShape.Height = 45;


 imageShape.VerticalAlignment = VerticalAlignment.Top;
 imageShape.HorizontalAlignment = HorizontalAlignment.Center;
 builder.InsertBreak(BreakType.ParagraphBreak);
 var shape = builder.InsertShape(ShapeType.TextBox, 290, 44);
 shape.VerticalAlignment = VerticalAlignment.Top;
 shape.HorizontalAlignment = HorizontalAlignment.Center;
 shape.Stroked = false;
 shape.TextBox.FitShapeToText = true;
 shape.AllowOverlap = false;



 builder.MoveTo(shape.LastParagraph);
 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
 builder.ParagraphFormat.SpaceAfter = 0;
 builder.Font.Name = "Calibri (Body)";
 builder.Font.Size = 11;
 builder.Font.Bold = true;
 builder.Font.SizeBi = 11;
 builder.Font.BoldBi = true;
 builder.Font.Bidi = true;
 builder.Writeln("الأسم الأول و الأسم الأخير ");
 builder.ParagraphFormat.SpaceAfter = 0;
 builder.Writeln("المسمى ");
 builder.ParagraphFormat.SpaceAfter = 0;
 doc.AutomaticallyUpdateStyles = true;
 table.PreferredWidth = PreferredWidth.FromPoints(shape.Width);
 doc.UpdatePageLayout();
 doc.UpdateFields();

@atalal In your case the DocumentBuilder is moved into the shape. Please try modifying your code like this:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
// Check whether DocumentBuilder cusrsor is inside shape.
if (builder.CurrentParagraph.GetAncestor(NodeType.Shape) != null)
{
    // Add an empty paragraph at the end of the document and move to it.
    doc.LastSection.Body.AppendParagraph("");
    builder.MoveToDocumentEnd();
}

builder.Font.Hidden = false;
builder.CurrentParagraph.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;

builder.InsertBreak(BreakType.ParagraphBreak);
builder.CurrentParagraph.ParagraphFormat.ClearFormatting();
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.CurrentParagraph.ParagraphFormat.SpaceAfter = 0;
builder.Font.Underline = Underline.None;

Table table = builder.StartTable();
builder.InsertCell();
builder.EndRow();
builder.EndTable();
table.ClearBorders();

table.LeftPadding = 0;
table.RightPadding = 0;
table.LeftIndent = -50;
// Get the first cell of the table
Cell cell = table.FirstRow.FirstCell;
// Set the cell formatting
cell.CellFormat.ClearFormatting();
// Set the cell border to none
cell.CellFormat.Borders.ClearFormatting();
cell.CellFormat.Width = 290;

builder.MoveTo(cell.LastParagraph);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
cell.CellFormat.FitText = true;
// Insert the image into the cell
Shape imageShape = builder.InsertImage(@"C:\Temp\image.png"); // Provide path to your image file

double scalePercentage = 50; // 50% scale

// Set the width and height of the image based on the scale percentage
imageShape.Width = cell.CellFormat.Width * scalePercentage / 100;
imageShape.Height = 45;

imageShape.VerticalAlignment = VerticalAlignment.Top;
imageShape.HorizontalAlignment = HorizontalAlignment.Center;
builder.InsertBreak(BreakType.ParagraphBreak);
var shape = builder.InsertShape(ShapeType.TextBox, 290, 44);
shape.VerticalAlignment = VerticalAlignment.Top;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.Stroked = false;
shape.TextBox.FitShapeToText = true;
shape.AllowOverlap = false;

builder.MoveTo(shape.LastParagraph);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.ParagraphFormat.SpaceAfter = 0;
builder.Font.Name = "Calibri (Body)";
builder.Font.Size = 11;
builder.Font.Bold = true;
builder.Font.SizeBi = 11;
builder.Font.BoldBi = true;
builder.Font.Bidi = true;
builder.Writeln("الأسم الأول و الأسم الأخير ");
builder.ParagraphFormat.SpaceAfter = 0;
builder.Writeln("المسمى ");
builder.ParagraphFormat.SpaceAfter = 0;
doc.AutomaticallyUpdateStyles = true;
table.PreferredWidth = PreferredWidth.FromPoints(shape.Width);

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

Dear @alexey.noskov
Thanks for your quick response! Now i can see the table cell.
but it is shows up in same line with the shape we need to insert table after the last content all the time even it is a shape or no , what code shall i use to make sure table cell at the end of the document all the time if shape exist or no.

Regards.

@atalal Shape in your document is floating so it’s position does not affect the rest of content. If it is required to put the created table at the bottom of the page, you can also use floating table. For example see the following modified code:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
// Check whether DocumentBuilder cusrsor is inside shape.
if (builder.CurrentParagraph.GetAncestor(NodeType.Shape) != null)
{
    // Add an empty paragraph at the end of the document and move to it.
    doc.LastSection.Body.AppendParagraph("");
    builder.MoveToDocumentEnd();
}

builder.Font.Hidden = false;
builder.CurrentParagraph.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;

builder.InsertBreak(BreakType.ParagraphBreak);
builder.CurrentParagraph.ParagraphFormat.ClearFormatting();
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.CurrentParagraph.ParagraphFormat.SpaceAfter = 0;
builder.Font.Underline = Underline.None;

Table table = builder.StartTable();
builder.InsertCell();
builder.EndRow();
builder.EndTable();
table.ClearBorders();

table.LeftPadding = 0;
table.RightPadding = 0;
table.LeftIndent = -50;
// Get the first cell of the table
Cell cell = table.FirstRow.FirstCell;
// Set the cell formatting
cell.CellFormat.ClearFormatting();
// Set the cell border to none
cell.CellFormat.Borders.ClearFormatting();
cell.CellFormat.Width = 290;

builder.MoveTo(cell.LastParagraph);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
cell.CellFormat.FitText = true;
// Insert the image into the cell
Shape imageShape = builder.InsertImage(@"C:\Temp\image.png"); // Provide path to your image file

double scalePercentage = 50; // 50% scale

// Set the width and height of the image based on the scale percentage
imageShape.Width = cell.CellFormat.Width * scalePercentage / 100;
imageShape.Height = 45;

imageShape.VerticalAlignment = VerticalAlignment.Top;
imageShape.HorizontalAlignment = HorizontalAlignment.Center;
builder.InsertBreak(BreakType.ParagraphBreak);
var shape = builder.InsertShape(ShapeType.TextBox, 290, 44);
shape.VerticalAlignment = VerticalAlignment.Top;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.Stroked = false;
shape.TextBox.FitShapeToText = true;
shape.AllowOverlap = false;

builder.MoveTo(shape.LastParagraph);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.ParagraphFormat.SpaceAfter = 0;
builder.Font.Name = "Calibri (Body)";
builder.Font.Size = 11;
builder.Font.Bold = true;
builder.Font.SizeBi = 11;
builder.Font.BoldBi = true;
builder.Font.Bidi = true;
builder.Writeln("الأسم الأول و الأسم الأخير ");
builder.ParagraphFormat.SpaceAfter = 0;
builder.Writeln("المسمى ");
builder.ParagraphFormat.SpaceAfter = 0;
doc.AutomaticallyUpdateStyles = true;
table.PreferredWidth = PreferredWidth.FromPoints(shape.Width);

// Set floating position of the table
table.VerticalAnchor = RelativeVerticalPosition.Margin;
table.RelativeVerticalAlignment = VerticalAlignment.Bottom;
table.HorizontalAnchor = RelativeHorizontalPosition.Margin;
table.RelativeHorizontalAlignment = HorizontalAlignment.Left;

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

@alexey.noskov , if the content of the word is a lot and fill one page , table still in the same line of the shape , below is sample for word file has more content.
what code shall i use to make sure the table is under last content because some time table should be right side and sometime should be left side. so when it is right side the table will overlap with the shape.

infile.docx (35.8 KB)

@atalal Unfortunately, there is no easy way to achieve this with floating shapes. In your case it is required to calculate absolute position of the shape and add the required number of empty paragraphs to move the table after the floating shape.