PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Centering Image in Table Cell
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2046
Page 1 of 1

Author:  blue666 [ Wed Jun 06, 2012 11:57 pm ]
Post subject:  Centering Image in Table Cell

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
                          };
         
      }


Author:  () => true [ Thu Jun 07, 2012 8:11 am ]
Post subject:  Re: Centering Image in Table Cell

Hi!

Maybe it works if you set textFrame.WrapFormat.DistanceLeft instead of textFrame.Left.

It should work if you simply set the cell/the column to centre alignment without setting any left padding on the textframe.

But it can be done without textframe and with automatically centered image:
I use cell.AddImage to add images to a centered cell ("cell.Format.Alignment = ParagraphAlignment.Center;") and that works perfectly. The image also has a LineFormat property, so I presume you can achieve what you need without the TextFrame.

Author:  blue666 [ Thu Jun 07, 2012 6:40 pm ]
Post subject:  Re: Centering Image in Table Cell

I have tried just adding the image to a centered cell, but it renders as left aligned.
If I add the image to a paragraph and then put the paragraph in the same cell, it renders as centered.
I've got it working correctly in a different class, so I'm doing something wrong, but I can't figure out what.

() => true wrote:
But it can be done without textframe and with automatically centered image:
I use cell.AddImage to add images to a centered cell ("cell.Format.Alignment = ParagraphAlignment.Center;") and that works perfectly. The image also has a LineFormat property, so I presume you can achieve what you need without the TextFrame.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/