Working with clips and clipping paths in PostScript (PS) documents in Java
Define, apply, and manage vector clipping paths in PostScript (PS/EPS) documents using Aspose.Page for Java. Restrict drawing, text rendering, and image display operations within arbitrary closed geometric boundaries. Manage local and global clip regions efficiently using PostScript graphics state stacks (writeGraphicsSave/writeGraphicsRestore) to create complex masked visual effects.
Aspose.Page for Java leverages Java’s java.awt.Shape primitives to modify the current clipping path within the PsDocument rendering state.
- To apply clips that intersect the current clipping path with the provided geometric shape boundary use
PsDocument.writeGraphicsSave()method. - To push the current graphics state—including active clip boundaries—onto the state stack
PsDocument.writeGraphicsSave()method. - To restore graphic state that pops the top graphics state, resetting the clipping area back to its previous state use
PsDocument.writeGraphicsRestore()method. - To clip shapes assepting any standard or complex closed vector path as a clipping region use
Rectangle2D,Ellipse2D,Path2D.
Applying a simple shape clip (rectangle or ellipse)
Restrict text or vector drawing operations to stay strictly inside a basic geometric shape.
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class BasicClippingPS {
public static void main(String[] args) throws Exception {
// Initialize output stream and PS document
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/basic_clipping.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Define a clipping shape (Circle boundary)
Ellipse2D clipShape = new Ellipse2D.Float(100, 100, 200, 200);
// Save state before clipping
document.writeGraphicsSave();
// Apply clip shape
document.clip(clipShape);
// Draw a large rectangle that extends past the clip boundary
document.setPaint(Color.BLUE);
document.fill(new Rectangle2D.Float(50, 50, 300, 300)); // Only the circle area gets rendered
// Restore state to release clip
document.writeGraphicsRestore();
document.closePage();
document.save();
outStream.close();
System.out.println("Basic clip applied successfully to PostScript document.");
}
}Managing nested clip regions with graphics states
Isolate local clipping boundaries and prevent clip persistence across page elements by wrapping clip operations inside writeGraphicsSave() and writeGraphicsRestore() blocks.
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class NestedClippingPS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/nested_clipping.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Outer graphic state
document.writeGraphicsSave();
// Intersect First Clip (Outer Box)
document.clip(new Rectangle2D.Float(100, 100, 200, 200));
// Inner graphic state
document.writeGraphicsSave();
// Intersect Second Clip (Inner Box - creates combined intersection)
document.clip(new Rectangle2D.Float(150, 150, 200, 200));
// Fill combined intersection region
document.setPaint(Color.RED);
document.fill(new Rectangle2D.Float(0, 0, 500, 500));
// Restore back to single outer box clip
document.writeGraphicsRestore();
// Restore back to unclipped original page state
document.writeGraphicsRestore();
document.closePage();
document.save();
outStream.close();
System.out.println("Nested clip states processed successfully.");
}
}Clipping complex text and vector paths
Use custom shapes constructed with Path2D as dynamic masks for patterned or multi-colored backgrounds.
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class ComplexPathClippingPS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/complex_clip.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Construct a custom triangle path for clipping
Path2D triangleClip = new Path2D.Float();
triangleClip.moveTo(250, 100);
triangleClip.lineTo(400, 350);
triangleClip.lineTo(100, 350);
triangleClip.closePath();
document.writeGraphicsSave();
// Apply custom path clip
document.clip(triangleClip);
// Render overlapping colored stripes inside the triangle boundary
Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };
for (int i = 0; i < colors.length; i++) {
document.setPaint(colors[i]);
document.fill(new Rectangle2D.Float(100, 100 + (i * 60), 300, 50));
}
document.writeGraphicsRestore();
document.closePage();
document.save();
outStream.close();
System.out.println("Complex vector path clip applied successfully.");
}
}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. How does PsDocument.clip(Shape) alter the current clip region?
Calling clip(Shape) intersects the specified shape with the existing clip path in the PostScript execution context. It does not replace the clip path entirely unless the state is restored first.
2. How do I remove or reset a clipping path in PostScript?
Because PostScript accumulates clips via intersection, you cannot directly ‘remove’ a clip path. Instead, save the state with document.writeGraphicsSave() before applying the clip, and restore it with document.writeGraphicsRestore() when finished.
3. Can I use arbitrary shapes like text outlines or curved paths for clipping?
Yes. Any Java java.awt.Shape implementation—including Path2D, curved Bézier shapes, and converted text vector outlines—can be passed directly to document.clip().