|
| 1 | +--- |
| 2 | +title: Adding an Image Border in PdfProcessing |
| 3 | +description: Learn how to draw borders around images with the Telerik PdfProcessing library. |
| 4 | +type: how-to |
| 5 | +page_title: Drawing Borders for Images in Telerik PdfProcessing |
| 6 | +meta_title: Drawing Borders for Images in Telerik PdfProcessing |
| 7 | +slug: pdf-image-border |
| 8 | +tags: pdf, processing, telerik, document, image, export, border, table |
| 9 | +res_type: kb |
| 10 | +ticketid: 1698380 |
| 11 | +--- |
| 12 | +<style> |
| 13 | +img[alt$="><"] { |
| 14 | + border: 1px solid lightgrey; |
| 15 | +} |
| 16 | +</style> |
| 17 | + |
| 18 | +## Environment |
| 19 | + |
| 20 | +| Version | Product | Author | |
| 21 | +| ---- | ---- | ---- | |
| 22 | +| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +Learn how to add borders around [images]({%slug radpdfprocessing-model-image%}) in the generated PDF document. |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +To draw borders around images follow one of the approaches below: |
| 31 | + |
| 32 | +### Approach 1: Using a Table with Borders |
| 33 | + |
| 34 | +Insert a table with a single [TableCell]({%slug radpdfprocessing-editing-table-tablecell%}) and put the image in the cell: |
| 35 | + |
| 36 | +```csharp |
| 37 | +RadFixedDocument document = new RadFixedDocument(); |
| 38 | +RadFixedPage page = document.Pages.AddPage(); |
| 39 | +FixedContentEditor editor = new FixedContentEditor(page); |
| 40 | +int thickness = 3; |
| 41 | + |
| 42 | +RgbColor bordersColor = new RgbColor(255, 0, 0); |
| 43 | +Border border = new Border(thickness, Telerik.Windows.Documents.Fixed.Model.Editing.BorderStyle.Single, bordersColor); |
| 44 | +TableCellBorders tableCellsBorder = new TableCellBorders(border, border, border, border, null, null); |
| 45 | +Table table = new Table(); |
| 46 | +table.Borders = new TableBorders(border); |
| 47 | +table.DefaultCellProperties.Borders = tableCellsBorder; |
| 48 | +TableRow imgRow = table.Rows.AddTableRow(); |
| 49 | +TableCell imgCell = imgRow.Cells.AddTableCell(); |
| 50 | +imgCell.Borders = tableCellsBorder; |
| 51 | +Block imageBlock = imgCell.Blocks.AddBlock(); |
| 52 | +imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; |
| 53 | +imageBlock.InsertImage(new FileStream("banner.png", FileMode.Open)); |
| 54 | + |
| 55 | +table.Draw(editor, new Rect(5, 5, page.Size.Width, page.Size.Height)); |
| 56 | + |
| 57 | +PdfFormatProvider pdfProvider = new PdfFormatProvider(); |
| 58 | +using (Stream output = File.OpenWrite(@"..\..\exported.pdf")) |
| 59 | +{ |
| 60 | + pdfProvider.Export(document, output); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +### Approach 2: Using FixedContentEditor to Draw a Rectangle Border |
| 67 | + |
| 68 | +An alternative approach is to draw a rectangular border around an image in a PDF using RadPdfProcessing, you can use the [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) to draw both the image and the rectangle: |
| 69 | + |
| 70 | +```csharp |
| 71 | +RadFixedDocument document = new RadFixedDocument(); |
| 72 | +RadFixedPage page = document.Pages.AddPage(); |
| 73 | +FixedContentEditor editor = new FixedContentEditor(page); |
| 74 | + |
| 75 | +double thickness = 3; |
| 76 | +double x = 10; |
| 77 | +double y = 10; |
| 78 | +double width = 150; |
| 79 | +double height = 150; |
| 80 | +string imagePath = "banner.png"; |
| 81 | + |
| 82 | +// Determine image dimensions |
| 83 | +using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read)) |
| 84 | +{ |
| 85 | + using (var img = System.Drawing.Image.FromStream(fs, false, false)) |
| 86 | + { |
| 87 | + width = img.Width; |
| 88 | + height = img.Height; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Draw rectangle border |
| 93 | +editor.Position.Translate(x - thickness, y - thickness); |
| 94 | +editor.SaveGraphicProperties(); |
| 95 | +editor.GraphicProperties.StrokeColor = new RgbColor(255, 0, 0); |
| 96 | +editor.GraphicProperties.StrokeThickness = thickness; |
| 97 | +editor.DrawRectangle(new Rect(0, 0, width + thickness * 2, height + thickness * 2)); |
| 98 | +editor.RestoreGraphicProperties(); |
| 99 | + |
| 100 | +// Draw image |
| 101 | +using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read)) |
| 102 | +{ |
| 103 | + editor.Position.Translate(x, y); |
| 104 | +} |
| 105 | + |
| 106 | +PdfFormatProvider pdfProvider = new PdfFormatProvider(); |
| 107 | +using (Stream output = File.OpenWrite(@"..\..\exported.pdf")) |
| 108 | +{ |
| 109 | + pdfProvider.Export(document, output); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## See Also |
| 116 | +- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) |
| 117 | +- [TableCell]({%slug radpdfprocessing-editing-table-tablecell%}) |
| 118 | +- [Images]({%slug radpdfprocessing-model-image%}) |
0 commit comments