Skip to content

Commit b4a9e24

Browse files
Merge pull request #438 from telerik/new-kb-resize-images-radpdfprocessing-7b1bacd1e7e84edb95f5730ca3ef6f78
Added new kb article resize-images-radpdfprocessing
2 parents b2cb924 + c1f1370 commit b4a9e24

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed
266 KB
Loading
316 KB
Loading
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: Resizing Large Images to Fit in the PDF Page
3+
description: Learn how to adjust large images to fit within the PDF page dimensions using RadPdfProcessing, preserving the aspect ratio.
4+
type: how-to
5+
page_title: How to Resize Images for PDF Documents in RadPdfProcessing
6+
slug: resize-images-radpdfprocessing
7+
tags: pdfprocessing, document, processing, image, pdf, resize, aspect, ratio
8+
res_type: kb
9+
ticketid: 1656650
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.2.426| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
When inserting an image to a PDF file using [RadPdfProcessing]({%slug radpdfprocessing-overview%}), the resulting PDF may cut off parts of the image if its dimensions exceed the PDF page size. The goal is to:
21+
22+
1. Display the image as is if it fits on the page without resizing.
23+
2. If the image is too large, shrink it to fit on the page while preserving the aspect ratio.
24+
25+
This KB article also answers the following questions:
26+
- How do I convert an image to a PDF while fitting it on the page in RadPdfProcessing?
27+
- How can I preserve the aspect ratio of an image when converting it to PDF?
28+
- What is the method to resize images for PDF conversion in RadPdfProcessing?
29+
30+
## Solution
31+
32+
To resize a large image to fit within the PDF page dimensions while preserving its aspect ratio, follow these steps:
33+
34+
1. Determine the dimensions of the image by creating an [ImageSource({%slug radpdfprocessing-model-imagesource%}) instance from the image file.
35+
36+
2. Check if the image dimensions exceed the page size. If they do, calculate the new size of the image that preserves the aspect ratio and fits within the page.
37+
38+
3. Use the `DrawImage(ImageSource source, Size size)` method to draw the resized image on the PDF page.
39+
40+
4. To preserve the aspect ratio, use the following code snippet that automatically adjusts the image size based on the page dimensions:
41+
42+
```csharp
43+
Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver();
44+
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver;
45+
46+
string[] imageFiles = Directory.GetFiles(@"..\..\..\MyImages");
47+
48+
foreach (string imageFilePath in imageFiles)
49+
{
50+
RadFixedDocument document = new RadFixedDocument();
51+
RadFixedPage fixedPage = document.Pages.AddPage();
52+
Telerik.Documents.Primitives.Size pageSize = new Telerik.Documents.Primitives.Size(
53+
Telerik.Windows.Documents.Media.Unit.MmToDip(210),
54+
Telerik.Windows.Documents.Media.Unit.MmToDip(297));
55+
Telerik.Documents.Primitives.Size original;
56+
using (Bitmap bmp = new Bitmap(imageFilePath))
57+
{
58+
original = new Telerik.Documents.Primitives.Size(bmp.Width, bmp.Height);
59+
}
60+
61+
double maxSize = Math.Min(pageSize.Width, pageSize.Height);
62+
float percent = (new List<float> { (float)maxSize / (float)original.Width, (float)maxSize / (float)original.Height }).Min();
63+
Telerik.Documents.Primitives.Size resultSize = new Telerik.Documents.Primitives.Size((int)Math.Floor(original.Width * percent), (int)Math.Floor(original.Height * percent));
64+
65+
if (resultSize.Width > resultSize.Height)
66+
{
67+
pageSize = new Telerik.Documents.Primitives.Size(
68+
Telerik.Windows.Documents.Media.Unit.MmToDip(297),
69+
Telerik.Windows.Documents.Media.Unit.MmToDip(210));
70+
}
71+
fixedPage.Size = pageSize;
72+
Telerik.Windows.Documents.Fixed.Model.Objects.Image image = new Telerik.Windows.Documents.Fixed.Model.Objects.Image();
73+
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open);
74+
ImageSource imageSrc = new ImageSource(fileStream);
75+
image.ImageSource = imageSrc;
76+
image.Width = resultSize.Width;
77+
image.Height = resultSize.Height;
78+
SimplePosition simplePosition = new SimplePosition();
79+
simplePosition.Translate(5, 5);
80+
image.Position = simplePosition;
81+
fixedPage.Content.Add(image);
82+
83+
PdfFormatProvider provider = new PdfFormatProvider();
84+
string outputFilePath = "output" + Guid.NewGuid().ToString() + ".pdf";
85+
86+
using (Stream output = File.OpenWrite(outputFilePath))
87+
{
88+
provider.Export(document, output);
89+
}
90+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
91+
}
92+
```
93+
94+
This approach ensures that images are resized to fit within the PDF page dimensions without losing their aspect ratio.
95+
96+
|Vertical Image|Horizontal Image|
97+
|----|----|
98+
|![PdfProcessing Resize Vertical Image](images/pdf-processing-resize-vertical-image.png)|![PdfProcessing Resize Horizontal](images/pdf-processing-resize-horizontal-image.png)|
99+
100+
## See Also
101+
102+
- [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
103+
- [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%})

libraries/radpdfprocessing/editing/fixedcontenteditor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,4 @@ __FixedContentEditor__ has some properties and methods that affect how it will b
302302
* [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%})
303303
* [Adding Images with a Shadow in PDF Documents]({%slug add-shadow-image-radpdfprocessing%})
304304
* [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
305-
305+
* [Resizing Large Images to Fit in the PDF Page]({%slug resize-images-radpdfprocessing%})

0 commit comments

Comments
 (0)