Work with graphics states in PS
Clip and transform graphics states of PS files using API for .NET
Managing graphics states in PS documents (equivalent of canvases in XPS) is one of the main features offered by Aspose.Page for .NET. In the example below you will find out how to:
Save the current graphics state, create a new graphics state, and set it as current.
Translate, scale, rotate, or shear the current graphics state.
Set a complex transformation for the current graphics state.
Set paint and stroke for the current graphics state.
Fill and draw a graphics path.
Clip the graphics state.
Restore the previous graphics state.
To transform graphics states of a PS file follow the next guide:
- Create a PS file using the PsDocument Class .
- Create a rectangle graphics path.
- Save the current graphics state, create a new graphics stateand and set it as current with the WriteGraphicsSave() Method.
- Translate the current graphics state using the Translate() Method.
- Set the paint in the current graphics state with the SetPaint() Method.
- Fill the graphics path by means of the Fill() method.
- Restore previous graphics state with the WriteGraphicsRestore Method.
- Repeat the steps 3-7 for adding more graphics states with other transformations using Scale() , Rotate() , Shear() and Transform() Methods.
- Close the current page by means of the ClosePage() method.
- Save the created PS document using the PsDocument.Save() Method.
PS file graphics state Transformation C# code
using Aspose.Page.EPS;
using Aspose.Page.EPS.Device;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
//Create output stream for PostScript document
using (Stream outPsStream = new FileStream(dataDir + "Transformations_outPS.ps", FileMode.Create))
{
//Create save options with default values
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(outPsStream, options, false);
//Create graphics path from the rectangle
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddRectangle(new System.Drawing.RectangleF(0, 0, 150, 100));
///////////////////// Translation //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Displace current graphics state on 250 to the right. So we add translation component to the current transformation.
document.Translate(250, 0);
//Set paint in the current graphics state
document.SetPaint(new System.Drawing.SolidBrush(Color.Blue));
//Fill the second rectangle in the current graphics state (has translation transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Displace on 200 to the bottom.
document.Translate(0, 200);
////////////////////// Scaling //////////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Scale current graphics state on 0.5 in X axis and on 0.75f in Y axis. So we add scale component to the current transformation.
document.Scale(0.5f, 0.75f);
//Set paint in the current graphics state
document.SetPaint(new System.Drawing.SolidBrush(Color.Red));
//Fill the third rectangle in the current graphics state (has scale transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////
//Displace upper level graphics state on 250 to the right.
document.Translate(250, 0);
////////////////////// Rotation //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Rotate current graphics state on 45 degrees around origin of current graphics state (350, 300). So we add rotation component to the current transformation.
document.Rotate(45);
//Set paint in the current graphics state
document.SetPaint(new System.Drawing.SolidBrush(Color.Green));
//Fill the fourth rectangle in the current graphics state (has rotation transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////
//Returns upper level graphics state back to the left and displace on 200 to the bottom.
document.Translate(-250, 200);
////////////////////// Shearing //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Shear current graphics state. So we add shear component to the current transformation.
document.Shear(0.1f, 0.2f);
//Set paint in the current graphics state
document.SetPaint(new System.Drawing.SolidBrush(Color.Pink));
//Fill the fifth rectangle in the current graphics state (has shear transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////
//Displace upper level graphics state on 250 to the right.
document.Translate(250, 0);
////////////////////// Complex transformation ////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Transform current graphics state with complex transformation. So we add translation, scale and rotation components to the current transformation.
document.Transform(new System.Drawing.Drawing2D.Matrix(1.2f, -0.965925f, 0.258819f, 1.5f, 0f, 50));
//Set paint in the current graphics state
document.SetPaint(new System.Drawing.SolidBrush(Color.Aquamarine));
//Fill the sixth rectangle in the current graphics state (has complex transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////
//Close current page
document.ClosePage();
//Save the document
document.Save();
}
To add Clip to graphics state of a PS file follow the next guide:
- Create a PS file using the PsDocument Class .
- Create a rectangle graphics path.
- Save the current graphics state, create a new graphics stateand and set it as current with the WriteGraphicsSave() Method.
- Translate the current graphics state using the Translate() Method.
- Create a circle graphics path.
- Add clipping by circle to the current graphics state using the Clip() Method.
- Set the paint in the current graphics state with the SetPaint() Method.
- Fill the rectangle graphics path by means of the Fill() Method.
- Restore the previous graphics state with WriteGraphicsRestore() Method.
- Translate the current graphics state using the Translate() Method.
- Create a System.Drawing.Pen object.
- Set a stroke in the current graphics state with the SetStroke() Method.
- Draw the rectangle graphics path above the clipped rectangle by means of the Draw() Method.
- Close the current page by means of the ClosePage() Method.
- Save created PS document using the PsDocument.Save() Method.
PS file graphics state clipping C# code
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
//Create output stream for PostScript document
using (Stream outPsStream = new FileStream(dataDir + "Clipping_outPS.ps", FileMode.Create))
{
//Create save options with default values
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(outPsStream, options, false);
//Create graphics path from the rectangle
System.Drawing.Drawing2D.GraphicsPath rectangePath = new System.Drawing.Drawing2D.GraphicsPath();
rectangePath.AddRectangle(new System.Drawing.RectangleF(0, 0, 300, 200));
////////////////////// Clipping by shape //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Displace current graphics state on 100 points to the right and 100 points to the bottom.
document.Translate(100, 100);
//Create graphics path from the circle
System.Drawing.Drawing2D.GraphicsPath circlePath = new System.Drawing.Drawing2D.GraphicsPath();
circlePath.AddEllipse(new System.Drawing.RectangleF(50, 0, 200, 200));
//Add clipping by circle to the current graphics state
document.Clip(circlePath);
//Set paint in the current graphics state
document.SetPaint(new System.Drawing.SolidBrush(Color.Blue));
//Fill the rectangle in the current graphics state (with clipping)
document.Fill(rectangePath);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//Displace upper level graphics state on 100 points to the right and 100 points to the bottom.
document.Translate(100, 100);
Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
pen.DashStyle = DashStyle.Dash;
document.SetStroke(pen);
//Draw the rectangle in the current graphics state (has no clipping) above clipped rectngle
document.Draw(rectangePath);
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Close current page
document.ClosePage();
//Save the document
document.Save();
}
FAQ
1. What are graphics states in PostScript (PS) documents?
Graphics states in PostScript are the current settings and attributes applied to graphical elements within the document. They include parameters such as the current transformation matrix, line style, fill color, clipping path, and other graphic attributes that affect how elements are rendered on the page.
2. How can I manage graphics states in PS documents?
This can be done using PostScript commands such as gsave
and grestore
to save and restore the graphics state, respectively. Additionally, operators like setlinewidth
, setrgbcolor
, and setdash
can be used to modify specific attributes of the graphics state as needed.
3. Why is it important to understand and manage graphics states in PostScript (PS) documents?
By manipulating graphics states, you can achieve effects such as transformations, color changes, and clipping regions, ensuring that your graphics are displayed correctly and according to your design specifications. It also helps optimize file size and performance, particularly when dealing with complex or repetitive graphical elements.
PS What is PS File Format
PS format is one of the page description language (PDL) formats. It is capable to contain graphic as well as text information on the page. That is why the format was supported by most of the programs for image editing. The postscript file itself is a kind of instruction for printers. It contains information on what and how to print from its page.