PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Nov 10, 2024 5:52 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Feb 20, 2018 1:30 pm 
Offline

Joined: Tue Jan 30, 2018 10:25 am
Posts: 4
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 8332 times ]
File comment: Inserted file 2 (Size 135 x 60 Pixel)
ipx5.png
ipx5.png [ 4.64 KiB | Viewed 8332 times ]
File comment: Result of the programming
result_of_programming.png
result_of_programming.png [ 9.01 KiB | Viewed 8332 times ]
Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 20, 2018 1:42 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 21, 2018 6:50 am 
Offline

Joined: Tue Jan 30, 2018 10:25 am
Posts: 4
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;
                }


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 21, 2018 9:07 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group