We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Resize shape size in table's cell without changing table cell width using C#

hi team,
I run into a problem of how to get the shape position in table cell, i can find the settings of the absolute position in word UI component, instruction.png (87.2 KB)
but not found the valid API in aspose.words.dll, so sorry, ask for your help! thanks!
the bellow is my used document
test_modify.zip (84.7 KB)

@TommyZhou

The shape inside the table has wrap type inline. Its absolute position is not set. However, you can find the shape’s position using layout API. The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

Following code example shows how to get the position of shape from the page’s top left corner. Hope this helps you.

Document doc = new Document(MyDir + "test_modify.docx");
Table table = (Table)doc.Sections[1].Body.GetChild(NodeType.Table, 1, true);
Shape shape = (Shape)table.GetChild(NodeType.Shape, 0, true);

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

enumerator.Current = collector.GetEntity(shape);
Console.WriteLine(" --> Left : " + enumerator.Rectangle.Left + " Top : " + enumerator.Rectangle.Top);

Thanks for your reply, but sorry, it’s not my expected.
On the other hand, i wonder that there is a way to resize the content of the cell and not change the cell’s width, for the normal content is OK, i have confirmed, but for my document, the cell contains a numbered list , one of the numbered list contain a picture, because for some reason i have to set the cell fixed width, the picture out range of the cell, e.g. outrange.png (67.5 KB)
so, is there a way to resize the cell content when cell content contains one or more node(e.g. shape, numbered list ect.) base on not to change the cell’s fixed width.
The below is my test document
test_modify.zip (84.7 KB)

Seek your help and proposal, thks.

@TommyZhou

Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

The bellow is my expected document, the picture can auto fit the cell and not change the cell’s widthautoFitCell.png (111.9 KB)

expected.zip (91.3 KB)

@TommyZhou

Thanks for sharing the detail. We are investigating this issue and will get back to you soon.

@TommyZhou

Please use the following code example to resize the shapes in the table’s cell. Hope this helps you.

Document doc = new Document(MyDir + "test_modify.docx");
Table table = doc.Sections[1].Body.Tables[1];

foreach (Shape shape in table.GetChildNodes(NodeType.Shape, true))
{
    double cellwidth = table.FirstRow.FirstCell.CellFormat.Width;

    if (cellwidth < shape.ParentParagraph.ParagraphFormat.LeftIndent + shape.Width)
    {
        double difference = shape.ParentParagraph.ParagraphFormat.LeftIndent + shape.Width - cellwidth;
        shape.Width = shape.Width - difference;
    }
}

It’s work for me, thks!

@TommyZhou

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