-
Notifications
You must be signed in to change notification settings - Fork 41
Added new kb article add-shadow-image-radpdfprocessing #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dessyordanova
merged 5 commits into
master
from
new-kb-add-shadow-image-radpdfprocessing-f22e138bf4584330be9f330371cb633e
Jul 2, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d4cc57
Added new kb article add-shadow-image-radpdfprocessing
751eb1a
add image
dessyordanova 407d6fe
fix links with slug
dessyordanova 8eeaa6a
addressed feedback and comments
dessyordanova 2e9e28f
Merge branch 'master' into new-kb-add-shadow-image-radpdfprocessing-f…
dessyordanova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| --- | ||
| title: Adding Images with a Shadow in PDF Documents | ||
| description: Learn how to add a shadow effect when inserting images into PDF documents using RadPdfProcessing. | ||
| type: how-to | ||
| page_title: How to Simulate Shadow Effects for Images in PDFs with RadPdfProcessing | ||
| slug: add-shadow-image-radpdfprocessing | ||
| tags: radpdfprocessing, document processing, image, shadow, insertimage, path, geometry | ||
| res_type: kb | ||
| ticketid: 1655064 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| | Version | Product | Author | | ||
| | --- | --- | ---- | | ||
| | 2024.2.426| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| | ||
|
|
||
| ## Description | ||
|
|
||
| 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. | ||
|
|
||
| ## Solution | ||
|
|
||
| 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: | ||
|
|
||
| 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%}). | ||
|
|
||
| 2. **Insert the image**: Use the `Block.InsertImage` method to insert the image into a block. | ||
|
|
||
| 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. | ||
|
|
||
| 4. **Export the PDF document**: Use the [PdfFormatProvider]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to export the document to a PDF file. | ||
|
|
||
| ```csharp | ||
| public static Padding pageMarginsValue = new Telerik.Windows.Documents.Primitives.Padding( | ||
| Unit.MmToDip(20), //left | ||
| Unit.MmToDip(20), //top | ||
| Unit.MmToDip(0), //right | ||
| Unit.MmToDip(0)); //bottom | ||
|
|
||
| static void Main(string[] args) | ||
| { | ||
| // Setup the environment for image handling | ||
| Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver(); | ||
| Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver; | ||
| Telerik.Windows.Documents.Extensibility.JpegImageConverterBase defaultJpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter(); | ||
| Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = defaultJpegImageConverter; | ||
|
|
||
| RadFixedDocument fixedDocument = new RadFixedDocument(); | ||
| RadFixedPage fixedPage = fixedDocument.Pages.AddPage(); | ||
| FixedContentEditor fixedContentEditor = new FixedContentEditor(fixedPage); | ||
|
|
||
| using (Stream imageStream = File.OpenRead("ninja.png")) | ||
| { | ||
| Block imageBlock = new Block(); | ||
| imageBlock.SpacingAfter = 0; | ||
| imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; | ||
| Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource _imageSource = | ||
| new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(imageStream); | ||
| imageBlock.InsertImage(_imageSource); | ||
| Size imageBlockDesiredSize = imageBlock.Measure(); | ||
| int shadowWidth = 10; | ||
|
|
||
| // DrawShadow | ||
| RectangleGeometry rectangleGeometry = new RectangleGeometry(); | ||
| rectangleGeometry.Rect = new Rect(pageMarginsValue.Left + shadowWidth, pageMarginsValue.Top + shadowWidth, imageBlockDesiredSize.Width, imageBlockDesiredSize.Height); | ||
| Telerik.Windows.Documents.Fixed.Model.Graphics.Path path = fixedPage.Content.AddPath(); | ||
| path.IsFilled = true; | ||
| path.IsStroked = false; | ||
| RgbColor shadowColor = new RgbColor(80, 0, 0, 0); | ||
| path.Fill = shadowColor; | ||
| path.Geometry = rectangleGeometry; | ||
|
|
||
| fixedContentEditor.DrawBlock(imageBlock); | ||
|
|
||
| // Export the document | ||
| PdfFormatProvider provider = new PdfFormatProvider(); | ||
| string outputFilePath = @"sample.pdf"; | ||
| using (Stream output = File.OpenWrite(outputFilePath)) | ||
| { | ||
| provider.Export(fixedDocument, output); | ||
| } | ||
| Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); | ||
| } | ||
| } | ||
| ``` | ||
|  | ||
|
|
||
| Adjust the shadow's size, color, and opacity according to your requirements. This approach can be customized to fit specific needs or visual styles. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The provided example is a basic approach to simulating a shadow and might not cover all use cases. | ||
| - Experiment with different geometry shapes and colors to achieve the desired shadow effect. | ||
|
|
||
| ## See Also | ||
|
|
||
| - [RadPdfProcessing Documentation]({%slug radpdfprocessing-overview%}) | ||
| - [Drawing Geometries in PDF Documents](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/fixedcontenteditor#inserting-geometries) | ||
| - [Handling Images in Cross-Platform Scenarios]({%slug radpdfprocessing-cross-platform-images%}) | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.