Working with Shapes in PostScript | .NET
Add Shapes in PS Document
Add Rectangle to PS
In order to add a rectangle to PsDocument with Aspose.Page for .NET library we should do the following steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a rectangle System.Drawing.Drawing2D.GraphicsPath from the rectangle.
- Set a paint to the current graphics state of PsDocument.
- Fill the rectangle path.
- Close the page.
- Save the document.
If we need to stroke (outline) a rectangle the first 4 and the last 2 steps will be the same, but points 5 and 6 will be:
Set the stroke to the current graphics state of PsDocument.
Stroke (outline) the rectangle path.
1//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "AddRectangle_outPS.ps", FileMode.Create))
3{
4 //Create save options with A4 size
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 //Create graphics path from the first rectangle
11 GraphicsPath path = new GraphicsPath();
12 path.AddRectangle(new RectangleF(250, 100, 150, 100));
13 //Set paint
14 document.SetPaint(new SolidBrush(Color.Orange));
15 //Fill the rectangle
16 document.Fill(path);
17
18 //Create graphics path from the second rectangle
19 path = new GraphicsPath();
20 path.AddRectangle(new RectangleF(250, 300, 150, 100));
21 //Set stroke
22 document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
23 //Stroke (outline) the rectangle
24 document.Draw(path);
25
26 //Close current page
27 document.ClosePage();
28
29 //Save the document
30 document.Save();
31}
For Linux, MacOS and other non-Windows operation systems we offer to use our Aspose.Page.Drawing Nuget package. It uses Aspose.Drawing backend instead of System.Drawing system library.
So import Aspose.Page.Drawing namespace instead of System.Drawing one. In the above and the following code snippets Aspose.Page.Drawing.Rectangle will be used instead of System.Drawing.Rectangle, Aspose.Page.Drawing.Drawing2D.GraphicsPath will be used instead of System.Drawing.Drawing2D.GraphicsPath and so on. Our code examples on GitHub contain all the necessary substitutions.
See working with shapes in PS documents in Java.
The result of running this code is appeared as
Add Ellipse to PS
In order to add ellipse to PsDocument also 8 steps are required:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create an ellipse System.Drawing.Drawing2D.GraphicsPath from the rectangle.
- Set paint to the current graphics state of PsDocument.
- Fill the ellipse path.
- Close the page.
- Save the document.
If we need to stroke (outline) an ellipse the first 4 and the last 2 steps will be the same but points 5 and 6 will be:
- Set stroke to the current graphics state of PsDocument.
- Stroke (outline) the ellipse path.
1//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "AddEllipse_outPS.ps", FileMode.Create))
3{
4 //Create save options with A4 size
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 //Create graphics path from the first ellipse
11 GraphicsPath path = new GraphicsPath();
12 path.AddEllipse(new RectangleF(250, 100, 150, 100));
13 //Set paint
14 document.SetPaint(new SolidBrush(Color.Orange));
15 //Fill the ellipse
16 document.Fill(path);
17
18 //Create graphics path from the second ellipse
19 path = new SystemGraphicsPath();
20 path.AddEllipse(new RectangleF(250, 300, 150, 100));
21 //Set stroke
22 document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
23 //Stroke (outline) the ellipse
24 document.Draw(path);
25
26 //Close current page
27 document.ClosePage();
28
29 //Save the document
30 document.Save();
31}
The result of running this code is appeared as
As we can see, any shape, both closed and unclosed, that can be put in System.Drawing.Drawing2D.GraphicsPathcan be filled or drawn by PsDocument. It also can be clipped, but it will be described in another article.
You can download examples and data files from GitHub.