We are using below code and getting exception -
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
try
{
// Skip shapes that are textboxes — they can’t have floating layout or valid rectangle
if (s.ShapeType == ShapeType.TextBox || IsInsideTextBoxOrSimilar(s))
{
Logger.Debug("Skipping shape as its a textbox or similar");
continue;
}
// Move enumerator to the shape
var entity = layoutCollector.GetEntity(s);
if (entity == null)
{
Logger.Debug("Skipping shape as it has no layout entity. Might be a floating shape");
continue;
}
layoutEnumerator.Current = entity;
// Get rendered rectangle of shape (position and size on the page)
System.Drawing.RectangleF rect = layoutEnumerator.Rectangle;
PageSetup ps = ((Section)s.GetAncestor(NodeType.Section)).PageSetup;
//calculare max possible width
double usablePageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
// Now check if shape overflows the usable area
bool isTruncated = (rect.X + rect.Width) > usablePageWidth;
if (isTruncated)
{
//Shape.AspectRatioLocked property in Aspose.Words controls whether the width and height scale together proportionally.
Logger.Debug("Changing the shape's width based on available width to avoid truncation");
//if shape type is not inline then below setting width will fail
double availablePageWidth = usablePageWidth - rect.X;
if (availablePageWidth > 0)
{
s.AspectRatioLocked = true;
s.Width = usablePageWidth - rect.X;
}
}
}
catch (Exception ex)
{
string message = "Exception occured while resizing of shape :" + s.ShapeType + "and wrap type : " + s.WrapType + "and shape text - " + s.GetText();
Logger.Error(message + ex.StackTrace);
Console.WriteLine("Exception occured while resizing shapes and tables :" + ex.Message);
throw ex;
}
}
@hrnallap
Cause
The exception “Floating shape not allowed inside textbox” occurs because you are trying to add a floating shape (like an image or a drawing object) inside a textbox. Textboxes in Aspose.Words do not support floating shapes, which leads to this error.
Solution
To resolve this issue, you need to ensure that any shapes you are working with are not placed inside a textbox. You can modify your code to check if the shape is a textbox and skip any operations that involve adding floating shapes to it.
Code
Here’s a modified version of your code that includes a check to prevent adding floating shapes inside textboxes:
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
try
{
// Skip shapes that are textboxes
if (s.ShapeType == ShapeType.TextBox || IsInsideTextBoxOrSimilar(s))
{
Logger.Debug("Skipping shape as it's a textbox or similar");
continue;
}
// Move enumerator to the shape
var entity = layoutCollector.GetEntity(s);
if (entity == null)
{
Logger.Debug("Skipping shape as it has no layout entity. Might be a floating shape");
continue;
}
layoutEnumerator.Current = entity;
// Get rendered rectangle of shape (position and size on the page)
System.Drawing.RectangleF rect = layoutEnumerator.Rectangle;
PageSetup ps = ((Section)s.GetAncestor(NodeType.Section)).PageSetup;
// Calculate max possible width
double usablePageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
// Check if shape overflows the usable area
bool isTruncated = (rect.X + rect.Width) > usablePageWidth;
if (isTruncated)
{
Logger.Debug("Changing the shape's width based on available width to avoid truncation");
double availablePageWidth = usablePageWidth - rect.X;
if (availablePageWidth > 0)
{
s.AspectRatioLocked = true;
s.Width = availablePageWidth;
}
}
}
catch (Exception ex)
{
string message = $"Exception occurred while resizing shape: {s.ShapeType}, wrap type: {s.WrapType}, shape text: {s.GetText()}";
Logger.Error(message + ex.StackTrace);
Console.WriteLine("Exception occurred while resizing shapes and tables: " + ex.Message);
throw;
}
}
Next Steps
- Review your document structure to ensure that shapes are not being added inside textboxes.
- Test the modified code to confirm that the exception no longer occurs.
- If you still encounter issues, check the layout and positioning of your shapes to ensure they comply with the requirements of Aspose.Words.
Please implement these changes and let me know if you need further assistance!
@hrnallap Could you please provide a document where this problem can be reproduced?
You just replied with my same code and saying this is modified code. I dont get it.
@hrnallap Professionalize.Discourse generates incorrect answers in some cases. We apologize for this. Could you please provide a document that can reproduce this issue? We will look into it and help you.
I cant upload the document as its a large document. Where and how can I upload?
@hrnallap You can use Google Drive to share your document.
Attaching the problematic document. Please help us with solution as soon as you can. Currently below is the exception we are getting -
Exception occured while resizing shapes and tables :Floating shape not allowed inside textbox.
ProblematicShape_1 - Share with ASPOSE.docx (2.9 MB)
@hrnallap Shape that throws this exception located inside the GroupShape. You need to resize this object instead of shapes inside, so you need to pass GroupShape object to layoutCollector for if (shape.GetAncestor(NodeType.GroupShape) != null)
.
Does that mean Groupshape was null in this case? Attaching more refined document with just the problematic shape .
ProblematicShape Floating Exception.docx (497.2 KB)
@hrnallap No, the exception is thrown on shapes inside a GroupShape. So you need to check if the shape is inside a GroupShape, then get the GroupShape and continue working with it.
How do I pass retrieve the group shape and pass it to layout collector?
@hrnallap Here is the code:
GroupShape groupShape = (GroupShape)s.GetAncestor(NodeType.GroupShape);
if (groupShape != null)
entity = layoutCollector.GetEntity(groupShape);
else
entity = layoutCollector.GetEntity(s);
if (entity == null)
{
Logger.Debug("Skipping shape as it has no layout entity. Might be a floating shape");
continue;
}
if (layoutEnumerator.Current != entity)
layoutEnumerator.Current = entity;
The issue doesnt seem to be resolved. I am attaching the document i tried the code fix on. Below is the changed code-
// Initialize layout collector and enumerator
Aspose.Words.Layout.LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
object entity = null;
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
try
{
GroupShape groupShape = (GroupShape)s.GetAncestor(NodeType.GroupShape);
if (groupShape != null)
entity = layoutCollector.GetEntity(groupShape);
else
entity = layoutCollector.GetEntity(s);
//since we are already fixing shapes within table , we dont need to fix these shapes that are within table
if (s.GetAncestor(NodeType.Table) != null)
continue;
// Skip shapes that are textboxes — they can’t have floating layout or valid rectangle
if (!s.IsInline && IsInsideTextBox(s))
{
Logger.Debug("Skipping shape as its a textbox or similar");
continue;
}
if (entity == null)
{
Logger.Debug("Skipping shape as it has no layout entity. Might be a floating shape");
continue;
}
s.WrapType = WrapType.Inline;
layoutEnumerator.Current = entity;
// Get rendered rectangle of shape (position and size on the page)
System.Drawing.RectangleF rect = layoutEnumerator.Rectangle;
PageSetup ps = ((Section)s.GetAncestor(NodeType.Section)).PageSetup;
//calculare max possible width
double usablePageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
// Now check if shape overflows the usable area
bool isTruncated = (rect.X + rect.Width) > usablePageWidth;
if (isTruncated)
{
//Shape.AspectRatioLocked property in Aspose.Words controls whether the width and height scale together proportionally.
Logger.Debug("Changing the shape's width based on available width to avoid truncation");
double availablePageWidth = usablePageWidth - rect.X;
if (availablePageWidth > 0)
{
s.AspectRatioLocked = true;
s.Width = usablePageWidth - rect.X;
}
}
}
catch (Exception ex)
{
string message = "Exception occured while resizing of shape :" + s.ShapeType + "and wrap type : " + s.WrapType + "and shape text - " + s.GetText();
Logger.Error(message + ex.StackTrace);
Console.WriteLine("Exception occured while resizing shapes and tables :" + ex.Message);
throw ex;
}
}
ProblematicShape Floating Exception.docx (497.2 KB)
@hrnallap There is another GroupShape object inside the GroupShape object. It should be skipped.
GroupShape groupShape = (GroupShape)s.GetAncestor(NodeType.GroupShape);
if (groupShape.GetAncestor(NodeType.GroupShape) != null)
continue;
if (groupShape != null)
entity = layoutCollector.GetEntity(groupShape);
else
entity = layoutCollector.GetEntity(s);
You should also work with groupShape
instead of Shape s
if a GroupShape object exists.
Perfect this resolved my issue. Thank you so much for your help. Your support team is great 
I will try this fix on all our docs.
1 Like
@hrnallap It would be more useful to change
if (groupShape.GetAncestor(NodeType.GroupShape) != null)
continue;
to
if (!groupShape.IsTopLevel)
continue;
to make sure it’s the right shape.
Sure and I am also checking if group shape is null before checking this. Else it was throwing NUll pointer exception.

1 Like