Back to Devexpress

PdfGraphics.DrawRectangle(DXPen, RectangleF) Method

officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-drawrectangle-x28-devexpress-dot-drawing-dot-dxpen-system-dot-drawing-dot-rectanglef-x29.md

latest5.1 KB
Original Source

PdfGraphics.DrawRectangle(DXPen, RectangleF) Method

Draws a rectangle in the specified page area.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Pdf.v25.2.Drawing.dll

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public void DrawRectangle(
    DXPen pen,
    RectangleF bounds
)
vb
Public Sub DrawRectangle(
    pen As DXPen,
    bounds As RectangleF
)

Parameters

NameTypeDescription
penDXPen

A DXPen object that specifies the color, width, and style of the rectangle.

| | bounds | RectangleF |

A RectangleF structure that specifies a page area (in world coordinate system) where you can draw a rectangle.

|

Remarks

Use this method to draw a rectangle in the page area specified by the bounds parameter.

To draw a shape on the PDF page, use one of the following methods:

PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackgroundThese methods allow you to draw content on an existing page.PdfDocumentProcessor.RenderNewPageDraws content on a new page.

Example

How to: Draw a Rectangle

The following code snippet draws a rectangle in the specified area of the document page.

csharp
using DevExpress.Pdf;
using DevExpress.Drawing;
//...

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
    processor.CreateEmptyDocument();
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4);
    using (PdfGraphics graphics = processor.CreateGraphicsPageSystem()) {

        // Draw a rectangle.
        using (var pen = new DXPen(Color.Red, 5))
            graphics.DrawRectangle(pen, new RectangleF(50, 50, 500, 300));

        // Add graphics content to the document page.
        graphics.AddToPageForeground(page);
    }
    processor.SaveDocument("out2.pdf");
}
Process.Start("out.pdf");
vb
Imports DevExpress.Pdf
Imports System.Drawing
'...

Using processor As New PdfDocumentProcessor()
    processor.CreateEmptyDocument()
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4)
    Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
        ' Draw a rectangle.
        Using pen As New DXPen(Color.Red, 5)
            graphics.DrawRectangle(pen, New RectangleF(50, 50, 500, 300))
        End Using

        ' Add graphics content to the document page.
        graphics.AddToPageForeground(page)
    End Using
    processor.SaveDocument("out2.pdf")
End Using
Process.Start("out.pdf")

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the DrawRectangle(DXPen, RectangleF) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

how-to-custom-draw-in-pdf-viewer/CS/PDF_Viewer/Form1.cs#L53

csharp
(float)Math.Max(p1.X, p2.X), (float)Math.Max(p1.Y, p2.Y));
graph.DrawRectangle(new DXPen(Color.Red), bounds);
graph.AddToPageForeground(page, 72, 72);

how-to-custom-draw-in-pdf-viewer/VB/PDF_Viewer/Form1.vb#L47

vb
Dim bounds As RectangleF = RectangleF.FromLTRB(CSng(Math.Min(p1.X, p2.X)), CSng(Math.Min(p1.Y, p2.Y)), CSng(Math.Max(p1.X, p2.X)), CSng(Math.Max(p1.Y, p2.Y)))
graph.DrawRectangle(New DXPen(Color.Red), bounds)
graph.AddToPageForeground(page, 72, 72)

See Also

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace