Skip to content

Commit 00f9d96

Browse files
authored
Merge pull request #631 from telerik/new-kb-pdf--image-border-17fa74984cc34c1db1675a9091760194
Added new kb article pdf--image-border
2 parents 7c4b5d1 + a1038e4 commit 00f9d96

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
47.7 KB
Loading
29.6 KB
Loading

knowledge-base/pdf-image-border.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
![Using a Table with Borders ><](images/image-table-cell-border.png)
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+
![Using a FixedContentEditor to Draw the Image Border ><](images/image-fixedcontenteditor-border.png)
114+
115+
## See Also
116+
- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%})
117+
- [TableCell]({%slug radpdfprocessing-editing-table-tablecell%})
118+
- [Images]({%slug radpdfprocessing-model-image%})

libraries/radpdfprocessing/model/image.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ The Image class exposes also the **GetBitmapSource()** method, enabling you to o
9595
* [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
9696
* [Change file size of a PDF with images through ImageCompression and ImageQuality]({%slug pdfprocessing-change-file-size-through-image-quality-and-compression%})
9797
* [Adding a Barcode to a PDF Document using PdfProcessing and the WinForms BarcodeView]({%slug add-barcode-to-pdf-telerik%})
98+
* [Adding an Image Border in PdfProcessing]({%slug pdf-image-border%})

0 commit comments

Comments
 (0)