New to Telerik Document Processing? Download free 30-day trial

How do I add an image using WordsProcessing with the correct aspect ratio?

Environment

Product Version 2021.2.728.1
Product Telerik Document Processing

Description

This article illustrates how you can insert an image in a RadFlowDocument instance and resize that image to fit into the page while keeping its aspect ratio.

Solution

Create an image and compare its size with the size available on the page. If the image is bigger, decrease its size:

Insert and resize image

RadFlowDocument document = new RadFlowDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
 
Section section = editor.InsertSection(); 
section.PageSize = new Size(300, 500); 
 
Image image = new ImageInline(document).Image; 
image.ImageSource = new ImageSource(File.OpenRead(@"C:\Users\tdimitrova\Desktop\sample.png"), "png"); 
 
double availableWidth = section.PageSize.Width - section.PageMargins.Left - section.PageMargins.Right; 
while (image.Width > availableWidth) 
{ 
    image.SetWidth(true, image.Width - 5); 
} 
 
editor.InsertImageInline(image.ImageSource, image.Size); 

See Also

In this article
Not finding the help you need? Improve this article