Working with Textures in PS file | Java
Add Texture Tiling Pattern in PS Document
A texture tiling pattern is an image that is used for filling or drawing objects: shapes or text. If the size of the image is less than the size of the object it is repeated in X and Y directions for covering all necessary areas.
The process of repetition of the image inside graphics objects is called tiling. In order to set paint or stroke in PsDocument we must pass an object of java.awt.Paint class for a painting and an object of java.awt.Stroke for stroking into respective methods.
Aspose.Page for Java library processes all important classes implemented java.awt.Paint that is offered by the Java platform. These are java.awt.Color, java.awt.TexturePaint, java.awt.LinearGradientPaint and java.awt.RadialGradientPaint. Stroke color in Java is assigned separately from stroke properties in java.awt.Stroke object with using again java.awt.Paint. Thus, Aspose.Page for Java library can also use a complete set of paint’s implementations also for drawing lines and outlining shapes and text.
In order to paint graphics objects with a textured pattern in Aspose.Page for Java library it is enough simply to pass java.awt.TexturePaint into setPaint() or one of the fillText() or fillAndStrokeText() methods which accepts java.awt.Paint as a parameter.
In order to outline graphics objects with a textured pattern in Aspose.Page for Java library you should pass java.awt.TexturePaint object also into setPaint() or one of the outlineText() or fillAndStrokeText() methods which accepts stroke paint as a parameter.
In the example below we demonstrate how to fill a shape and a text and outline a text with a texture tiling pattern.
Description of steps of working with Texture Pattern and PsDocument in the example:
- 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 and translate it to the necessary position.
- Create java.awt.image.BufferedImage from image file.
- Create java.awt.TexturePaint from the image.
- Set the necessary transformation in the texture brush.
- Set the texture paint as current paint in the current graphics state of PsDocument.
- Create a rectangle path.
- Fill the rectangle with the texture paint.
- Save current paint as a local variable for future use.
- Set the current paint with a red color.
- Set stroke with 2-point width java.awt.BasicStroke
- Outline the rectangle with a current stroke.
- Exit from the current graphics state to the upper-level graphics state.
- Create system font.
- Fill and stroke (outline) text. For filling the texture paint is used, and for stroking black pen is used.
- Outline the text in the other position with the texture paint and new java.awt.BasicStroke.
- Close the page.
- Save the document.
1//Create an output stream for PostScript document
2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddTextureTilingPattern_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
9document.writeGraphicsSave();
10document.translate(200, 100);
11
12//Create a BufferedImage object from image file
13BufferedImage image = ImageIO.read(new File(dataDir + "TestTexture.bmp"));
14
15//Create image area doubled in width
16Rectangle2D.Float imageArea = new Rectangle2D.Float(0, 0, image.getWidth() * 2, image.getHeight());
17//Create texture brush from the image
18TexturePaint paint = new TexturePaint(image, imageArea);
19
20//Create rectangle
21Rectangle2D.Float shape = new Rectangle2D.Float(0, 0, 200, 100);
22
23//Set this texture brush as current paint
24document.setPaint(paint);
25//Fill rectangle
26document.fill(shape);
27
28
29document.setPaint(Color.RED);
30document.setStroke(new BasicStroke(2));
31document.draw(shape);
32
33document.writeGraphicsRestore();
34
35//Fill the text with the texture pattern
36Font font = new Font("Arial", Font.BOLD, 96);
37document.fillAndStrokeText("ABC", font, 200, 300, paint, Color.BLACK, new BasicStroke(2));
38
39//Outline the text with the texture pattern
40document.outlineText("ABC", font, 200, 400, paint, new BasicStroke(5));
41
42//Close current page
43document.closePage();
44//Save the document
45document.save();
See working with textures in PS documents in .NET.
The result of running this code is appeared as
You can download examples and data files from GitHub.