WordDocument Image Scaling and Fit to Size of Parent when inserting a WPicture

Hi,
I'm loading an image into a WordDocument using a WPicture(see image below) but cannot get the image to scale/fit it's parent container.
I've tried different combinations of alignment, scaling, width, height etc. but they seem to have no effect on the loaded image.

Thanks,
Jonathan

9 Replies

SY Sethumanikkam Yogendran Syncfusion Team January 15, 2018 07:23 AM UTC

Hi Jonathan,

Thank you for contacting Syncfusion support.

On further analyzing with the given details, we suspect that your requirement is to set the scale of image for picture based on the page size. If so, kindly set the WidthScale and HeightScale values of WPicture based on the width and height value of image. Please refer the below highlighted code snippet and let us know if this helps you.

Code snippet:
 
float clientWidth = section.PageSetup.ClientWidth;
float clientHeight = section.PageSetup.PageSize.Height - section.PageSetup.Margins.Top - section.PageSetup.Margins.Bottom;
float scalePer = 100;
if (picture.Width > clientWidth)
{
    scalePer = clientWidth / imageWidth * 100;
}
else if (picture.Height > clientHeight)
{
    scalePer = clientHeight / imageHeight * 100;
}
// This will resizes the width and height.
picture.WidthScale = scalePer;
picture.HeightScale = scalePer;
 

Please refer the below UG link to know more about working with images.
https://help.syncfusion.com/file-formats/docio/working-with-paragraph#working-with-images
Also, please refer below link to know more about access Word document behvaior.
https://www.syncfusion.com/kb/3989/why-it-is-not-possible-to-access-the-word-document-contents-page-by-page

Please let us know if you have any other questions.

Regards,
Sethumanikkam.Y



JB Jonathan Bailey January 23, 2018 09:54 AM UTC

Thanks for the quick response. 

I've used the below code to successfully load an image into a WPicture and scale it to the size of the table cell (parent container).

I have encountered another problem.

The cell has margins set in MS Word. In my case, the left and right margins are set to 1.9cm.

I need to take the margins into account when calculating a scaling factor for the image. I cannot find the margin information in the table cell class.

The WPicture has a property called DistanceFromLeft but it's not public.

Thanks,
Jonathan


MJ Mohanaselvam Jothi Syncfusion Team January 24, 2018 11:41 AM UTC

//Gets the table cell from table. 
WTableCell tableCell = table.Rows[0].Cells[0]; 
float cellSpacing = 0; 
 
//Gets the spacing value from the row format. 
if (tableCell.OwnerRow != null && (tableCell.OwnerRow as WTableRow).RowFormat.CellSpacing > 0) 
cellSpacing = (tableCell.OwnerRow as WTableRow).RowFormat.CellSpacing; 
//Gets the spacing value from the table format. 
else if (tableCell.OwnerRow != null && tableCell.OwnerRow.Owner != null && (tableCell.OwnerRow.Owner as WTable).TableFormat.CellSpacing > 0) 
cellSpacing = (tableCell.OwnerRow.Owner as WTable).TableFormat.CellSpacing; 
 
//Gets the client width of cell excluding the margins and spacing values. 
float clientWidth = tableCell.Width - (tableCell.CellFormat.Paddings.Left + tableCell.CellFormat.Paddings.Right) - (cellSpacing * 2); 
float scalePercentage = 100; 
 
//Calculates value for width scale factor. 
if (picture.Width > clientWidth) 
scalePercentage = clientWidth / picture.Width * 100; 
//This will resizes the width. 
picture.WidthScale *= scalePercentage / 100; 


Please let us know if you have any other questions.

Regards,
Mohanaselvam J





JB Jonathan Bailey January 26, 2018 01:38 PM UTC

Hi,

I had previously looked at RowFormat. CellSpacing & CellFormat.Paddings but they didn't contain values that I could use.

In MS Word my Cell Margins are 1.9cm.

But CellSpacing is always NULL or -1 and Paddings.Right and Paddings.Left are always 0. 

My WPicture, which is inside the Cell, has two private fields, DistanceFromLeft & DistanceFromRight, both = 9. But I would have to read these with reflection and they don't give me an accurate scaling for the margins when used in a calculation.

