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:

      5. Set the stroke to the current graphics state of PsDocument.
      6. 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    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
12    path.AddRectangle(new System.Drawing.RectangleF(250, 100, 150, 100));
13    //Set paint
14    document.SetPaint(new System.Drawing.SolidBrush(Color.Orange));
15    //Fill the rectangle
16    document.Fill(path);
17
18    //Create graphics path from the second rectangle
19    path = new System.Drawing.Drawing2D.GraphicsPath();
20    path.AddRectangle(new System.Drawing.RectangleF(250, 300, 150, 100));
21    //Set stroke
22    document.SetStroke(new System.Drawing.Pen(new System.Drawing.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}

See working with shapes in PS documents in Java and C++.


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:

      5. Set stroke to the current graphics state of PsDocument.
      6. 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    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
12    path.AddEllipse(new System.Drawing.RectangleF(250, 100, 150, 100));
13    //Set paint
14    document.SetPaint(new System.Drawing.SolidBrush(Color.Orange));
15    //Fill the ellipse
16    document.Fill(path);
17
18    //Create graphics path from the second ellipse
19    path = new System.Drawing.Drawing2D.GraphicsPath();
20    path.AddEllipse(new System.Drawing.RectangleF(250, 300, 150, 100));
21    //Set stroke
22    document.SetStroke(new System.Drawing.Pen(new System.Drawing.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

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.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.