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

Free Support Forum - aspose.com

Layout of picture in a table - Word

Hi,

I have a inserted a picture within a table cell using Aspose.Word, and am trying to replicate the same functionality I have in Word 2013 to centre the picture.

I can do what I need to do in Word by selecting:

Picture Tools > Format > Position

and selecting the option “Position in Middle Center With Square Text Wrapping”

Is there a way of setting this option within Aspose?

Thanks

Hi Paresh,


Thanks for your inquiry. Yes, you can achieve your requirements using Aspose.Words. Image is imported into Shape node in Aspose.Words DOM. Please use Shape.WrapType as WrapType.Square and set shape’s position using Shape.Left and Shape.Top properties. Hope this helps you.

Hi,

Thanks for your speedy reply!

I am already setting the wrap type which is good. As for the top and left properties are you saying I need to specify these exact values?

I was hoping the value would be worked out automatically depending on the size of the table cell as this could change depending on how big the image is.

Thanks

Hi Paresh,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does.

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

Thanks again for your reply and help in this matter.

I have uploaded a sample document.

The image within the first table has been manually centred by me within word (this is the desired outcome), but the second picture titled "Example Sketch" has been inserted and formatted programmatically using Aspose
Hi Paresh,

Thanks for your inquiry. Please use the following code example to set the image position as shown in first table.

Document doc = new Document(MyDir + "aspose_centre_picture_issue.doc");

Table table = (Table)doc.GetChild(NodeType.Table, 1, true);

Shape shape = (Shape)table.GetChild(NodeType.Shape, 0, true);

shape.WrapType = WrapType.Square;

shape.Left = 5;

shape.Top = 5;

shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Column;

shape.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;

doc.Save(MyDir + "Out.docx");


You can also set the image position in the middle of cell using following code example. Hope this helps you.

Document doc = new Document(MyDir + "aspose_centre_picture_issue.doc");

Table table = (Table)doc.GetChild(NodeType.Table, 1, true);

Shape shape = (Shape)table.GetChild(NodeType.Shape, 0, true);

shape.WrapType = WrapType.Inline;

Cell cell = (Cell)shape.GetAncestor(NodeType.Cell);

cell.FirstParagraph.ParagraphFormat.ClearFormatting();

shape.ParentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;

cell.CellFormat.ClearFormatting();

cell.CellFormat.LeftPadding = 10;

cell.CellFormat.TopPadding = 10;

cell.CellFormat.BottomPadding = 10;

cell.CellFormat.RightPadding = 10;

doc.Save(MyDir + "Out.docx");

Hi Tahir,

That is working as we wanted now. Thank you very much for your help!

Hi Paresh,

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