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:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions object with default options.
  3. Create a 1-paged PsDocument with an already created output stream and save options.
  4. Create a rectangle System.Drawing.Drawing2D.GraphicsPath from the rectangle.
  5. Set a paint to the current graphics state of PsDocument.
  6. Fill the rectangle path.
  7. Close the page.
  8. 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:

  1. Set the stroke to the current graphics state of PsDocument.

  2. Stroke (outline) the rectangle path.

 1// Add Rectangle to PS document.
 2
 3string outputFileName = "AddRectangle_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
10
11//Create graphics path from the first rectangle
12GraphicsPath path = new GraphicsPath();
13path.AddRectangle(new RectangleF(250, 100, 150, 100));
14//Set paint
15document.SetPaint(new SolidBrush(Color.Orange));
16//Fill the rectangle
17document.Fill(path);
18
19//Create graphics path from the second rectangle
20path = new GraphicsPath();
21path.AddRectangle(new RectangleF(250, 300, 150, 100));
22//Set stroke
23document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
24//Stroke (outline) the rectangle
25document.Draw(path);
26
27//Close current page
28document.ClosePage();
29
30//Save the document
31document.Save();

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 Rectangle

Add Ellipse to PS

In order to add ellipse to PsDocument also 8 steps are required:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions object with default options.
  3. Create a 1-paged PsDocument with an already created output stream and save options.
  4. Create an ellipse System.Drawing.Drawing2D.GraphicsPath from the rectangle.
  5. Set paint to the current graphics state of PsDocument.
  6. Fill the ellipse path.
  7. Close the page.
  8. 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:

  1. Set stroke to the current graphics state of PsDocument.
  2. Stroke (outline) the ellipse path.
 1// Add ellipse to PS document.
 2
 3string outputFileName = "AddEllipse_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
10
11//Create graphics path from the first ellipse
12GraphicsPath path = new GraphicsPath();
13path.AddEllipse(new RectangleF(250, 100, 150, 100));
14//Set paint
15document.SetPaint(new SolidBrush(Color.Orange));
16//Fill the ellipse
17document.Fill(path);
18
19//Create graphics path from the second ellipse
20path = new GraphicsPath();
21path.AddEllipse(new RectangleF(250, 300, 150, 100));
22//Set stroke
23document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
24//Stroke (outline) the ellipse
25document.Draw(path);
26
27//Close current page
28document.ClosePage();
29
30//Save the document
31document.Save();
Example-AddEllipsePS.cs hosted with ❤ by GitHub

The result of running this code is appeared as

Add Ellipse

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.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.