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

Free Support Forum - aspose.com

Bug: Images inside a table are being cut in pdf format

Hi,

I have a problem when inserting an image into a table cell using DocumentBuilder.InsertImage() and saving it in pdf format. When I do that, the table cell’s width does not adjust to fit the width of the image and the image is being cut. The table cell’s height, however, is adjusted correctly to fit the height of the image.
This happens although the DocumentBuilder.RowFormat.AllowAutoFit is true. When saving in Doc, Docx or Html formats, the cell auto fits the image. There’s only a problem in pdf format and only in the width of the cell.

I have found that setting the cell’s width manually like this:

Shape shape = builder.InsertImage(imageFileName);
builder.CellFormat.Width = shape.Width + builder.CellFormat.RightPadding + builder.CellFormat.LeftPadding;

solves the problem.
However, I wanted to do that only if there is a table so i added a check as follows:

Shape shape = builder.InsertImage(imageFileName);
if (builder.CellFormat != null)
{

builder.CellFormat.Width = shape.Width + builder.CellFormat.RightPadding + builder.CellFormat.LeftPadding;
}

and I noticed that builder.CellFormat is never null, even when there’s no cell, so I’m not sure what will be the effect if I change the cell’s width when there’s no cell.

My question are:

  1. Is there a better way to go around this problem?
  2. If not, what is this CellFormat the builder has when there’s no cell and what effect may be if I change its width anyway.
  3. Is this bug going to be fixed in next version?
Thanks,
Itai

Hi

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

Thanks for your inquiry. Could you please attach your input document and image here for testing? I will check the problem on my side and provide you more information.

Best regards,

Hi

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

Thanks for your inquiry. To check whether the image was inserted into a table cell you should use code like the following:

Shape shape = builder.InsertImage(imageFileName);

if (shape.GetAncestor(NodeType.Cell) != null)

{

builder.CellFormat.Width = shape.Width + builder.CellFormat.RightPadding + builder.CellFormat.LeftPadding;

}

Hope this helps.

Best regards,

Thanks for the quick reply. Your suggestion helps a lot.

I’ve attached an input document and an image file as per Andrey’s request. In order to reproduce this, insert the image in the place of the <span style=“font-size: 11pt; font-family: “Arial”,“sans-serif”;”>«Attachment Content» merge field.

Thanks,
Itai

Hi Itai,

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

Thank you for additional information. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is fixed.

Best regards,

Hi Daniel,

Just to follow up on what Michael has said, you can also set the size of the table cell to match the image if you are inserting the image during mail merge. Please see the code below which achieves this. It uses the MailMerge.FieldMergingCallback property.

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

Image image = Image.FromFile((string)args.FieldValue);

Cell cell = (Cell)args.Field.Start.GetAncestor(NodeType.Cell);

cell.CellFormat.Width = image.Width;

args.ImageFileName = (string)args.FieldValue;

}

Also please note, the method Michael was referring to is the Document.UpdateTableLayout method.

Thanks,