GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / ResetGraphicsState Method
Example





In This Topic
ResetGraphicsState Method (GdPicturePDF)
In This Topic
Resets the graphics state parameters to their default configuration. For example, any specified transformation is canceled, the fill color and the stroke color are set to their default values, etc. It's a good practice to call this method before adding new data to the currently loaded PDF document.

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.

According to the PDF Reference, Section "Graphics State", a PDF consumer application maintains an internal data structure called the graphics state that holds current graphics control parameters. These parameters define the global framework within which the graphics operators execute. For example, the fill operator implicitly uses the current color parameter, and the stroke operator additionally uses the current line width parameter from the graphics state. The graphics state is initialized at the beginning of each page with the values specified in the PDF Reference, Section "Graphics State".

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

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
This method is only allowed for use with non-encrypted documents.

Be aware that graphics state parameters are initialized to their default values at the beginning of each page. Therefore we recommend to call this method before adding new data to the existing PDF document.

Example
How to reset the graphics state to its default configuration.
Dim caption As String = "Example: ResetGraphicsState"
Dim gdpicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.NewPDF()
If status = GdPictureStatus.OK Then
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
    'This is the font only used to describe the drawn rectangles.
    Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        If (gdpicturePDF.NewPage(210, 297) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.ResetGraphicsState() = GdPictureStatus.OK) AndAlso 'Setting the default graphics state.
           (gdpicturePDF.DrawRectangle(120, 10, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(255, 255, 255) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawText(fontName, 125, 15, "default state") = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetLineWidth(1) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetLineColor(0, 0, 255) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(0, 255, 255) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawRectangle(10, 10, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawText(fontName, 15, 15, "1. rectangle") = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(0, 255, 255) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SaveGraphicsState() = GdPictureStatus.OK) AndAlso 'Saving the graphics state.
           (gdpicturePDF.SaveGraphicsState() = GdPictureStatus.OK) AndAlso 'Save the graphics state again.
           (gdpicturePDF.AddTransformationMatrix(1, 0, 0, 1, 150, 0) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetLineColor(255, 0, 0) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(128, 0, 128) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawRectangle(10, 10, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawText(fontName, 15, 15, "2. rectangle") = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.RestoreGraphicsState() = GdPictureStatus.OK) AndAlso 'Restoring the preceding graphics state.
           (gdpicturePDF.AddTransformationMatrix(1, 0.1F, 0, 1, 150, -100) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetLineColor(0, 128, 0) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(173, 255, 47) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawRectangle(10, 10, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawText(fontName, 15, 15, "3. rectangle") = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.RestoreGraphicsState() = GdPictureStatus.OK) AndAlso 'Restoring the initial graphics state.
           (gdpicturePDF.DrawRectangle(10, 50, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawText(fontName, 15, 55, "4. rectangle") = GdPictureStatus.OK) AndAlso '1. and 4. rectangle use the same colors.
           (gdpicturePDF.ResetGraphicsState() = GdPictureStatus.OK) AndAlso 'Resetting to the default state again.
           (gdpicturePDF.DrawRectangle(120, 50, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFillColor(255, 255, 255) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.DrawText(fontName, 125, 55, "default state") = GdPictureStatus.OK) Then
            status = gdpicturePDF.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 " + gdpicturePDF.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
gdpicturePDF.Dispose()
string caption = "Example: ResetGraphicsState";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.NewPDF();
if (status == GdPictureStatus.OK)
{
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
    //This is the font only used to describe the drawn rectangles.
    string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        if ((gdpicturePDF.NewPage(210, 297) == GdPictureStatus.OK) &&
            (gdpicturePDF.ResetGraphicsState() == GdPictureStatus.OK) && //Setting the default graphics state.
            (gdpicturePDF.DrawRectangle(120, 10, 40, 20, true, true) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(255, 255, 255) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawText(fontName, 125, 15, "default state") == GdPictureStatus.OK) &&
            (gdpicturePDF.SetLineWidth(1) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetLineColor(0, 0, 255) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(0, 255, 255) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawRectangle(10, 10, 40, 20, true, true) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawText(fontName, 15, 15, "1. rectangle") == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(0, 255, 255) == GdPictureStatus.OK) &&
            (gdpicturePDF.SaveGraphicsState() == GdPictureStatus.OK) && //Saving the graphics state.
            (gdpicturePDF.SaveGraphicsState() == GdPictureStatus.OK) && //Saving the graphics state again.
            (gdpicturePDF.AddTransformationMatrix(1, 0, 0, 1, 150, 0) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetLineColor(255, 0, 0) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(128, 0, 128) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawRectangle(10, 10, 40, 20, true, true) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawText(fontName, 15, 15, "2. rectangle") == GdPictureStatus.OK) &&
            (gdpicturePDF.RestoreGraphicsState() == GdPictureStatus.OK) && //Restoring the preceding graphics state.
            (gdpicturePDF.AddTransformationMatrix(1, 0.1f, 0, 1, 150, -100) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetLineColor(0, 128, 0) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(173, 255, 47) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawRectangle(10, 10, 40, 20, true, true) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawText(fontName, 15, 15, "3. rectangle") == GdPictureStatus.OK) &&
            (gdpicturePDF.RestoreGraphicsState() == GdPictureStatus.OK) && //Restoring the initial graphics state.
            (gdpicturePDF.DrawRectangle(10, 50, 40, 20, true, true) == GdPictureStatus.OK) && //1. and 4. rectangle use the same colors.
            (gdpicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawText(fontName, 15, 55, "4. rectangle") == GdPictureStatus.OK) &&
            (gdpicturePDF.ResetGraphicsState() == GdPictureStatus.OK) && //Resetting to the default state again.
            (gdpicturePDF.DrawRectangle(120, 50, 40, 20, true, true) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFillColor(255, 255, 255) == GdPictureStatus.OK) &&
            (gdpicturePDF.DrawText(fontName, 125, 55, "default state") == GdPictureStatus.OK))
        {
            status = gdpicturePDF.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 " + gdpicturePDF.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);
gdpicturePDF.Dispose();
See Also