Working with Clips in PS file| Java
Add Clip in PS Document
A clip in a PS document is a path that bounds the content of the current graphics state that will be shown in the PS viewer or editor. The content that is left beyond the bounds will be cut off.
A clipping path in Java can be assigned in three ways:
- by any class implemented java.awt.Shape that can contain any closed shapes;
- by text outline;
- by 1 bpp (bits per pixel) 2-color image as a stencil mask;
At this moment Aspose.Page for Java library offers the first and the second ways of clipping. In the example below we obtain a circle shape as a clipping path and cut off a blue-filled rectangle in the same graphics state.
To add a clip to the new PsDocument with Aspose.Page for Java library in this example we 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 new graphics state.
- Create a circle (java.awt.geom.Ellipse2D object) shape.
- Set a clip with this path.
- Set a paint to the current graphics state of PsDocument.
- Fill the rectangle path with the current paint.
- Exit from the current graphics state to upper level one.
- Translate to the place of the filled rectangle.
- Stroke with a dashed line the bounds of the same rectangle above the filled one to show the bounds of the clipped filled rectangle.
- Close the page.
- Save the document.
1//Create an output stream for the PostScript document
2FileOutputStream outPsStream = new FileOutputStream(dataDir + "Clipping_outPS.ps");
3//Create save options with A4 size
4PsSaveOptions options = new PsSaveOptions();
5
6// Create a new PS Document with the page opened
7PsDocument document = new PsDocument(outPsStream, options, false);
8
9//Create a rectangle
10Shape rectangle = new Rectangle2D.Float(0, 0, 300, 200);
11
12//Set the paint in the upper level graphics state
13document.setPaint(Color.BLUE);
14
15//Save the graphics state to return back to this state after the transformation
16document.writeGraphicsSave();
17
18//Displace the current graphics state on 100 points to the right and 100 points to the bottom.
19document.translate(100, 100);
20
21//Create a circle shape
22Shape circle = new Ellipse2D.Float(50, 0, 200, 200);
23
24//Add clipping by the circle to the current graphics state
25document.clip(circle);
26
27//Fill the rectangle in the current graphics state (with clipping)
28document.fill(rectangle);
29
30//Restore the graphics state to the previus (upper) level
31document.writeGraphicsRestore();
32
33//Displace the upper-level graphics state on 100 points to the right and 100 points to the bottom.
34document.translate(100, 100);
35
36BasicStroke stroke = new BasicStroke(2,
37 BasicStroke.CAP_BUTT,
38 BasicStroke.JOIN_MITER,
39 10.0f, new float []{5.0f}, 0.0f);
40
41document.setStroke(stroke);
42
43//Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
44document.draw(rectangle);
45
46//Close the current page
47document.closePage();
48//Save the document
49document.save();
See working with clips in PS documents in .NET.
The result of running this code is appeared as
In the next example, we obtain a font used to clip a blue-filled rectangle with the text’s outline.
To add a clipping by text to the new PsDocument with Aspose.Page for Java library in this example we 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 new graphics state.
- Create a font.
- Set a clip with the text and the font.
- Set a paint to the current graphics state of PsDocument.
- Fill the rectangle path with the current paint.
- Exit from the current graphics state to upper level one.
- Translate to the place of the filled rectangle.
- Stroke with a dashed line the bounds of the same rectangle above the filled one to show the bounds of the clipped filled rectangle.
- Close the page.
- Save the document.
1//Create output stream for PostScript document
2FileOutputStream outPsStream = new FileOutputStream(dataDir + "Clipping_outPS.ps");
3//Create save options with A4 size
4PsSaveOptions options = new PsSaveOptions();
5
6// Create a new PS Document with the page opened
7PsDocument document = new PsDocument(outPsStream, options, false);
8
9//Create a rectangle
10Shape rectangle = new Rectangle2D.Float(0, 0, 300, 200);
11
12//Set paint in the upper-level graphics state
13document.setPaint(Color.BLUE);
14
15//Save graphics state to return back to this state after the transformation
16document.writeGraphicsSave();
17
18//Displace current graphics state on 100 points to the right and 100 points to the bottom.
19document.translate(100, 100);
20
21int fontSize = 120;
22Font font = new Font("Arial", Font.BOLD, fontSize);
23
24//Clip rectangle by text's outline
25document.clipText("ABC", font, 20, fontSize + 10);
26document.fill(rectangle);
27
28//Restore graphics state to the previous (upper) level
29document.writeGraphicsRestore();
30
31//Displace upper-level graphics state on 100 points to the right and 100 points to the bottom.
32document.translate(100, 100);
33
34BasicStroke stroke = new BasicStroke(2,
35 BasicStroke.CAP_BUTT,
36 BasicStroke.JOIN_MITER,
37 10.0f, new float []{5.0f}, 0.0f);
38
39document.setStroke(stroke);
40
41//Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
42document.draw(rectangle);
43
44//Close the current page
45document.closePage();
46//Save the document
47document.save();
See working with clips in PS documents in .NET.
The result of running this code appears as
You can download examples and data files from GitHub.