Working with Shapes in PS file | Java
Add Shapes in PS Document
Add Rectangle to PS
In order to add a rectangle to PsDocument with Aspose.Page for Java library we should do the following steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a rectangle (java.awt.geom.Rectangle2D object).
- Set a paint to the current graphics state of PsDocument.
- Fill the rectangle.
- Close the page.
- 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:
- Set the stroke to the current graphics state of PsDocument.
- Stroke (outline) the rectangle.
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(getOutputDir() + outputFileName, options, false);
10
11//Create graphics path from the first rectangle
12GeneralPath path = new GeneralPath();
13path.append(new Rectangle2D.Float(250, 100, 150, 100), false);
14//Set paint
15document.setPaint(Color.ORANGE);
16//Fill the rectangle
17document.fill(path);
18
19//Create graphics path from the second rectangle
20path = new GeneralPath();
21path.append(new Rectangle2D.Float(250, 300, 150, 100), false);
22//Set stroke
23document.setStroke(new java.awt.BasicStroke(3));
24//Stroke (outline) the rectangle
25document.draw(path);
26
27//Close current page
28document.closePage();
29
30//Save the document
31document.save();See working with shapes in PS documents in .NET.
The result of running this code is appeared as

Add Ellipse to PS
In order to add ellipse to PsDocument also 8 steps are required:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create an ellipse (java.awt.geom.Ellipse2D object).
- Set paint to the current graphics state of PsDocument.
- Fill the ellipse path.
- Close the page.
- 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:
- Set stroke to the current graphics state of PsDocument.
- Stroke (outline) the ellipse:
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(getOutputDir() + outputFileName, options, false);
10
11//Create graphics path from the first ellipse
12GeneralPath path = new GeneralPath();
13path.append(new java.awt.geom.Ellipse2D.Float(250, 100, 150, 100), false);
14//Set paint
15document.setPaint(Color.ORANGE);
16//Fill the ellipse
17document.fill(path);
18
19//Create graphics path from the second ellipse
20path = new GeneralPath();
21path.append(new java.awt.geom.Ellipse2D.Float(250, 300, 150, 100), false);
22//Set stroke
23document.setStroke(new java.awt.BasicStroke(3));
24//Stroke (outline) the ellipse
25document.draw(path);
26
27//Close current page
28document.closePage();
29
30//Save the document
31document.save();The result of running this code is appeared as

As we can see, any shape, both closed and unclosed, can 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.