Skip to content

Commit 81024e5

Browse files
Merge pull request #520 from telerik/new-kb-draw-rectangle-with-content-019aa1e5d94b401bab95b24e2a1a4cf4
Added new kb article draw-rectangle-with-content
2 parents 7abd863 + a86b2b8 commit 81024e5

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed
22.7 KB
Loading
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
title: Drawing Rectangles with Text and Image Content with RadPdfProcessing
3+
description: Learn how to draw rectangles with specific styles, add centered text, and images within those rectangles using the RadPdfProcessing library.
4+
type: how-to
5+
page_title: How to Draw Styled Rectangles with Text and Image Content in PDFs with RadPdfProcessing
6+
slug: draw-rectangles-text-images-radpdfprocessing
7+
tags: radpdfprocessing, document, processing, rectangles, text, images, drawing, pdf, content, style, format, fixedcontenteditor, editor, image, centered
8+
res_type: kb
9+
ticketid: 1677969
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2025.1.205| RadPdfProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)|
17+
18+
## Description
19+
20+
This article shows how to draw rectangles with formatted text or image content with the [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) in the [PdfProcessing]({%slug radpdfprocessing-overview%}) library.
21+
22+
This knowledge base article also answers the following questions:
23+
- How can I draw a rectangle and style it using RadPdfProcessing?
24+
- How to add centered text within a rectangle in a PDF document?
25+
- How to insert an image and center it within a rectangle using RadPdfProcessing?
26+
27+
## Solution
28+
29+
To draw a rectangle with a black stroke and a light blue background, add centered text, and insert a centered image within the rectangle using the RadPdfProcessing library, follow these steps:
30+
31+
1. Draw a rectangle by creating a [Path]({%slug radpdfprocessing-model-path%}) with a [RectangleGeometry]({%slug radpdfprocessing-concepts-geometry%}#rectanglegeometry), defining its dimensions, formatting it, and inserting it in the page.
32+
2. Create a **Block** of text, format it, and draw it on top of the rectangle with the **FixedContentEditor** by specifying its **Position**.
33+
3. Draw a second rectangle at a different position.
34+
4. Create an image **Block** and draw it on top of the second rectangle with the **FixedContentEditor** while specifying its **Position**.
35+
5. Export the **RadFixedDocument** to PDF.
36+
37+
![RadPdfProcessing Draw Rectangles With Text and Image content](images/draw-rectangles-text-images-radpdfprocessing-result.png)
38+
39+
```csharp
40+
RadFixedDocument radFixedDocument = new RadFixedDocument();
41+
RadFixedPage radFixedPage = new RadFixedPage();
42+
43+
radFixedPage.Size = new Size(11 * 100, 8.5 * 100);
44+
radFixedDocument.Pages.Add(radFixedPage);
45+
46+
FixedContentEditor fixedContentEditor = new FixedContentEditor(radFixedPage);
47+
48+
int rectangle1X = 400;
49+
int rectangle1Y = 100;
50+
double rectangle1Width = 100;
51+
double rectangle1Height = 300;
52+
53+
int rectangle2X = 700;
54+
int rectangle2Y = 100;
55+
double rectangle2Width = 100;
56+
double rectangle2Height = 300;
57+
58+
// Draw a rectangle
59+
RectangleGeometry rectangleGeometry1 = new RectangleGeometry();
60+
rectangleGeometry1.Rect = new Rect(rectangle1X, rectangle1Y, rectangle1Width, rectangle1Height);
61+
62+
Telerik.Windows.Documents.Fixed.Model.Graphics.Path rectangle1Path = new Telerik.Windows.Documents.Fixed.Model.Graphics.Path();
63+
rectangle1Path.Geometry = rectangleGeometry1;
64+
rectangle1Path.IsFilled = true;
65+
rectangle1Path.IsStroked = true;
66+
rectangle1Path.Fill = new RgbColor(173, 216, 230);
67+
rectangle1Path.Stroke = RgbColors.Black;
68+
rectangle1Path.StrokeThickness = 2;
69+
70+
radFixedPage.Content.Add(rectangle1Path);
71+
72+
// Add formatted text Block on top of the rectangle
73+
Block textBlock = new Block();
74+
75+
textBlock.VerticalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.VerticalAlignment.Center;
76+
textBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
77+
textBlock.TextProperties.CharacterSpacing = 5;
78+
textBlock.TextProperties.Font = FontsRepository.TimesBold;
79+
textBlock.TextProperties.FontSize = Unit.PointToDip(12);
80+
81+
textBlock.InsertText("How will this text needs to see how this draws");
82+
83+
fixedContentEditor.Position.Translate(rectangle1X, rectangle1Y);
84+
fixedContentEditor.DrawBlock(textBlock, new Size(rectangle1Width, rectangle1Height));
85+
86+
// Draw a second rectangle
87+
RectangleGeometry rectangleGeometry2 = new RectangleGeometry();
88+
rectangleGeometry2.Rect = new Rect(rectangle2X, rectangle2Y, rectangle2Width, rectangle2Height);
89+
90+
Telerik.Windows.Documents.Fixed.Model.Graphics.Path rectangle2Path = new Telerik.Windows.Documents.Fixed.Model.Graphics.Path();
91+
rectangle2Path.Geometry = rectangleGeometry2;
92+
rectangle2Path.IsFilled = true;
93+
rectangle2Path.IsStroked = true;
94+
rectangle2Path.Fill = new RgbColor(173, 216, 230);
95+
rectangle2Path.Stroke = RgbColors.Black;
96+
rectangle2Path.StrokeThickness = 2;
97+
98+
radFixedPage.Content.Add(rectangle2Path);
99+
100+
// Draw a block with an image on top of the second rectangle
101+
Block imageBlock = new Block();
102+
103+
imageBlock.VerticalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.VerticalAlignment.Center;
104+
imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
105+
106+
Image image = new Image();
107+
string imageFilePath = "..\\..\\..\\image.png";
108+
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open);
109+
110+
imageBlock.InsertImage(fileStream);
111+
112+
fixedContentEditor.Position.Translate(rectangle2X, rectangle2Y);
113+
fixedContentEditor.DrawBlock(imageBlock, new Size(rectangle2Width, rectangle2Height));
114+
115+
// Export To PDF
116+
string pdfOutputPath = "output.pdf";
117+
PdfFormatProvider PDFProvider = new PdfFormatProvider();
118+
using (FileStream FS = File.OpenWrite(pdfOutputPath))
119+
{
120+
PDFProvider.Export(radFixedDocument, FS);
121+
}
122+
123+
var psi = new ProcessStartInfo()
124+
{
125+
FileName = pdfOutputPath,
126+
UseShellExecute = true
127+
};
128+
Process.Start(psi);
129+
```
130+
131+
## See Also
132+
133+
- [Text and Graphic Properties]({%slug radpdfprocessing-editing-text-and-graphic-properties%})
134+
- [Path]({%slug radpdfprocessing-model-path%})
135+
- [Geometry]({%slug radpdfprocessing-concepts-geometry%})
136+
- [PdfProcessing Image]({%slug radpdfprocessing-model-image%})
137+
---

libraries/radpdfprocessing/editing/fixedcontenteditor.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,5 @@ __FixedContentEditor__ has some properties and methods that affect how it will b
313313
* [Adding Images with a Shadow in PDF Documents]({%slug add-shadow-image-radpdfprocessing%})
314314
* [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
315315
* [Resizing Large Images to Fit in the PDF Page]({%slug resize-images-radpdfprocessing%})
316-
* [ Inserting HTML Content into PDF TableCell with RadPdfProcessing]({%slug insert-html-content-into-pdf-tablecell-radpdfprocessing%})
316+
* [Inserting HTML Content into PDF TableCell with RadPdfProcessing]({%slug insert-html-content-into-pdf-tablecell-radpdfprocessing%})
317+
* [Drawing Rectangles with Text and Image Contant with RadPdfProcessing]({%slug draw-rectangles-text-images-radpdfprocessing%})

0 commit comments

Comments
 (0)