Back to Devexpress

PageCanvas Class

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-layout-783faf5d.md

latest11.8 KB
Original Source

PageCanvas Class

Encapsulates the layout drawing surface.

Namespace : DevExpress.XtraRichEdit.API.Layout

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
public abstract class PageCanvas
vb
Public MustInherit Class PageCanvas

The following members return PageCanvas objects:

Remarks

The PageCanvas class provides methods for drawing objects in the document layout.

Use the PagePainter.Canvas property to obtain the PageCanvas object in overridden methods of the PagePainter descendant.

Use the BeforePagePaintEventArgs.Canvas property to access the PageCanvas object and perform a custom draw in the BeforePagePaint event handler.

The PageCanvas enables you to draw lines and shapes, with the specific Draw_GraphicalElement_ method. You can also draw images with the PageCanvas.DrawImage method and text with the PageCanvas.DrawString method.

For complex drawings, you may need to convert the document units of measurement to the units used in layout drawings. The PageCanvas.ConvertToDrawingLayoutUnits method is implemented to accomplish this task.

Example

This code snippet illustrates the implementation of a PagePainter descendant. It draws color rectangles in place of the words and labels a floating picture with a text string drawn over it.

View Example

csharp
public class MyLayoutPainter : PagePainter
{
    public override void DrawPlainTextBox(PlainTextBox plainTextBox)
    {
        if (Form1.customDrawText == true)
        {
            Canvas.DrawRectangle(new RichEditPen(Color.FromArgb(141, 179, 226)), plainTextBox.Bounds);
        }
        else base.DrawPlainTextBox(plainTextBox);
    }

    public override void DrawFloatingPicture(LayoutFloatingPicture floatingPicture)
    {
        if (Form1.customDrawImage == true)
        {
            Rectangle bounds = floatingPicture.Bounds;
            Point startPoint = new Point(bounds.X + Canvas.ConvertToDrawingLayoutUnits(10, DocumentLayoutUnit.Pixel), bounds.Y + bounds.Height - Canvas.ConvertToDrawingLayoutUnits(40, DocumentLayoutUnit.Pixel));
            RichEditPen pEn = new RichEditPen(Color.Coral);
            pEn.Thickness = 30;
            pEn.DashStyle = RichEditDashStyle.Dash;
            Canvas.DrawEllipse(pEn, bounds);
            Canvas.DrawString("Approved", new Font("Courier New", 26), new RichEditBrush(Color.DarkMagenta), startPoint);
        }
        else
            base.DrawFloatingPicture(floatingPicture);
    }

