Working with Shapes in PS file | Python

Add Shapes to PS Document

Add Rectangle to PS

In order to insert a rectangle into PsDocument using Aspose.Page for Python via .NET library take the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create a PsSaveOptions object with the default options.
  3. Create a 1-paged PsDocument with an already created output stream and save options.
  4. Create a rectangle aspose.pydrawing.GraphicsPath from the rectangle.
  5. Set a paint to the current graphics state of PsDocument.
  6. Fill the rectangle.
  7. Close the page.
  8. Save the document.

To stroke (outline) a rectangle the first 4 and the last 2 steps will be the same, but the 5-th and 6-th one will be:

  1. Set the stroke to the current graphics state of the PsDocument.
  2. Stroke (outline) the rectangle.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "AddRectangle_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first rectangle
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_rectangle(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the rectangle
18    document.fill(path)
19    
20    # Create a graphics path from the second rectangle
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_rectangle(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the rectangle
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

See working with shapes in PS documents in .NET, Java.

The result of running this code is

Add Rectangle

Add Ellipse to PS

To add an ellipse to PsDocument also 8 steps are required to be taken:

  1. Create an output stream for the resulting PS file.
  2. Create a PsSaveOptions object with the default options.
  3. Create a 1-paged PsDocument with an already created output stream and save options.
  4. Create an ellipse aspose.pydrawing.drawing2d.GraphicsPath from the rectangle.
  5. Set the paint to the current graphics state of PsDocument.
  6. Fill the ellipse path.
  7. Close the page.
  8. Save the document.

To stroke (outline) an ellipse the first 4 and the last 2 steps will be the same but the 5-th and 6-th one will be:

  1. Set stroke to the current graphics state of PsDocument.
  2. Stroke (outline) the ellipse.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for PostScript document
 5with open(data_dir + "AddEllipse_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first ellipse
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_ellipse(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the ellipse
18    document.fill(path)
19    
20    # Create a graphics path from the second ellipse
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_ellipse(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set the stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the ellipse
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

The result of running this code is

Add Ellipse

As we can see, any shape, both closed and unclosed, can be filled or drawn by PsDocument. It can also be clipped, but it will be described in a different 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.