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:
- Create an output stream for the resulting PS file.
- Create a PsSaveOptions object with the default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a rectangle aspose.pydrawing.GraphicsPath from the rectangle.
- Set a paint to the current graphics state of PsDocument.
- Fill the rectangle.
- Close the page.
- 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:
- Set the stroke to the current graphics state of the PsDocument.
- 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()
The result of running this code is
Add Ellipse to PS
To add an ellipse to PsDocument also 8 steps are required to be taken:
- Create an output stream for the resulting PS file.
- Create a PsSaveOptions object with the default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create an ellipse aspose.pydrawing.drawing2d.GraphicsPath from the rectangle.
- Set the paint to the current graphics state of PsDocument.
- Fill the ellipse path.
- Close the page.
- 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:
- Set stroke to the current graphics state of PsDocument.
- 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
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.