PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Tue Nov 04, 2025 8:24 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jun 06, 2012 11:57 pm 
Offline

Joined: Wed May 09, 2012 10:06 pm
Posts: 5
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
                          };
         
      }



Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 07, 2012 8:11 am 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 355
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.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 07, 2012 6:40 pm 
Offline

Joined: Wed May 09, 2012 10:06 pm
Posts: 5
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider], Google Adsense [Bot] and 250 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group