我需要将一张图片放置在表格单元格的中心位置。当我在单元格中添加图片时,图片会被左上角对齐。如何使图片居中对齐于单元格?
你可能需要在单元格中添加一个段落,设置段落的对齐方式,然后将图片添加到段落中。
row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].VerticalAlignment = VerticalAlignment.Center;
row.Cells[0].AddParagraph().AddImage(imageLocation);
row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].VerticalAlignment = VerticalAlignment.Center;
Format.Alignment
属性没有效果,即使图像位于段落内部也是如此。// Here we assume the "TableText" style has already been created
var sty = doc.Styles.AddStyle( "TableTextCenter", "TableText" );
sty.ParagraphFormat.Alignment = ParagraphAlignment.Center;
在我添加图片的那个点,我会这样做:
var para = table.Cells[ 0 ].AddParagraph();
para.Style = "TableTextCenter"; // <----- This is the magic!
var img = para.AddImage( imageFileSpec );
img.LockAspectRatio = true;
img.Width = "4cm";
img.WrapFormat = new WrapFormat {
Style = WrapStyle.Through
};
正如@Reuben所说,有趣的是:
row.Cells[0].AddParagraph().AddImage(imageLocation);
我正在尝试使用
row.Cells[0].AddImage(imageLocation);
图片已插入文档,但我无法使其居中。