|
| 1 | +--- |
| 2 | +title: Adding an Image Border in PdfProcessing |
| 3 | +description: Learn how to draw borders around images. |
| 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 |
| 9 | +res_type: kb |
| 10 | +ticketid: 1698380 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +Learn how to add borders around images in the generated PDF document. |
| 22 | + |
| 23 | +## Solution |
| 24 | + |
| 25 | +### Drawing Borders Around Images |
| 26 | +Follow one of the approaches below: |
| 27 | + |
| 28 | +#### Approach 1: Using a Table with Borders |
| 29 | +```csharp |
| 30 | +RadFixedDocument document = new RadFixedDocument(); |
| 31 | +RadFixedPage page = document.Pages.AddPage(); |
| 32 | +FixedContentEditor editor = new FixedContentEditor(page); |
| 33 | +int thickness = 3; |
| 34 | + |
| 35 | +RgbColor bordersColor = new RgbColor(255, 0, 0); |
| 36 | +Border border = new Border(thickness, Telerik.Windows.Documents.Fixed.Model.Editing.BorderStyle.Single, bordersColor); |
| 37 | +TableCellBorders tableCellsBorder = new TableCellBorders(border, border, border, border, null, null); |
| 38 | +Table table = new Table(); |
| 39 | +table.Borders = new TableBorders(border); |
| 40 | +table.DefaultCellProperties.Borders = tableCellsBorder; |
| 41 | +TableRow imgRow = table.Rows.AddTableRow(); |
| 42 | +TableCell imgCell = imgRow.Cells.AddTableCell(); |
| 43 | +imgCell.Borders = tableCellsBorder; |
| 44 | +Block imageBlock = imgCell.Blocks.AddBlock(); |
| 45 | +imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; |
| 46 | +imageBlock.InsertImage(new FileStream("banner.png", FileMode.Open)); |
| 47 | + |
| 48 | +table.Draw(editor, new Rect(5, 5, page.Size.Width, page.Size.Height)); |
| 49 | + |
| 50 | +PdfFormatProvider pdfProvider = new PdfFormatProvider(); |
| 51 | +using (Stream output = File.OpenWrite(@"..\..\exported.pdf")) |
| 52 | +{ |
| 53 | + pdfProvider.Export(document, output); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +#### Approach 2: Using FixedContentEditor to Draw a Rectangle Border |
| 58 | +```csharp |
| 59 | +RadFixedDocument document = new RadFixedDocument(); |
| 60 | +RadFixedPage page = document.Pages.AddPage(); |
| 61 | +FixedContentEditor editor = new FixedContentEditor(page); |
| 62 | + |
| 63 | +double thickness = 3; |
| 64 | +double x = 10; |
| 65 | +double y = 10; |
| 66 | +double width = 150; |
| 67 | +double height = 150; |
| 68 | +string imagePath = "banner.png"; |
| 69 | + |
| 70 | +// Determine image dimensions |
| 71 | +using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read)) |
| 72 | +{ |
| 73 | + using (var img = System.Drawing.Image.FromStream(fs, false, false)) |
| 74 | + { |
| 75 | + width = img.Width; |
| 76 | + height = img.Height; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Draw rectangle border |
| 81 | +editor.Position.Translate(x - thickness, y - thickness); |
| 82 | +editor.SaveGraphicProperties(); |
| 83 | +editor.GraphicProperties.StrokeColor = new RgbColor(255, 0, 0); |
| 84 | +editor.GraphicProperties.StrokeThickness = thickness; |
| 85 | +editor.DrawRectangle(new Rect(0, 0, width + thickness * 2, height + thickness * 2)); |
| 86 | +editor.RestoreGraphicProperties(); |
| 87 | + |
| 88 | +// Draw image |
| 89 | +using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read)) |
| 90 | +{ |
| 91 | + editor.Position.Translate(x, y); |
| 92 | + editor.DrawImage(fs); |
| 93 | +} |
| 94 | + |
| 95 | +PdfFormatProvider pdfProvider = new PdfFormatProvider(); |
| 96 | +using (Stream output = File.OpenWrite(@"..\..\exported.pdf")) |
| 97 | +{ |
| 98 | + pdfProvider.Export(document, output); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## See Also |
| 103 | +- [PdfProcessing ImageQuality Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/concepts/imagequality) |
| 104 | +- [How to Change File Size of PDF with Images](https://docs.telerik.com/devtools/document-processing/knowledge-base/pdfprocessing-change-file-size-through-image-quality-and-compression) |
| 105 | +- [Optimizing and Reducing PDF Size with RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/knowledge-base/optimize-and-reduce-pdf-size-radpdfprocessing) |
0 commit comments