    public override void DrawTableCell(LayoutTableCell tableCell)
    {
        if (Form1.customDrawTable == true)
        {
            Rectangle Tbounds = tableCell.Bounds;

            int x = Tbounds.X + Tbounds.Width/2 - Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel);
            int y = Tbounds.Y + Tbounds.Height/2 - Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel);
            Rectangle tableRectangle = new Rectangle(x,y, Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel), 
                Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel));

            Canvas.FillEllipse(new RichEditBrush(Color.MediumAquamarine), tableRectangle);
            base.DrawTableCell(tableCell);
        }
        else
            base.DrawTableCell(tableCell);
    }

   public override void DrawInlineDrawingObject(InlineDrawingObjectBox inlinePictureBox)
    {
        if (Form1.customDrawPicture == true)
        {
            Rectangle Ebounds = inlinePictureBox.Bounds;
            RichEditPen pen = new RichEditPen(Color.Maroon, 50);
            pen.DashStyle = RichEditDashStyle.Dot;
            Canvas.DrawLine(pen, new Point(Ebounds.X, Ebounds.Y + Ebounds.Height), new Point(Ebounds.X + Ebounds.Width, Ebounds.Y));
            Canvas.DrawLine(pen, new Point(Ebounds.X, Ebounds.Y), new Point(Ebounds.X + Ebounds.Width, Ebounds.Y + Ebounds.Height));
            Rectangle inlineRect = new Rectangle(Ebounds.X, Ebounds.Y, Ebounds.Width, Ebounds.Height);
            Canvas.DrawRectangle(new RichEditPen(Color.Aquamarine, 40), inlineRect);
        }
        else
            base.DrawInlineDrawingObject(inlinePictureBox);
    }

    public override void DrawTextBox(LayoutTextBox textBox)
    {
        if (Form1.customDrawTextBox == true)
        {
            Point[] StarPoints =
                {
            new Point(textBox.Bounds.X+textBox.Bounds.Width/2,textBox.Bounds.Y),
            new Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height),
            new Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height/2),
            new Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height/2),
            new Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height),
            new Point(textBox.Bounds.X+textBox.Bounds.Width/2,textBox.Bounds.Y)
                };
            Canvas.DrawLines(new RichEditPen(Color.HotPink, 30), StarPoints);
        }
        else
            base.DrawTextBox(textBox);
    }

    public override void DrawNumberingListWithSeparatorBox(NumberingListWithSeparatorBox numberingListWithSeparatorBox)
    {
        if (Form1.customDrawSeparator == true) Canvas.FillRectangle(new RichEditBrush(Color.DarkRed), numberingListWithSeparatorBox.Bounds);
        else base.DrawNumberingListWithSeparatorBox(numberingListWithSeparatorBox);
    }

    public override void DrawPage(LayoutPage page)
    {
        if (Form1.customDrawPage == true)
        {
            Rectangle inlineRect = new Rectangle(100, 100, 150, 200);
            Canvas.DrawRectangle(new RichEditPen(Color.Aquamarine, Canvas.ConvertToDrawingLayoutUnits(4, DocumentLayoutUnit.Pixel)), Canvas.ConvertToDrawingLayoutUnits(inlineRect, DocumentLayoutUnit.Pixel));

        }
        base.DrawPage(page);
    }
}
vb
Public Class MyLayoutPainter
    Inherits PagePainter

    Public Overrides Sub DrawPlainTextBox(ByVal plainTextBox As PlainTextBox)
        If Form1.customDrawText = True Then
            Canvas.DrawRectangle(New RichEditPen(Color.FromArgb(141, 179, 226)), plainTextBox.Bounds)
        Else
            MyBase.DrawPlainTextBox(plainTextBox)
        End If
    End Sub

    Public Overrides Sub DrawFloatingPicture(ByVal floatingPicture As LayoutFloatingPicture)
        If Form1.customDrawImage = True Then
            Dim bounds As Rectangle = floatingPicture.Bounds
            Dim startPoint As New Point(bounds.X + Canvas.ConvertToDrawingLayoutUnits(10, DocumentLayoutUnit.Pixel), bounds.Y + bounds.Height - Canvas.ConvertToDrawingLayoutUnits(40, DocumentLayoutUnit.Pixel))
            Dim pEn As New RichEditPen(Color.Coral)
            pEn.Thickness = 30
            pEn.DashStyle = RichEditDashStyle.Dash
            Canvas.DrawEllipse(pEn, bounds)
            Canvas.DrawString("Approved", New Font("Courier New", 26), New RichEditBrush(Color.DarkMagenta), startPoint)
        Else
            MyBase.DrawFloatingPicture(floatingPicture)
        End If
    End Sub

    Public Overrides Sub DrawTableCell(ByVal tableCell As LayoutTableCell)
        If Form1.customDrawTable = True Then
            Dim Tbounds As Rectangle = tableCell.Bounds

            Dim x As Integer = Tbounds.X + Tbounds.Width\2 - Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel)
            Dim y As Integer = Tbounds.Y + Tbounds.Height\2 - Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel)
            Dim tableRectangle As New Rectangle(x,y, Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel), Canvas.ConvertToDrawingLayoutUnits(20, DocumentLayoutUnit.Pixel))

            Canvas.FillEllipse(New RichEditBrush(Color.MediumAquamarine), tableRectangle)
            MyBase.DrawTableCell(tableCell)
        Else
            MyBase.DrawTableCell(tableCell)
        End If
    End Sub

    Public Overrides Sub DrawInlineDrawingObject(ByVal inlinePictureBox As InlineDrawingObjectBox)
        If Form1.customDrawPicture = True Then
            Dim Ebounds As Rectangle = inlinePictureBox.Bounds
            Dim pen As New RichEditPen(Color.Maroon, 50)
            pen.DashStyle = RichEditDashStyle.Dot
            Canvas.DrawLine(pen, New Point(Ebounds.X, Ebounds.Y + Ebounds.Height), New Point(Ebounds.X + Ebounds.Width, Ebounds.Y))
            Canvas.DrawLine(pen, New Point(Ebounds.X, Ebounds.Y), New Point(Ebounds.X + Ebounds.Width, Ebounds.Y + Ebounds.Height))
            Dim inlineRect As New Rectangle(Ebounds.X, Ebounds.Y, Ebounds.Width, Ebounds.Height)
            Canvas.DrawRectangle(New RichEditPen(Color.Aquamarine, 40), inlineRect)
        Else
            MyBase.DrawInlineDrawingObject(inlinePictureBox)
        End If
    End Sub

    Public Overrides Sub DrawTextBox(ByVal textBox As LayoutTextBox)
        If Form1.customDrawTextBox = True Then
            Dim StarPoints() As Point = {
                New Point(textBox.Bounds.X+textBox.Bounds.Width\2,textBox.Bounds.Y),
                New Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height),
                New Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height\2),
                New Point(textBox.Bounds.X+textBox.Bounds.Width,textBox.Bounds.Y+textBox.Bounds.Height\2),
                New Point(textBox.Bounds.X,textBox.Bounds.Y+textBox.Bounds.Height),
                New Point(textBox.Bounds.X+textBox.Bounds.Width\2,textBox.Bounds.Y)
            }
            Canvas.DrawLines(New RichEditPen(Color.HotPink, 30), StarPoints)
        Else
            MyBase.DrawTextBox(textBox)
        End If
    End Sub

    Public Overrides Sub DrawNumberingListWithSeparatorBox(ByVal numberingListWithSeparatorBox As NumberingListWithSeparatorBox)
        If Form1.customDrawSeparator = True Then
            Canvas.FillRectangle(New RichEditBrush(Color.DarkRed), numberingListWithSeparatorBox.Bounds)
        Else
            MyBase.DrawNumberingListWithSeparatorBox(numberingListWithSeparatorBox)
        End If
    End Sub

    Public Overrides Sub DrawPage(ByVal page As LayoutPage)
        If Form1.customDrawPage = True Then
            Dim inlineRect As New Rectangle(100, 100, 150, 200)
            Canvas.DrawRectangle(New RichEditPen(Color.Aquamarine, Canvas.ConvertToDrawingLayoutUnits(4, DocumentLayoutUnit.Pixel)), Canvas.ConvertToDrawingLayoutUnits(inlineRect, DocumentLayoutUnit.Pixel))

        End If
        MyBase.DrawPage(page)
    End Sub
End Class

Inheritance

Object PageCanvas

See Also

PageCanvas Members

DevExpress.XtraRichEdit.API.Layout Namespace