Horizontal alignment - image in table cell

I’d like to center an image in a cell.

var tmpTable = new Table{
	Border = new BorderInfo(BorderSide.All, .5f, Color.FromRgb(System.Drawing.Color.LightGray)),
	DefaultColumnWidth = ((PageSize.A4.Height - 17) / 13).ToString(),
	DefaultCellPadding = new MarginInfo(2f, 2f, 0f, 2f),
};

var tmpImage = new Image{
	File = "/myimg",
	FixWidth = 76,
	FixHeight = 160,
	HorizontalAlignment = HorizontalAlignment.Center
};

var tmpRow = tmpTable.Rows.Add();
var tmpCell = tmpRow.Cells.Add();
tmpCell.ColSpan = 7;

tmpCell.DefaultCellTextState = new TextState(){
	HorizontalAlignment = HorizontalAlignment.Center
};
tmpCell.Paragraphs.Add(tmpImage);
tmpCell.Alignment = HorizontalAlignment.Center;
tmpPage.Paragraphs.Add(tmpTable);

The image is still left aligned.

@newwin-sws

Thank you for contacting support.

We have been able to reproduce the problem with HorzontalAlignment and a ticket with ID PDFNET-45526 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

However, we have devised a workaround to center align the image while setting cell margins. Please try using below code snippet in your environment and then share your kind feedback with us.

// Instantiate Document object
Document doc = new Document();
// Create an image instance
Aspose.Pdf.Image img = new Aspose.Pdf.Image();
// Path for source file
img.File = dataDir + "18.8issue.png";
// Set width for image instance
img.FixWidth = 50;
// Set height for image instance
img.FixHeight = 50;
// Create table instance
Aspose.Pdf.Table table = new Aspose.Pdf.Table();

table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));
// Set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));

// Set width for table cells
table.ColumnWidths = "100 100";
// Create row object and add it to table instance
Aspose.Pdf.Row row = table.Rows.Add();
// Create cell object and add it to row instance
Aspose.Pdf.Cell cell = row.Cells.Add();
// Add textfragment to paragraphs collection of cell object
cell.Paragraphs.Add(new TextFragment("First cell"));
// Add another cell to row object
cell = row.Cells.Add();

int width = System.Convert.ToInt32(table.DefaultColumnWidth);
cell.Margin = new MarginInfo(((System.Convert.ToInt32(table.DefaultColumnWidth) - img.FixWidth) / 2), 0, ((System.Convert.ToInt32(table.DefaultColumnWidth) - img.FixWidth) / 2), 0);
cell.Alignment = HorizontalAlignment.Center;
        
// Add image to paragraphs collection of recently added cell instance
cell.Paragraphs.Add(img);
// Create page object and add it to pages collection of document instance
Aspose.Pdf.Page page = doc.Pages.Add();
// Add table to paragraphs collection of page object
page.Paragraphs.Add(table);

dataDir = dataDir + "AddImage_18.10.pdf";
// Save PDF file
doc.Save(dataDir);

We are sorry for the inconvenience.

This still looks like it is broken.
Adding an image to a cell and then using
container.Rows[0].Cells[1].Alignment = HorizontalAlignment.Right;

Still ends up with a left aligned image

I found that this can be done, but you have to also set the Image horizontal alignment as well as the cells horizontal alignment (i…e if your cell had images and text)

This is still incorrect as ideally the cell alignment would align all child content

@R_C_D_T

We appreciate your worthy input over this.

We have noted down your findings and concerns under respective ticket and will inform here once the problem is resolved.