Back to Devexpress

PdfGraphics.RestoreGraphicsState() Method

officefileapi-devexpress-dot-pdf-dot-pdfgraphics-cea306f8.md

latest4.9 KB
Original Source

PdfGraphics.RestoreGraphicsState() Method

Restores the saved graphics state.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public void RestoreGraphicsState()
vb
Public Sub RestoreGraphicsState

Remarks

The graphics state is a data structure that contains the drawing parameters of the PdfGraphics object (the clip region in which graphics are drawn and the transformation matrix). Call the SaveGraphicsState method to save the current graphics state to the stack.

After you modify the PdfGraphics object’s drawing parameters, call the RestoreGraphicsState paired method to return the parameters to the most recently saved graphics state. This graphics state is removed from the stack when you call the RestoreGraphicsState method.

You can nest calls to the SaveGraphicsState method to save multiple graphics states. Pair each call to the SaveGraphicsState method with the RestoreGraphicsState method.

The following example saves the current graphics state, draws a shape in the translated coordinate system, restores the saved graphics state, and draws another shape in the restored coordinate system.

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

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.CreateEmptyDocument();
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4);
        using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem())
        {
            // Save the current graphics state (the coordinate system origin is (0, 0)).
            graphics.SaveGraphicsState();

            // Translate the origin of the coordinate system to the point (300, 300).
            graphics.TranslateTransform(300, 300);

            // Draw a blue square in the translated coordinate system.
            using (var brush = new DXSolidBrush(Color.Blue))
                graphics.FillRectangle(brush, new RectangleF(0, 0, 200, 200));

            // Restore the saved graphics state (the coordinate system origin is (0, 0)).
            graphics.RestoreGraphicsState();

            // Draw a red square in the restored coordinate system.
            using (var brush = new DXSolidBrush(Color.Red))
                graphics.FillRectangle(brush, new RectangleF(0, 0, 200, 200));

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

Using processor As New PdfDocumentProcessor()
    processor.CreateEmptyDocument()
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4)
        Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
            ' Save the current graphics state (the coordinate system origin is (0, 0)).
            graphics.SaveGraphicsState()

            ' Translate the origin of the coordinate system to the point (300, 300).
            graphics.TranslateTransform(300, 300)

            ' Draw a blue square in the translated coordinate system.
            Using brush = New DXSolidBrush(Color.Blue)
                graphics.FillRectangle(brush, New RectangleF(0, 0, 200, 200))
            End Using

            ' Restore the saved graphics state (the coordinate system origin is (0, 0)).
            graphics.RestoreGraphicsState()

            ' Draw a red square in the restored coordinate system.
            Using brush = New DXSolidBrush(Color.Red)
                graphics.FillRectangle(brush, New RectangleF(0, 0, 200, 200))
            End Using

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

See Also

SaveGraphicsState()

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace