Back to Devexpress

PdfGraphics.SaveGraphicsState() Method

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

latest4.9 KB
Original Source

PdfGraphics.SaveGraphicsState() Method

Saves the current graphics state to the stack.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public void SaveGraphicsState()
vb
Public Sub SaveGraphicsState

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.

Example

Example: Save and Restore Graphics State

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 System.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 SolidBrush(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 SolidBrush(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 System.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 SolidBrush(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 SolidBrush(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

RestoreGraphicsState()

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace