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.
 1from aspose.page import *
 2from aspose.page.eps import *
 3from aspose.page.eps.device import *
 4import aspose.pydrawing
 5from util import Util
 6###############################################
 7###### Class and Method declaration here ######
 8###############################################
 9
10# The path to the documents directory.
11data_dir = Util.get_data_dir_working_with_shapes()
12
13# Create an output stream for the PostScript document
14with open(data_dir + "AddRectangle_outPS.ps", "wb") as out_ps_stream:
15    # Create save options with A4 size
16    options = PsSaveOptions()
17    
18    # Create a new 1-paged PS Document
19    document = PsDocument(out_ps_stream, options, False)
20    
21    # Create a graphics path from the first rectangle
22    path = aspose.pydrawing.drawing2d.GraphicsPath()
23    path.add_rectangle(aspose.pydrawing.RectangleF(250, 100, 150, 100))
24    # Set the paint
25    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
26    # Fill the rectangle
27    document.fill(path)
28    
29    # Create a graphics path from the second rectangle
30    path = aspose.pydrawing.drawing2d.GraphicsPath()
31    path.add_rectangle(aspose.pydrawing.RectangleF(250, 300, 150, 100))
32    # Set stroke
33    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
34    # Stroke (outline) the rectangle
35    document.draw(path)
36    
37    # Close the current page
38    document.close_page()
39    
40    # Save the document
41    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.
 1from aspose.page import *
 2from aspose.page.eps import *
 3from aspose.page.eps.device import *
 4import aspose.pydrawing
 5from util import Util
 6###############################################
 7###### Class and Method declaration here ######
 8###############################################
 9
10# The path to the documents directory.
11data_dir = Util.get_data_dir_working_with_shapes()
12
13# Create an output stream for PostScript document
14with open(data_dir + "AddEllipse_outPS.ps", "wb") as out_ps_stream:
15    # Create save options with the A4 size
16    options = PsSaveOptions()
17    
18    # Create a new 1-paged PS Document
19    document = PsDocument(out_ps_stream, options, False)
20    
21    # Create a graphics path from the first ellipse
22    path = aspose.pydrawing.drawing2d.GraphicsPath()
23    path.add_ellipse(aspose.pydrawing.RectangleF(250, 100, 150, 100))
24    # Set the paint
25    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
26    # Fill the ellipse
27    document.fill(path)
28    
29    # Create a graphics path from the second ellipse
30    path = aspose.pydrawing.drawing2d.GraphicsPath()
31    path.add_ellipse(aspose.pydrawing.RectangleF(250, 300, 150, 100))
32    # Set the stroke
33    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
34    # Stroke (outline) the ellipse
35    document.draw(path)
36    
37    # Close the current page
38    document.close_page()
39    
40    # Save the document
41    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.