Skip to main content

PdfGraphics.SaveGraphicsState() Method

Saves the current graphics state to the stack.

Namespace: DevExpress.Pdf

Assembly: DevExpress.Pdf.v24.1.Drawing.dll

NuGet Package: DevExpress.Pdf.Drawing

Declaration

public void 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

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.

restore

using DevExpress.Pdf;
using System.Drawing;
//...

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

            // 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, 72, 72);
        }
    processor.SaveDocument("out2.pdf");
}
Process.Start("out.pdf");
See Also