Resizing images embedded in table

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v7)  /  Re: Resizing images embedded in table

DynamicPDF CoreSuite for .NET (v7) Forum

 May 13 2019 11:17 AM
I am trying to build a table with images interspersed within it.  The idea is to have a number of normal table rows (no images) filled with multiple columns of data.  This is followed by a single row with a single image spanning all columns.  After this there may be one or more sequences of the same.  Example:

row 1 text data in columns
row 2 text data in columns
row 3 image spanning all columns
row 4 text data in columns
row 5 text data in columns
row 6 text data in columns
row 7 image spanning all columns.
...

What I would like to do is to be able to adjust the height of the images at the time the table elements are being added to the page.  The code to add the table elements to the page looks like this currently:

While (Not IsNothing(_table))
      ' Center the table horizontally
      Location = New coord((Style.Width - Width) / 2, useY)
      Dim requiredHeight As Single = _table.GetRequiredHeight
      _table.Height = doc.ContentBottom(Style.PageDimensions.Body.Height) - useY
      currentPage.Elements.Add(_table)
      _table = _table.GetOverflowRows
      If (Not IsNothing(_table)) Then
        doc.AppendPage(currentPage)
        pageNumber += 1
        currentPage = doc.NewPage(Style, pageNumber)
        useY = doc.ContentTop + TopMargin
      Else
        useY += requiredHeight
      End If
    End While


I would like to be able to modify this code to be able to adjust the height of images in the table to reduce the amount of blank space on the page due to the images not fitting and having to spill to the next page.  Is there some way (possibly a callback during the Page.Elements.Add operation) that I can get the images that would cause the page to overflow and adjust their height to potentially fit the page?
Posted by a ceTe Software moderator
Hello,

You will need to build the table by adding the images to the cell  by setting calculated dimensions. You can calculate the available space or occupied space in the page using page dimensions and position and height of the table. Once you completely build the table then add it to the page. Below is the code sample.

            Document document = new Document();
            Table2 table = new Table2(0, 0, 500, 0);
            // Add columns to the table
            Page page1 = new Page();
            table.Columns.Add(100);
            table.Columns.Add(400);
            float occupiedHeight = table.Y;

            //Add other rows to the Table.
            for (int i = 1; i < 12; i++)
            {
                Row2 row1 = table.Rows.Add();
                Cell2 r1c1 = row1.Cells.Add("Row text :" + i);
                Cell2 r1c2 = row1.Cells.Add("Row text :" + i);
                occupiedHeight = row1.ActualRowHeight + occupiedHeight + (float)table.CellDefault.Border.Width;
                if (occupiedHeight > page1.Dimensions.Body.Height - 10)
                {
                    occupiedHeight = 0;
                }
            }
            string imagePath = @"File path of an image";

            Image imageObj = new Image(imagePath, 0, 0);
            imageObj.Width = table.Width - 6;
            // Build the table by adding the image to cell.
            Row2 row = table.Rows.Add(20);
            if (imageObj.Height < page1.Dimensions.Body.Height - occupiedHeight)
            {
                imageObj.Height = imageObj.Height;
            }
            else
            {
                imageObj.Height = page1.Dimensions.Body.Height - occupiedHeight;
            }
            Cell2 cell2 = row.Cells.Add(imageObj);
            row.Height = imageObj.Height;
            cell2.ColumnSpan = 2;
            occupiedHeight = table.GetRequiredHeight() + occupiedHeight + (float)table.CellDefault.Border.Width;
            if (occupiedHeight > page1.Dimensions.Body.Height - 10)
            {
                occupiedHeight = 0;
            }

            //Add other rows to the Table.
            for (int i = 1; i < 30; i++)
            {
                Row2 row1 = table.Rows.Add();
                Cell2 r1c1 = row1.Cells.Add("Row text :" + i);
                Cell2 r1c2 = row1.Cells.Add("Row text :" + i);
                occupiedHeight = row1.ActualRowHeight + occupiedHeight + (float)table.CellDefault.Border.Width;
                if (occupiedHeight > page1.Dimensions.Body.Height - 10)
                {
                    occupiedHeight = 0;
                }

            }

            string imagePath1 = @"File path of an image";
            Image imageObj1 = new Image(imagePath1, 0, 0);
            imageObj1.Width = table.Width - 6;
            // Build the table by adding the image to cell.
            Row2 row2 = table.Rows.Add(20);
            if (imageObj1.Height < page1.Dimensions.Body.Height - occupiedHeight)
            {
                imageObj1.Height = imageObj1.Height;
            }
            else
            {
                imageObj1.Height = page1.Dimensions.Body.Height - occupiedHeight;
            }
            Cell2 cell21 = row2.Cells.Add(imageObj1);
            row2.Height = imageObj1.Height;
            cell21.ColumnSpan = 2;
            occupiedHeight = table.GetRequiredHeight() + occupiedHeight + (float)table.CellDefault.Border.Width;
            if (occupiedHeight > page1.Dimensions.Body.Height - 10)
            {
                occupiedHeight = 0;
            }

            //Adding table to the PDF page.
            do
            {
                Page page = new Page();
                document.Pages.Add(page);
                if (table.GetRequiredHeight() < page.Dimensions.Body.Height)
                {
                    table.Height = table.GetRequiredHeight();
                }
                else
                {
                    table.Height = page.Dimensions.Body.Height;
                }
                page.Elements.Add(table);
                table = table.GetOverflowRows();
            } while (table != null);

            // Save the PDF
            document.Draw(@"C:\Temp\MyDocument.pdf");

Thanks,
ceTe Software Support Team
 May 15 2019 10:00 AM
Thanks,

I was hoping to be able to avoid calculating the heights in the the initial table population code as that is very distributed code.  But if there are no hooks available in teh methods for adding the tables to the page or calculating overflow I guess that is the approach I will have to take.

All times are US Eastern Standard time. The time now is 8:47 AM.