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:

  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 (java.awt.geom.Rectangle2D object).
  5. Set a paint to the current graphics state of PsDocument.
  6. Fill the rectangle.
  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:

  1. Set the stroke to the current graphics state of PsDocument.
  2. Stroke (outline) the rectangle.
 1//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddRectangle_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Set paint for filling rectangle
10document.setPaint(Color.ORANGE);        
11//Fill the first rectangle
12document.fill(new Rectangle2D.Float(250, 100, 150, 100));
13
14//Set paint for stroking rectangle
15document.setPaint(Color.RED);
16//Set stroke
17document.setStroke(new BasicStroke(3));
18//Stroke (outline) the second rectangle
19document.draw(new Rectangle2D.Float(250, 300, 150, 100));
20
21//Close current page
22document.closePage();
23//Save the document
24document.save();

See working with shapes in PS documents in .NET.


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 (java.awt.geom.Ellipse2D object).
  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:

  1. Set stroke to the current graphics state of PsDocument.
  2. Stroke (outline) the ellipse:
 1//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddEllipse_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new multipaged PS Document with one page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Set paint for filling rectangle
10document.setPaint(Color.ORANGE);        
11//Fill the first ellipse
12document.fill(new Ellipse2D.Float(250, 100, 150, 100));
13
14//Set paint for stroking rectangle
15document.setPaint(Color.RED);
16//Set stroke
17document.setStroke(new BasicStroke(3));
18//Stroke (outline) the second ellipse
19document.draw(new Ellipse2D.Float(250, 300, 150, 100));
20
21//Close current page
22document.closePage();
23//Save the document
24document.save();

The result of running this code is appeared as

Add Ellipse

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.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.