|
| 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 | +  |
| 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%}) |
0 commit comments