Skip to content

Commit a438be7

Browse files
committed
Added content.
1 parent a9c913f commit a438be7

File tree

3 files changed

+92
-48
lines changed

3 files changed

+92
-48
lines changed
22.7 KB
Loading

knowledge-base/pdfprocessing-draw-rectangle-with-content.md

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,20 @@ description: Learn how to draw rectangles with specific styles, add centered tex
44
type: how-to
55
page_title: How to Draw Styled Rectangles with Text and Image Content in PDFs with RadPdfProcessing
66
slug: draw-rectangles-text-images-radpdfprocessing
7-
tags: radpdfprocessing, document processing, rectangles, text, images, drawing, pdf, content, style, format
7+
tags: radpdfprocessing, document, processing, rectangles, text, images, drawing, pdf, content, style, format, fixedcontenteditor
88
res_type: kb
99
ticketid: 1677969
1010
---
1111

1212
## Environment
1313

14-
<table>
15-
<tbody>
16-
<tr>
17-
<td>Product</td>
18-
<td>RadPdfProcessing for Document Processing</td>
19-
</tr>
20-
</tbody>
21-
</table>
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2025.1.205| RadPdfProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)|
2217

2318
## Description
2419

25-
I have created a block that contains some text and is located at a specific position with a defined size. I need to draw a rectangle at the same position and size, style it with a black stroke and a light blue background, and then place text and an image centered both vertically and horizontally within this rectangle.
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.
2621

2722
This knowledge base article also answers the following questions:
2823
- How can I draw a rectangle and style it using RadPdfProcessing?
@@ -35,57 +30,105 @@ To draw a rectangle with a black stroke and a light blue background, add centere
3530

3631
### Drawing a Rectangle
3732

38-
1. Create a `RectangleGeometry` and define its dimensions.
39-
2. Create a `Path` and set the `RectangleGeometry` to its geometry.
40-
3. Set the `IsFilled` and `IsStroked` properties of the path to true.
41-
4. Specify the `Fill`, `Stroke`, and `StrokeThickness` properties for the path.
42-
5. Add the path to the page content.
33+
1. Draw a rectangle by creating a **Path** with a **RectangleGeometry** , defining its dimensions, formatting it, and inserting it in the page.
34+
2. Create a **Block** of text, format it, and draw it on top of the rectangle with the **FixedContentEditor** by specifying its **Position**.
35+
3. Draw a second rectangle at a different position.
36+
4. Create an image **Block** and draw it on top of the second rectangle with the **FixedContentEditor** while specifying its **Position**.
37+
5. Export the **RadFixedDocument** to PDF.
38+
39+
![RadPdfProcessing Draw Rectangles With Text and Image content](images/draw-rectangles-text-images-radpdfprocessing-result.png)
4340

4441
```csharp
45-
RectangleGeometry rectangleGeometry = new RectangleGeometry();
46-
rectangleGeometry.Rect = new Rect(100, 100, 100, 300);
42+
RadFixedDocument radFixedDocument = new RadFixedDocument();
43+
RadFixedPage radFixedPage = new RadFixedPage();
4744

48-
Telerik.Windows.Documents.Fixed.Model.Graphics.Path path = new Telerik.Windows.Documents.Fixed.Model.Graphics.Path();
49-
path.Geometry = rectangleGeometry;
45+
radFixedPage.Size = new Size(11 * 100, 8.5 * 100);
46+
radFixedDocument.Pages.Add(radFixedPage);
5047

51-
path.IsFilled = true;
52-
path.IsStroked = true;
48+
FixedContentEditor fixedContentEditor = new FixedContentEditor(radFixedPage);
5349

54-
path.Fill = new RgbColor(173, 216, 230); // Light blue
55-
path.Stroke = RgbColors.Black;
56-
path.StrokeThickness = 2;
50+
int rectangle1X = 400;
51+
int rectangle1Y = 100;
52+
double rectangle1Width = 100;
53+
double rectangle1Height = 300;
5754

58-
Page.Content.Add(path);
59-
```
55+
int rectangle2X = 700;
56+
int rectangle2Y = 100;
57+
double rectangle2Width = 100;
58+
double rectangle2Height = 300;
6059

61-
### Adding Centered Text
60+
// Draw a rectangle
61+
RectangleGeometry rectangleGeometry1 = new RectangleGeometry();
62+
rectangleGeometry1.Rect = new Rect(rectangle1X, rectangle1Y, rectangle1Width, rectangle1Height);
6263

63-
1. Create a `Block` and insert the text.
64-
2. Apply text and graphic properties as desired.
65-
3. Use a `FixedContentEditor` to draw the block at the desired position and size.
64+
Telerik.Windows.Documents.Fixed.Model.Graphics.Path rectangle1Path = new Telerik.Windows.Documents.Fixed.Model.Graphics.Path();
65+
rectangle1Path.Geometry = rectangleGeometry1;
66+
rectangle1Path.IsFilled = true;
67+
rectangle1Path.IsStroked = true;
68+
rectangle1Path.Fill = new RgbColor(173, 216, 230);
69+
rectangle1Path.Stroke = RgbColors.Black;
70+
rectangle1Path.StrokeThickness = 2;
6671

67-
```csharp
72+
radFixedPage.Content.Add(rectangle1Path);
73+
74+
// Add formatted text Block on top of the rectangle
6875
Block textBlock = new Block();
69-
textBlock.InsertText("Your text here");
70-
// Apply desired text and graphic properties to textBlock
71-
FixedContentEditor editor = new FixedContentEditor(Page);
72-
editor.Position.Translate(100, 100);
73-
editor.DrawBlock(textBlock, new System.Windows.Size(100, 300));
74-
```
7576

76-
### Inserting a Centered Image
77+
textBlock.VerticalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.VerticalAlignment.Center;
78+
textBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
79+
textBlock.TextProperties.CharacterSpacing = 5;
80+
textBlock.TextProperties.Font = FontsRepository.TimesBold;
81+
textBlock.TextProperties.FontSize = Unit.PointToDip(12);
7782

78-
1. Load or create an `ImageSource`.
79-
2. Create an `Image` and set the image source.
80-
3. Use a `FixedContentEditor` to draw the image at the desired position and size.
83+
textBlock.InsertText("How will this text needs to see how this draws");
8184

82-
```csharp
83-
ImageSource imageSource = new ImageSource("path/to/your/image.jpg");
84-
Image image = new Image(imageSource);
85-
// Use FixedContentEditor to draw the image as shown for the text block
86-
```
85+
fixedContentEditor.Position.Translate(rectangle1X, rectangle1Y);
86+
fixedContentEditor.DrawBlock(textBlock, new Size(rectangle1Width, rectangle1Height));
87+
88+
// Draw a second rectangle
89+
RectangleGeometry rectangleGeometry2 = new RectangleGeometry();
90+
rectangleGeometry2.Rect = new Rect(rectangle2X, rectangle2Y, rectangle2Width, rectangle2Height);
91+
92+
Telerik.Windows.Documents.Fixed.Model.Graphics.Path rectangle2Path = new Telerik.Windows.Documents.Fixed.Model.Graphics.Path();
93+
rectangle2Path.Geometry = rectangleGeometry2;
94+
rectangle2Path.IsFilled = true;
95+
rectangle2Path.IsStroked = true;
96+
rectangle2Path.Fill = new RgbColor(173, 216, 230);
97+
rectangle2Path.Stroke = RgbColors.Black;
98+
rectangle2Path.StrokeThickness = 2;
8799

88-
Following these steps allows you to draw styled rectangles, add centered text, and insert centered images within these rectangles using the RadPdfProcessing library.
100+
radFixedPage.Content.Add(rectangle2Path);
101+
102+
// Draw a block with an image on top of the second rectangle
103+
Block imageBlock = new Block();
104+
105+
imageBlock.VerticalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.VerticalAlignment.Center;
106+
imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
107+
108+
Image image = new Image();
109+
string imageFilePath = "..\\..\\..\\image.png";
110+
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open);
111+
112+
imageBlock.InsertImage(fileStream);
113+
114+
fixedContentEditor.Position.Translate(rectangle2X, rectangle2Y);
115+
fixedContentEditor.DrawBlock(imageBlock, new Size(rectangle2Width, rectangle2Height));
116+
117+
// Export To PDF
118+
string pdfOutputPath = "output.pdf";
119+
PdfFormatProvider PDFProvider = new PdfFormatProvider();
120+
using (FileStream FS = File.OpenWrite(pdfOutputPath))
121+
{
122+
PDFProvider.Export(radFixedDocument, FS);
123+
}
124+
125+
var psi = new ProcessStartInfo()
126+
{
127+
FileName = pdfOutputPath,
128+
UseShellExecute = true
129+
};
130+
Process.Start(psi);
131+
```
89132

90133
## See Also
91134

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)