Skip to content

Commit c1f1370

Browse files
Merge branch 'master' into new-kb-resize-images-radpdfprocessing-7b1bacd1e7e84edb95f5730ca3ef6f78
2 parents ed7f22c + b2cb924 commit c1f1370

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: Adding Images with a Shadow in PDF Documents
3+
description: Learn how to add a shadow effect when inserting images into PDF documents using RadPdfProcessing.
4+
type: how-to
5+
page_title: How to Simulate Shadow Effects for Images in PDFs with RadPdfProcessing
6+
slug: add-shadow-image-radpdfprocessing
7+
tags: radpdfprocessing, document processing, image, shadow, insertimage, path, geometry
8+
res_type: kb
9+
ticketid: 1655064
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 into a PDF document using the [Block.InsertImage](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/block#inserting-image) method, you might want to add a shadow effect to enhance its appearance. [RadPdfProcessing](%slug radpdfprocessing-overview%) provides functionalities to draw paths and geometries, enabling the simulation of shadows around images. This KB article demonstrates how to add a shadow to an image in a PDF document.
21+
22+
## Solution
23+
24+
To add a shadow to an image, utilize [paths]({%slug radpdfprocessing-model-path%}) with specific [geometries]({%slug radpdfprocessing-concepts-geometry%}) to simulate the shadow effect. The following example outlines the necessary steps to insert an image and draw a shadow around it using RadPdfProcessing:
25+
26+
1. **Prepare the environment**: Ensure that [ImagePropertiesResolver](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/cross-platform/images#imagepropertiesresolver) and [JpegImageConverter](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/cross-platform/images#jpegimageconverter) are set for cross-platform image scenarios. Refer to the RadPdfProcessing documentation on [cross-platform image handling]({%slug radpdfprocessing-cross-platform-images%}).
27+
28+
2. **Insert the image**: Use the `Block.InsertImage` method to insert the image into a block.
29+
30+
3. **Draw the shadow**: Create a `RectangleGeometry` around the image's location with an offset to simulate the shadow effect. Use a dark color for the shadow and adjust its opacity as needed.
31+
32+
4. **Export the PDF document**: Use the [PdfFormatProvider]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to export the document to a PDF file.
33+
34+
```csharp
35+
public static Padding pageMarginsValue = new Telerik.Windows.Documents.Primitives.Padding(
36+
Unit.MmToDip(20), //left
37+
Unit.MmToDip(20), //top
38+
Unit.MmToDip(0), //right
39+
Unit.MmToDip(0)); //bottom
40+
41+
static void Main(string[] args)
42+
{
43+
// Setup the environment for image handling
44+
Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver();
45+
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver;
46+
Telerik.Windows.Documents.Extensibility.JpegImageConverterBase defaultJpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();
47+
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = defaultJpegImageConverter;
48+
49+
RadFixedDocument fixedDocument = new RadFixedDocument();
50+
RadFixedPage fixedPage = fixedDocument.Pages.AddPage();
51+
FixedContentEditor fixedContentEditor = new FixedContentEditor(fixedPage);
52+
53+
using (Stream imageStream = File.OpenRead("ninja.png"))
54+
{
55+
Block imageBlock = new Block();
56+
imageBlock.SpacingAfter = 0;
57+
imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
58+
Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource _imageSource =
59+
new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(imageStream);
60+
imageBlock.InsertImage(_imageSource);
61+
Size imageBlockDesiredSize = imageBlock.Measure();
62+
int shadowWidth = 10;
63+
64+
// DrawShadow
65+
RectangleGeometry rectangleGeometry = new RectangleGeometry();
66+
rectangleGeometry.Rect = new Rect(pageMarginsValue.Left + shadowWidth, pageMarginsValue.Top + shadowWidth, imageBlockDesiredSize.Width, imageBlockDesiredSize.Height);
67+
Telerik.Windows.Documents.Fixed.Model.Graphics.Path path = fixedPage.Content.AddPath();
68+
path.IsFilled = true;
69+
path.IsStroked = false;
70+
RgbColor shadowColor = new RgbColor(80, 0, 0, 0);
71+
path.Fill = shadowColor;
72+
path.Geometry = rectangleGeometry;
73+
74+
fixedContentEditor.DrawBlock(imageBlock);
75+
76+
// Export the document
77+
PdfFormatProvider provider = new PdfFormatProvider();
78+
string outputFilePath = @"sample.pdf";
79+
using (Stream output = File.OpenWrite(outputFilePath))
80+
{
81+
provider.Export(fixedDocument, output);
82+
}
83+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
84+
}
85+
}
86+
```
87+
![Image Shadon in PDF](images/image-shadow-pdf.png)
88+
89+
Adjust the shadow's size, color, and opacity according to your requirements. This approach can be customized to fit specific needs or visual styles.
90+
91+
## Notes
92+
93+
- The provided example is a basic approach to simulating a shadow and might not cover all use cases.
94+
- Experiment with different geometry shapes and colors to achieve the desired shadow effect.
95+
96+
## See Also
97+
98+
- [RadPdfProcessing Documentation]({%slug radpdfprocessing-overview%})
99+
- [Drawing Geometries in PDF Documents](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/fixedcontenteditor#inserting-geometries)
100+
- [Handling Images in Cross-Platform Scenarios]({%slug radpdfprocessing-cross-platform-images%})
24.4 KB
Loading

libraries/radpdfprocessing/editing/fixedcontenteditor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,6 @@ __FixedContentEditor__ has some properties and methods that affect how it will b
300300
* [Clipping]({%slug radpdfprocessing-concepts-clipping%})
301301
* [Table]({%slug radpdfprocessing-editing-table%})
302302
* [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%})
303+
* [Adding Images with a Shadow in PDF Documents]({%slug add-shadow-image-radpdfprocessing%})
303304
* [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
304305
* [Resizing Large Images to Fit in the PDF Page]({%slug resize-images-radpdfprocessing%})

libraries/radpdfprocessing/model/image.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,7 @@ The Image class exposes also the **GetBitmapSource()** method, enabling you to o
7575
* [Position]({%slug radpdfprocessing-concepts-position%})
7676
* [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%})
7777
* [How to Generate a PDF Document from Images with RadFixedDocumentEditor]({%slug pdf-from-images-with-radfixeddocumenteditor%})
78+
* [Change file size of a PDF with images through ImageCompression and ImageQuality]({%slug pdfprocessing-change-file-size-through-image-quality-and-compression%})
79+
* [Adding Images with a Shadow in PDF Documents]({%slug add-shadow-image-radpdfprocessing%})
7880
* [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
7981
* [Change file size of a PDF with images through ImageCompression and ImageQuality]({%slug pdfprocessing-change-file-size-through-image-quality-and-compression%})

0 commit comments

Comments
 (0)