PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Resize table column to fit image width
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3733
Page 1 of 1

Author:  IDE-Lenneper [ Tue Feb 20, 2018 1:30 pm ]
Post subject:  Resize table column to fit image width

Hello,

I'm trying to read Images from a file, load them into a table, resize the images so they all have the same height and then resize the column width to fit the resized image.

Actually everything works. I load a file place it in a table and I can resize the column width. Works fine with text too, but after reading the Image I need to get the image width (Col.Width = Img.width + 2), but the Image.Width and Height are Empty.
I dont understand why.

Code:
                public static Paragraph CellParaImg(MigraDoc.DocumentObjectModel.Tables.Table Tbl, int RowNr, int CellNr, String ImageLocation, int ScaleHeight)
                {
                    Paragraph ret = Tbl.Rows[RowNr].Cells[CellNr].AddParagraph();

                    MigraDoc.DocumentObjectModel.Shapes.Image img = ret.AddImage(ImageLocation);
                    img.LockAspectRatio = true;
                    img.Height = Unit.FromPoint(ScaleHeight);

                    if (!img.Width.IsEmpty)
                        Tbl.Columns[CellNr].Width = img.Width + 2;
                    else
                        Tbl.Columns[CellNr].Width = 10;

                    Tbl.Rows[RowNr].BottomPadding = 0;
                    Tbl.Rows[RowNr].TopPadding = 0;


                    return ret;
                }


Attached are the files I loaded for the example with different widths.
I'm using MigraDoc 1.31

Attachments:
File comment: Inserted file 2 (Size 60 x 60 Pixel)
ip5x.png
ip5x.png [ 2.7 KiB | Viewed 8305 times ]
File comment: Inserted file 2 (Size 135 x 60 Pixel)
ipx5.png
ipx5.png [ 4.64 KiB | Viewed 8305 times ]
File comment: Result of the programming
result_of_programming.png
result_of_programming.png [ 9.01 KiB | Viewed 8305 times ]

Author:  Thomas Hoevel [ Tue Feb 20, 2018 1:42 pm ]
Post subject:  Re: Resize table column to fit image width

Hi!

Use PDFsharp's XImage class to open the image and query its size.

With the Image class you can optionally set the size of the image for display in the file. The properties are all null by default.

Author:  IDE-Lenneper [ Wed Feb 21, 2018 6:50 am ]
Post subject:  Re: Resize table column to fit image width

Thanks for the hint.
That gave me a solution for the problem. For others here the code I used.
Code:
                public static Paragraph CellParaImg(MigraDoc.DocumentObjectModel.Tables.Table Tbl, int RowNr, int CellNr, String ImageLocation, int ScaleHeight)
                {
                    Paragraph ret = Tbl.Rows[RowNr].Cells[CellNr].AddParagraph();

                    MigraDoc.DocumentObjectModel.Shapes.Image img = ret.AddImage(ImageLocation);
                    img.LockAspectRatio = true;
                    img.Height = Unit.FromPoint(ScaleHeight);

                    XImage image = XImage.FromFile(ImageLocation);
                    Tbl.Columns[CellNr].Width = (Int32)Math.Round((15 / (Double)image.PixelHeight * (Double)image.PixelWidth)) + 2;

                    return ret;
                }

Author:  Thomas Hoevel [ Wed Feb 21, 2018 9:07 am ]
Post subject:  Re: Resize table column to fit image width

Thanks for the feedback and the code snippet.

XImage implements IDisposable, so I'd use "using" with it:
Code:
                public static Paragraph CellParaImg(MigraDoc.DocumentObjectModel.Tables.Table Tbl, int RowNr, int CellNr, String ImageLocation, int ScaleHeight)
                {
                    Paragraph ret = Tbl.Rows[RowNr].Cells[CellNr].AddParagraph();

                    MigraDoc.DocumentObjectModel.Shapes.Image img = ret.AddImage(ImageLocation);
                    img.LockAspectRatio = true;
                    img.Height = Unit.FromPoint(ScaleHeight);

                    using (XImage image = XImage.FromFile(ImageLocation))
                        Tbl.Columns[CellNr].Width = (Int32)Math.Round((15 / (Double)image.PixelHeight * (Double)image.PixelWidth)) + 2;

                    return ret;
                }
Your factor "15" works for your images, but other users with different images or different requirements may need a different factor.

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