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

Free Support Forum - aspose.com

How to resize image

Hi,

I want to read all the images in a document, and identifying if any image is having more than 3" width and 5" height, then need to resize the image as 3" width and 5" height.
Could you please suggest me to achieve this?

Thanks,

Hi there,

Sure, you can use the code from this documentation page here as a basis to retrieve the image data from all images in a document.

You should use the ConvertUtil methods to check the image size/specify the new image size. e.g to check if the image is 3 inches you can call shape.Width on the shape containing your image. This will return the width of image in points. You can then use the ConvertUtil.PointToInch method on the width to convert it to inches and check if the returned value is > 3. the code from this thread can then be implemented to resize your image.

Thanks,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can just loop over all shapes in your document and check size of each shape. Code will look like the following:

// Open document.

Document doc = new Document(@"in.doc");

// Get all shapes.

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// Loop over all shapes.

foreach (Shape shape in shapes)

{

if(shape.ShapeType == ShapeType.Image)

{

// Here you should check size of the shape and resize it if necessary.

if (shape.Width > 100 && shape.Height > 100)

{

// Resize the shape.

}

}

}

Best regards,

Hi,

Thanks for your response. If the table cells having images, then how to identify the width/height of the cells which are having images to resize image to cell area.

Thanks,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can use CellFormat.Width and RowFormat.Height to get size of the cell. But you should note that call can have variable width and height. For example, cells usually do not have fixed height and grows vertically to fit content.

Best regards.

Hi Alexey,

Thanks for your response. I am able to get the width of the Cell, but Cell having another Table as a cell content and that table doesn’t fit with in the cell’s width.

I can resize the images in the document by identifying both image width and cell width using this statements:

NodeCollection cells = oSrc.GetChildNodes(NodeType.Cell, true);
foreach (Aspose.Words.Tables.Cell oCell in cells)
{
oCell.CellFormat.FitText = true;
NodeCollection shapes = oCell.GetChildNodes(NodeType.Shape, true);
foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
shape.Width = oCell.CellFormat.Width;
}
}

In the similar way how can I fit the tables too inside cell’s width?
PFA for my test document.

Thanks,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. There is no way to set width of the whole table using Aspose.Words. You can set width of cells only. So in your case, you can decrease width of each cell in the table in order to decrease width of the whole table.

Best regards,

Hi Alexey,

Thanks for your response. I don’t want to set the width of the whole table. I need only fit the content inside the Cell’s with, but content could be any of normal text/ image/ table.

In my previous post attachment, 3rd and 4th columns of 1.3 having normal text and fitted within the Cell’s width and the images in 3rd and 4th columns of 1.1 can fit with my previous post code. But 5th column in 1.1 and 1.2 having tables as Cell’s data and they were not fitted within Cell’s area (wrapped).

So how can I fit the 5th (Actual Results) column’s data inside Cell’s area?

Thanks,

Hi Srinu,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Please try using the following code:

Document doc = new Document(@"Test001\in.doc");

Node[] tables = doc.GetChildNodes(NodeType.Table, true).ToArray();

foreach (Table table in tables)

{

if(table.ParentNode.NodeType == NodeType.Cell)

{

Cell parentCell = (Cell)table.ParentNode;

// Create new table. We need to recreate the table to reset prefered width.

Table newTable = new Table(doc);

// Get width of the container (Cell).

double containerWidth = parentCell.CellFormat.Width - parentCell.CellFormat.LeftPadding - parentCell.CellFormat.RightPadding;

// Calculate width of each row in the table.

foreach (Row row in table.Rows)

{

double rowWidth = 0;

foreach (Cell cell in row.Cells)

rowWidth += cell.CellFormat.Width;

newTable.Rows.Add(new Row(doc));

// For each cell calculate width in percents and recalculate width of cell to fit the container.

foreach (Cell cell in row.Cells)

{

double ratio = cell.CellFormat.Width/rowWidth;

cell.CellFormat.Width = containerWidth*ratio;

cell.CellFormat.WrapText = true;

newTable.LastRow.AppendChild(cell);

}

}

table.ParentNode.InsertAfter(newTable, table);

table.Remove();

}

}

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards.