Rectangles
Rectangles can frame content, highlight a section, or add a visual accent. Create a dashed rectangle and a rounded rectangle with a solid border. This example follows RectanglesInPdf.cs and saves the result as RectanglesInPdf_output.pdf.
What you’ll create
- An A4 PDF containing the text “Rectangle page element sample.”
- A light-blue rectangle measuring 200 millimeters by 30 millimeters with a small-dash border.
- A light-orange rectangle measuring 120 millimeters by 20 millimeters with rounded corners and a solid orange border.
- A file named
RectanglesInPdf_output.pdfin your chosen output directory.
Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class adds rectangles and saves the document to an existing output directory.
using DynamicPDF.Flow;
namespace FluentExamples.Examples;
internal static class RectanglesInPdf
{
public static void Run(string outputDir)
{
var path = Path.Combine(
outputDir, "RectanglesInPdf_output.pdf");
DocumentBuilder.Create()
.Title("Rectangles In PDF")
.Pages(page => page
.Size(PageSize.A4)
.Spacing(10)
.Margins(15)
.Text("Rectangle page element sample.")
.Rectangle(rectangle => rectangle
.Width(200, LengthUnit.Millimeter)
.Height(30, LengthUnit.Millimeter)
.BackgroundColor("#DDEEFF")
.Border(
1.5,
LengthUnit.Pixel,
"#1A3D6D",
LineStyle.DashSmall))
.Spacing(10)
.Rectangle(rectangle => rectangle
.Width(1, LengthUnit.Point)
.Height(1, LengthUnit.Point)
.Border(
0.1,
LengthUnit.Point,
"white",
LineStyle.Solid))
.Rectangle(rectangle => rectangle
.Width(120, LengthUnit.Millimeter)
.Height(20, LengthUnit.Millimeter)
.Style("background-color: #FFE8CC;")
.Border(
2,
LengthUnit.Point,
"#CC6600",
LineStyle.Solid)
.BorderRadius(4)))
.Save(path);
Console.WriteLine($"Created {path}");
}
}
Imports System.IO
Imports DynamicPDF.Flow
Friend Module RectanglesInPdf
Public Sub Run(outputDir As String)
Dim outputPath = Path.Combine(
outputDir, "RectanglesInPdf_output.pdf")
DocumentBuilder.Create().
Title("Rectangles In PDF").
Pages(Sub(page) page.
Size(PageSize.A4).
Spacing(10).
Margins(15).
Text("Rectangle page element sample.").
Rectangle(Sub(rectangle) rectangle.
Width(200, LengthUnit.Millimeter).
Height(30, LengthUnit.Millimeter).
BackgroundColor("#DDEEFF").
Border(
1.5,
LengthUnit.Pixel,
"#1A3D6D",
LineStyle.DashSmall)).
Spacing(10).
Rectangle(Sub(rectangle) rectangle.
Width(1, LengthUnit.Point).
Height(1, LengthUnit.Point).
Border(
0.1,
LengthUnit.Point,
"white",
LineStyle.Solid)).
Rectangle(Sub(rectangle) rectangle.
Width(120, LengthUnit.Millimeter).
Height(20, LengthUnit.Millimeter).
Style("background-color: #FFE8CC;").
Border(
2,
LengthUnit.Point,
"#CC6600",
LineStyle.Solid).
BorderRadius(4))).
Save(outputPath)
Console.WriteLine($"Created {outputPath}")
End Sub
End Module
How it works
1. Choose where to save the PDF
Path.Combine joins outputDir with RectanglesInPdf_output.pdf. Make sure the output directory exists before calling Run. Saving overwrites an existing file with the same name.
2. Configure the document and page
DocumentBuilder.Create() starts the document, and .Title("Rectangles In PDF") sets its metadata title before adding pages.
.Size(PageSize.A4) selects an A4 page. .Margins(15) and .Spacing(10) use the default pixel unit, corresponding to 11.25-point margins and 7.5-point spacing. The label and rectangles follow the page’s normal content flow.
3. Add the dashed rectangle
The first .Rectangle(...) sets its width to 200 millimeters and its height to 30 millimeters. .BackgroundColor("#DDEEFF") gives it a light-blue fill.
.Border(...) sets a 1.5px dark-blue border and selects LineStyle.DashSmall. This rectangle has square corners.
4. Reset the border dash pattern
The next .Rectangle(...) draws a 1-point white square with a solid border in the white gap between the visible rectangles. It resets the PDF dash pattern before the rounded rectangle is drawn.
This is a workaround for the current DLL’s border rendering. A rounded rectangle can inherit the preceding rectangle’s dash pattern even when its own border specifies LineStyle.Solid. The white square forces a solid-pattern reset; it is not a third visible sample. It also occupies 1 point in the content flow.
5. Add the rounded rectangle
The final .Rectangle(...) sets a width of 120 millimeters and a height of 20 millimeters. .Style("background-color: #FFE8CC;") applies the light-orange fill through an inline CSS declaration.
.Border(...) sets a 2-point solid orange border, and .BorderRadius(4) rounds all four corners. The radius uses the default pixel unit, equivalent to 3 points.
The explicit border call and the preceding reset keep this rectangle solid. CSS border styling alone does not reliably reset an earlier dash pattern in the current DLL.
6. Save the PDF
.Save(path) builds and saves the document through the fluent builder. Console.WriteLine prints the output path so you can find and open the file.
Next steps
Change the rectangle dimensions, fill colors, or border widths. Use BorderTopLeftRadius, BorderTopRightRadius, BorderBottomRightRadius, and BorderBottomLeftRadius to configure individual corners. The current Fluent API supports rectangles; it does not expose native builders for circles, ellipses, standalone lines, polygons, arcs, or paths.
See RectangleExamples.cs for more rectangle examples.
Explore Add a table for CSS borders and backgrounds on table cells, or browse all examples.