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:

  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 new graphics state and translate it to the necessary position.
  5. Create java.awt.image.BufferedImage from image file.
  6. Create java.awt.TexturePaint from the image.
  7. Set the necessary transformation in the texture brush.
  8. Set the texture paint as current paint in the current graphics state of PsDocument.
  9. Create a rectangle path.
  10. Fill the rectangle with the texture paint.
  11. Save current paint as a local variable for future use.
  12. Set the current paint with a red color.
  13. Set stroke with 2-point width java.awt.BasicStroke
  14. Outline the rectangle with a current stroke.
  15. Exit from the current graphics state to the upper-level graphics state.
  16. Create system font.
  17. Fill and stroke (outline) text. For filling the texture paint is used, and for stroking black pen is used.
  18. Outline the text in the other position with the texture paint and new java.awt.BasicStroke.
  19. Close the page.
  20. Save the document.
 1  // Paint rectangle and text and outline text with tiled image (texture pattern) in PS document.
 2
 3  String outputFileName = "AddTextureTilingPattern_outPS.ps";
 4
 5  //Create save options with A4 size
 6  PsSaveOptions options = new PsSaveOptions();
 7
 8  // Create new 1-paged PS Document
 9  PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11  document.writeGraphicsSave();
12  document.translate(200, 100);
13
14//Create a BufferedImage object from image file
15  BufferedImage image = ImageIO.read(new File(getDataDir() + "TestTexture.bmp"));
16  
17  //Create image area doubled in width
18  Rectangle2D.Float imageArea = new Rectangle2D.Float(0, 0, image.getWidth() * 2, image.getHeight());
19  //Create texture brush from the image
20  TexturePaint paint = new TexturePaint(image, imageArea);
21  
22  //Create rectangle
23  Rectangle2D.Float shape = new Rectangle2D.Float(0, 0, 200, 100);
24  
25  //Set this texture brush as current paint
26  document.setPaint(paint);
27  //Fill rectangle
28  document.fill(shape);
29  
30  
31  document.setPaint(Color.RED);
32  document.setStroke(new BasicStroke(2));
33  document.draw(shape);
34  
35  document.writeGraphicsRestore();
36  
37  //Fill the text with the texture pattern
38  Font font = new Font("Arial", Font.BOLD, 96);
39  document.fillAndStrokeText("ABC", font, 200, 300, paint, Color.BLACK, new BasicStroke(2));
40
41  //Outline the text with the texture pattern
42  document.outlineText("ABC", font, 200, 400, paint, new BasicStroke(5));
43
44  //Close current page
45  document.closePage();
46
47  //Save the document
48  document.save();

See working with textures in PS documents in .NET.


The result of running this code is appeared as

Add Texture Tiling Pattern

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.