Reference Guide
GdPicture14 Namespace / GdPicturePDF Class / RestoreGraphicsState Method
Example





In This Topic
    RestoreGraphicsState Method (GdPicturePDF)
    In This Topic
    Restores the graphics state previously saved by the SaveGraphicsState method. Restoring the graphics state means removing the most recently saved graphics state from the graphics state stack and making it the current graphics state. Please note that SaveGraphicsState and following RestoreGraphicsState calls need to be applied on the same page, in other words changing the current page between these two calls causes incorrect behaviour of the graphics state.

    The graphics on a PDF page are described by a sequence of draw operators, graphics state operators, and marked-content operators. The graphics state operators change the graphics state (set the line width, dash pattern, fill color, font size etc) and the result of draw operators are affected by the graphics state. Please see also the ResetGraphicsState method for more details.

    Syntax
    'Declaration
     
    
    Public Function RestoreGraphicsState() As GdPictureStatus
    public GdPictureStatus RestoreGraphicsState()
    public function RestoreGraphicsState(): GdPictureStatus; 
    public function RestoreGraphicsState() : GdPictureStatus;
    public: GdPictureStatus RestoreGraphicsState(); 
    public:
    GdPictureStatus RestoreGraphicsState(); 

    Return Value

    A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.

    We strongly recommend always checking this status first.

    Remarks
    Please note that this method is only allowed for use with non-encrypted documents.

    Be aware that changing the current page between calls to SaveGraphicsState and RestoreGraphicsState methods causes incorrect behaviour of the graphics state. According to the PDF Reference, Section "Graphics State", the graphics state parameters are initialized to their default values at the beginning of each page.

    Example
    How to correctly save and restore graphics state to draw various rectangles onto the newly created PDF document.
    Dim caption As String = "Example: RestoreGraphicsState"
    Dim oGdPicturePDF As New GdPicturePDF()
    Dim status As GdPictureStatus = oGdPicturePDF.NewPDF()
    If status = GdPictureStatus.OK Then
        oGdPicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
        oGdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
        'This is the font only used to describe the drawn rectangles.
        Dim fontName As String = oGdPicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
        If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
            If (oGdPicturePDF.NewPage(210, 297) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.ResetGraphicsState() = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetLineWidth(0.5F) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetLineColor(0, 0, 255) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(0, 255, 255) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawRectangle(10, 10, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawText(fontName, 15, 15, "1. rectangle") = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(0, 255, 255) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SaveGraphicsState() = GdPictureStatus.OK) AndAlso 'Saving the graphics state.
               (oGdPicturePDF.SaveGraphicsState() = GdPictureStatus.OK) AndAlso 'Saving the graphics state again.
               (oGdPicturePDF.AddTransformationMatrix(1, 0, 0, 1, 150, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetLineColor(255, 0, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(128, 0, 128) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawRectangle(10, 10, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawText(fontName, 15, 15, "2. rectangle") = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.RestoreGraphicsState() = GdPictureStatus.OK) AndAlso 'Restoring the preceding graphics state.
               (oGdPicturePDF.AddTransformationMatrix(1, 0.1F, 0, 1, 150, -100) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetLineColor(0, 128, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(173, 255, 47) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawRectangle(10, 10, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawText(fontName, 15, 15, "3. rectangle") = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.RestoreGraphicsState() = GdPictureStatus.OK) AndAlso 'Restoring the initial graphics state.
               (oGdPicturePDF.DrawRectangle(10, 80, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawText(fontName, 15, 85, "4. rectangle") = GdPictureStatus.OK) AndAlso '1. and 4. rectangle use the same colors.
               (oGdPicturePDF.AddTransformationMatrix(1, 0.1F, 0.1F, 1, 100, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetLineColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(255, 255, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawRectangle(10, 80, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.DrawText(fontName, 15, 85, "5. rectangle") = GdPictureStatus.OK) Then
                status = oGdPicturePDF.SaveToFile("test_GraphicState.pdf")
                If status = GdPictureStatus.OK Then
                    MessageBox.Show("The example has been followed successfully and the file has been saved.", caption)
                Else
                    MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption)
                End If
            Else
                MessageBox.Show("The example has not been followed successfully." + vbCrLf + "The last known status is " + oGdPicturePDF.GetStat().ToString(), caption)
            End If
        Else
            MessageBox.Show("The AddStandardFont() method has failed with the status: " + status.ToString(), caption)
        End If
    Else
        MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption)
    End If
    oGdPicturePDF.Dispose()
    string caption = "Example: RestoreGraphicsState";
    GdPicturePDF oGdPicturePDF = new GdPicturePDF();
    GdPictureStatus status = oGdPicturePDF.NewPDF();
    if (status == GdPictureStatus.OK)
    {
        oGdPicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
        oGdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
        //This is the font only used to describe the drawn rectangles.
        string fontName = oGdPicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
        if (oGdPicturePDF.GetStat() == GdPictureStatus.OK)
        {
            if ((oGdPicturePDF.NewPage(210, 297) == GdPictureStatus.OK) &&
                (oGdPicturePDF.ResetGraphicsState() == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetLineWidth(0.5f) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetLineColor(0, 0, 255) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(0, 255, 255) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawRectangle(10, 10, 40, 20, true, true) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawText(fontName, 15, 15, "1. rectangle") == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(0, 255, 255) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SaveGraphicsState() == GdPictureStatus.OK) && //Saving the graphics state.
                (oGdPicturePDF.SaveGraphicsState() == GdPictureStatus.OK) && //Saving the graphics state again.
                (oGdPicturePDF.AddTransformationMatrix(1, 0, 0, 1, 150, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetLineColor(255, 0, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(128, 0, 128) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawRectangle(10, 10, 40, 20, true, true) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawText(fontName, 15, 15, "2. rectangle") == GdPictureStatus.OK) &&
                (oGdPicturePDF.RestoreGraphicsState() == GdPictureStatus.OK) && //Restoring the preceding graphics state.
                (oGdPicturePDF.AddTransformationMatrix(1, 0.1f, 0, 1, 150, -100) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetLineColor(0, 128, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(173, 255, 47) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawRectangle(10, 10, 40, 20, true, true) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawText(fontName, 15, 15, "3. rectangle") == GdPictureStatus.OK) &&
                (oGdPicturePDF.RestoreGraphicsState() == GdPictureStatus.OK) && //Restoring the initial graphics state.
                (oGdPicturePDF.DrawRectangle(10, 80, 40, 20, true, true) == GdPictureStatus.OK) && //1. and 4. rectangle use the same colors.
                (oGdPicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawText(fontName, 15, 85, "4. rectangle") == GdPictureStatus.OK) &&
                (oGdPicturePDF.AddTransformationMatrix(1, 0.1f, 0.1f, 1, 100, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetLineColor(0, 0, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(255, 255, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawRectangle(10, 80, 40, 20, true, true) == GdPictureStatus.OK) &&
                (oGdPicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
                (oGdPicturePDF.DrawText(fontName, 15, 85, "5. rectangle") == GdPictureStatus.OK))
            {
                status = oGdPicturePDF.SaveToFile("test_GraphicState.pdf");
                if (status == GdPictureStatus.OK)
                    MessageBox.Show("The example has been followed successfully and the file has been saved.", caption);
                else
                    MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption);
            }
            else
                MessageBox.Show("The example has not been followed successfully.\nThe last known status is " + oGdPicturePDF.GetStat().ToString(), caption);
        }
        else
            MessageBox.Show("The AddStandardFont() method has failed with the status: " + status.ToString(), caption);
    }
    else
        MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption);
    oGdPicturePDF.Dispose();
    See Also