Thanks,
Jonathan


MJ Mohanaselvam Jothi Syncfusion Team January 29, 2018 02:53 PM UTC

Hi Jonathan,

Thank you for your update.

On further analyzing with the given details, we suspect that the cell margins are set as same as the whole table in your input Word document. We suggest you to use cell paddings in the Essential DocIO to get the margins of the table cell and the CellSpacing to get spacing values between the cells in the table or table row.

For your we have prepared a sample and in this sample we have done the following things:
1. Loads an existing Word document using DocIO.
2. Gets table cell from the table.
3. Gets spacings and margins(paddings) from table cell, based on the values applied on it.
4. Calculates the cell client width using spacing and margin values.
5. Loads an image and performs scaling as per cell client width.
6. Sets the scaled image in a table cell and saves the Word document.

Please find the sample form the below location :
https://www.syncfusion.com/downloads/support/forum/135433/ze/GenerateWord-1014696211.zip

Regarding DistanceFromLeft & DistanceFromRight in WPicture:
In DocIO, DistanceFromLeft and DistanceFromRight in WPicture are used to represent the spacing between text content and bounding edge of the picture, which does not related to the cell margins.

If you are still facing the same issue means, then kindly provide us the template Word document which used at your end or modified sample and the working scenario. Thereby we will analyze further with the mentioned case and
provide appropriate solution at the earliest.

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 



JB Jonathan Bailey January 30, 2018 10:10 AM UTC

Hi,

I now have your examples and code working for calculating paddings and margins within cells.

The problem was that my cells are inheriting paddings from the global table setting. So if I set my global table cell setting to 1.9cm this is propagated to all cells. When I try to read paddings from the CellFormat they always return 0, even though the 1.9cm (DIU when read) has been inherited from the table setting. I must manually override the margin values in WORD at cell level first and then they are populated in the CellFormat. 

Is this by design? I haven't been able to get values for the global table setting paddings, they are also 0 even though they indicate 1.9cm in Word.

I see the same behaviour with WTable.TableFormat.Paddings. If I try to read the Padding Values from the default settings then they are always 0.

If I override the values then they are then populated. Is there somewhere else I can read these values from?

They are set in a private member of the table style but don't get copied to the public TableFormat.


Thanks,
Jonathan


MJ Mohanaselvam Jothi Syncfusion Team January 31, 2018 07:36 AM UTC

Hi Jonathan,

Thank you for your update.

We need to analyze your input Word document to understand from where the cell padding values are inherited (Table, styles, or default) and then proceed further with exact solution. Could you please provide us the input Word document along with the code snippets which used at your end? So that we can analyze further in line with your requirement and provide exact solution at the earliest.

Note: If you have any confidential data in your Word document, please replace with some dummy data and provide us the same. We just need your document to recreate the problem you face.


Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 



IA Irshad Ahmed August 7, 2018 07:26 AM UTC

paragraph = section.AddParagraph(); 
paragraph.AppendHTML("

" + sbquestion.ToString().Replace("\\n", "
").Replace("\r", "").Replace("\a", "").Replace("\f", "") + "

");
                                                    paragraph.ParagraphFormat.KeepFollow = true;
                                                    paragraph.ParagraphFormat.Keep = true;
                                                    paragraph.ParagraphFormat.AfterSpacing = 8f // This line is not working. No space is given after paragraph.AppendHTML
                                                    paragraph = section.AddParagraph();



DB Dilli Babu Nandha Gopal Syncfusion Team August 7, 2018 11:40 AM UTC

Hi Irshad, 

In Essential DocIO, BeforeSpacing and AfterSpacing will be considered as auto once HTML text is appended in a paragraph which is similar Microsoft Word behavior. This behavior can be suppressed by disabling SpaceAfterAuto and SpaceBeforeAuto property i.e., set as false. Please find the code example for the same. 
paragraph.ParagraphFormat.SpaceAfterAuto = false; 
paragraph.ParagraphFormat.SpaceBeforeAuto = false; 

Please let us know if you have any questions. 

Regards, 
Dilli babu. 


Loader.
Up arrow icon