Working with vector shapes in PostScript (PS) documents in Java
Draw, fill, outline, and transform 2D vector shapes and graphics in PostScript (PS/EPS) documents using Aspose.Page for Java. Build rich vector graphics programmatically by rendering geometric primitives—including lines, rectangles, ellipses, arcs, polygons, and complex bezier curves—with full control over stroking, solid and gradient fills, clipping regions, and affine coordinate transformations.
Aspose.Page for Java leverages Java’s standard java.awt.Shape objects alongside its high-level PsDocument API to draw vector primitives directly into PostScript execution streams.
- To stroke the contour of a geometric shape using active paint and stroke settings use
PsDocument.draw(Shape) - To fill the interior of a shape with solid colors, textures, or gradient paints use
PsDocument.fill(Shape). - To build complex multi-segment vector paths using lines and Bézier curves use
java.awt.geom.Path2Dmethod. - To apply translation, scaling, rotation, and shearing matrix transformations to graphics use
PsDocument.transform()method. - To constrain rendering operations within an arbitrary closed vector boundary use
PsDocument.clip(Shape).
Drawing and filling basic primitives (rectangles and ellipses)
Render basic geometric primitives with custom outline colors and solid color fills.
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class DrawBasicShapesInPS {
public static void main(String[] args) throws Exception {
// Initialize output stream and PS document
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/basic_shapes.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Draw an outlined rectangle
document.setPaint(Color.BLUE);
Rectangle2D rectangle = new Rectangle2D.Float(100, 100, 200, 100);
document.draw(rectangle);
// Fill an ellipse with a solid color
document.setPaint(Color.RED);
Ellipse2D ellipse = new Ellipse2D.Float(100, 250, 200, 100);
document.fill(ellipse);
// Close page and save document
document.closePage();
document.save();
outStream.close();
System.out.println("Basic shapes drawn and saved to PostScript document.");
}
}Constructing complex vector paths and curves
Build multi-segment paths, stars, or custom polygons using Java’s Path2D.Float and render them in PostScript.
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Path2D;
import java.io.FileOutputStream;
public class DrawComplexPathsInPS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/complex_path.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Create a star-shaped path using Path2D
Path2D path = new Path2D.Float();
path.moveTo(150, 100);
path.lineTo(180, 180);
path.lineTo(260, 180);
path.lineTo(195, 225);
path.lineTo(220, 300);
path.lineTo(150, 250);
path.lineTo(80, 300);
path.lineTo(105, 225);
path.lineTo(40, 180);
path.lineTo(120, 180);
path.closePath();
// Configure stroke thickness and fill color
document.setStroke(new BasicStroke(3.0f));
document.setPaint(Color.ORANGE);
document.fill(path);
document.setPaint(Color.BLACK);
document.draw(path);
document.closePage();
document.save();
outStream.close();
System.out.println("Complex vector path rendered successfully.");
}
}Applying transformations and graphic states
Rotate, scale, and translate vector shapes using affine transformations within PostScript graphics states (writeGraphicsSave/writeGraphicsRestore).
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class TransformedShapesInPS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/transformed_shapes.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Save graphics state
document.writeGraphicsSave();
// Apply rotation (45 degrees) and translation
AffineTransform transform = new AffineTransform();
transform.translate(200, 200);
transform.rotate(Math.toRadians(45));
document.transform(transform);
// Draw square at transformed position
document.setPaint(Color.MAGENTA);
document.fill(new Rectangle2D.Float(-50, -50, 100, 100));
// Restore graphics state
document.writeGraphicsRestore();
document.closePage();
document.save();
outStream.close();
System.out.println("Transformed shapes successfully saved to PS file.");
}
}Installation and Setup
Add Aspose.Page for Java to your Maven:
Package Manager Console Command
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-page</artifactId>
<version>Latest</version>
</dependency>
FAQ
1. Can I use native Java AWT Shape instances directly with PsDocument?
Yes. Aspose.Page for Java natively accepts any standard java.awt.Shape implementation—such as Rectangle2D, Ellipse2D, Line2D, RoundRectangle2D, or Path2D—directly in its draw() and fill() calls.
2. How do I apply stroke styles like dashed or dotted lines to shapes?
Configure a custom stroke via document.setStroke(new BasicStroke(width, cap, join, miterlimit, dashArray, dashPhase)) before calling document.draw().
3. Does Aspose.Page support linear and radial gradient fills for PostScript shapes?
Yes. You can pass instances of gradient paint implementations (like java.awt.LinearGradientPaint or java.awt.RadialGradientPaint) to document.setPaint() to render gradient fills on vector shapes.