RestoreGraphicsState Method (GdPicturePDF)
In This Topic
Restores the graphics state previously saved by the
GdPicturePDF.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
GdPicturePDF.SaveGraphicsState and following
GdPicturePDF.RestoreGraphicsState calls need to be applied on the same page, in other words changing the current page between these two calls causes incorrect behavior 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 GdPicturePDF.ResetGraphicsState method for more details.
Syntax
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.
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 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
(gdpicturePDF.SetLineWidth(0.5F) = 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 'Saving 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, 80, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 15, 85, "4. rectangle") = GdPictureStatus.OK) AndAlso '1. and 4. rectangle use the same colors.
(gdpicturePDF.AddTransformationMatrix(1, 0.1F, 0.1F, 1, 100, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetLineColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(255, 255, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawRectangle(10, 80, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 15, 85, "5. rectangle") = 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: RestoreGraphicsState";
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) &&
(gdpicturePDF.SetLineWidth(0.5f) == 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, 80, 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, 85, "4. rectangle") == GdPictureStatus.OK) &&
(gdpicturePDF.AddTransformationMatrix(1, 0.1f, 0.1f, 1, 100, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.SetLineColor(0, 0, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.SetFillColor(255, 255, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawRectangle(10, 80, 40, 20, true, true) == GdPictureStatus.OK) &&
(gdpicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 15, 85, "5. rectangle") == 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();
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 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
(gdpicturePDF.SetLineWidth(0.5F) = 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 'Saving 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, 80, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 15, 85, "4. rectangle") = GdPictureStatus.OK) AndAlso '1. and 4. rectangle use the same colors.
(gdpicturePDF.AddTransformationMatrix(1, 0.1F, 0.1F, 1, 100, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetLineColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(255, 255, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawRectangle(10, 80, 40, 20, True, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(0, 0, 0) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 15, 85, "5. rectangle") = 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: RestoreGraphicsState";
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) &&
(gdpicturePDF.SetLineWidth(0.5f) == 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, 80, 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, 85, "4. rectangle") == GdPictureStatus.OK) &&
(gdpicturePDF.AddTransformationMatrix(1, 0.1f, 0.1f, 1, 100, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.SetLineColor(0, 0, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.SetFillColor(255, 255, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawRectangle(10, 80, 40, 20, true, true) == GdPictureStatus.OK) &&
(gdpicturePDF.SetFillColor(0, 0, 0) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 15, 85, "5. rectangle") == 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