I have a bunch of photos to add to pdf. Using MigraDoc, I am having a very difficult time getting the image to center in the cell. In my current example the textFrame.Left is generally supposed to be around 25, however, when I render it to pdf, it's right on the edge of the cell. Can somebody help me figure out what I'm doing wrong?
Here is the code I am using.
Code:
private void AddPhotos(Section section) {
Photos=new List<Photo>();
foreach (var photo in _photoPaths.Select(photoPath => new Photo(photoPath)).Where(photo => photo.Size.Width > 0 && photo.Size.Height > 0)) {
Photos.Add(photo);
}
Photos = Photos.OrderBy(a => a.Size.Width).ToList();
var photosIndex=0;
while (photosIndex < Photos.Count) {
var currentRowPhotos = new List<Photo>();
float totalColumnsWidth = 0;
var table = section.AddTable();
table.SetupTable();
while (totalColumnsWidth < _tableWidth && photosIndex < Photos.Count) {
var photo = Photos[photosIndex];
if (totalColumnsWidth + photo.Size.Width > _tableWidth) { break; }
currentRowPhotos.Add(photo);
totalColumnsWidth += photo.Size.Width;
photosIndex++;
table.AddColumn(photo.Size.Width);
}
if (totalColumnsWidth < _tableWidth) {
var amountToAddToEachColumn = (_tableWidth - totalColumnsWidth)/currentRowPhotos.Count;
foreach (Column column in table.Columns) { column.Width += amountToAddToEachColumn; }
}
var row = table.AddRow();
for (var index = 0; index < currentRowPhotos.Count; index++) {
var photo = currentRowPhotos[index];
var cell = row.Cells[index];
var textFrame = GetTextFrame(photo);
var textFrameLeft = (cell.Column.Width / 2) - (textFrame.Width / 2);
textFrame.Left = textFrameLeft;
var image = textFrame.AddImage(photo.ImagePath);
image.Width = photo.Size.Width;
image.Height = photo.Size.Height;
cell.Add(textFrame);
}
}
}
private static TextFrame GetTextFrame(Photo photo) {
var lineFormat = new LineFormat { DashStyle = DashStyle.Solid, Color = Colors.Black };
return new TextFrame {
Width = photo.Size.Width + 4,
Height = photo.Size.Height + 4,
MarginBottom = 2,
MarginLeft = 2,
MarginRight = 2,
MarginTop = 2,
LineFormat = lineFormat
};
}