Get Text Column Width & Resize Images in Word DOCX Document to Fit Non Equal Width Columns using C# .NET

When there is just a single column, we can use the PageSetup along with margins to find out if an image clips off the page and are able to scale it back down to fit on the page.

In the case of 2+ columns how do you determine which column an image is in to properly determine it’s resize value to fit inside the column when columns are not equal width?

example.zip (224.5 KB)

@HylandRendering,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-20432. We will further look into the details of this requirement and will keep you updated on the status of the linked issue.

@HylandRendering,

Regarding WORDSNET-20432, please check if the following code example is acceptable for you?

Document doc = new Document("E:\\Temp\\Example\\aspose.docx");

//get the picture shape
Shape shape = doc.GetChild(NodeType.Shape, 0, true) as Shape;

//get the picture location
doc.UpdatePageLayout();
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

Object renderObject = collector.GetEntity(shape);
enumerator.Current = renderObject;

//i take column's size and position from doc to simplify the example, 
//but all this info is accessible from the document model.

float firstColumnLeft = 72;
double firstColumnWidth = 216;
double secondColumnWidth = 144;

if (enumerator.Rectangle.Left == firstColumnLeft)
    shape.Width = firstColumnWidth;
else
    shape.Width = secondColumnWidth;

    doc.Save("E:\\Temp\\Example\\20.6.docx");

You just need to determine the position of Image (Shape) in Word document. Then you can use the above code and the PageSetup class that contains info about page size, margins, columns space, size and other useful properties. Hope, this helps.