Working with Hatch Patterns in PS file | Java
Add Hatch Pattern in PS Document
Hatch pattern is a texture tiling pattern usually represented by small 2-colors (usually black&white) simple image. The main content of these small images is various hatches.
The Java platform doesn’t have a separate class for painting shapes and text with hatches. However, Aspose.Page for Java library offers com.aspose.eps.HatchPaintLibrary class that gives a possibilty to create java.awt.TexturePaint filled with hatches defined by one of 55 styles offered by com.aspose.eps.HatchStyle.
In order to paint graphics objects with a hatch pattern in Aspose.Page for Java library it is necessary to create hatch java.awt.TexturePaint with assigned hatch style, pass it into setPaint() or one of the fillText() or fillAndStrokeText() methods that accept java.awt.Paint as a parameter.
In order to outline graphics objects with a hatch pattern in Aspose.Page for Java library someone should set hatch pattern as current paint in PsDocument, create new java.awt.BasicStroke and pass it to setStroke() or one of the outlineText() or fillAndStrokeText() methods that accept java.awt.Stroke as a parameter.
In the example below we demonstrate, firstly, how to fill a shape with a hatch pattern, then all variety of hatch styles in Java, and, finally, how to fill and outline a text with a hatch pattern.
An algorithm for painting graphics objects with a hatch pattern in a new PS document includes the following steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions.
- Create PsDocument with the already created output stream and save options.
- Create the shape or font in dependence on what object we are going to fill or outline.
- Create with com.aspose.eps.HatchPaintLibrary an object of java.awt.TexturePaint with wishful style.
- Set the hatch paint as current paint in PsDocument
- Fill the shape with current paint or fill a text. If we use one of the methods for filling a text that accepts java.awt.Paint as a parameter, the previous point can be ignored.
- Close the page.
- Save the document.
If we need stroking (outlining) graphics objects with a hatch pattern instead of the last 4 points following will be:
Set the hatch paint as a current paint in PsDocument.
Create java.awt.BasicStroke object.
Set this stroke as current stroke in PsDocument.
Outline the shape with current paint and stroke or outline the text. If we use one of the methods for outlining the text that accepts java.awt.Stroke as a parameter, previous point can be ignored.
Close the page.
Save the document.
1 // Demonstrates all embedded hatch patterns that can be used to paint or outline shapes or text in PS document.
2
3 String outputFileName = "AddHatchPattern_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 int x0 = 20;
12 int y0 = 100;
13 int squareSide = 32;
14 int width = 500;
15 int sumX = 0;
16
17//Restore graphics state
18 document.writeGraphicsSave();
19
20 //Translate to initial point
21 document.translate(x0, y0);
22
23 //Create a square for every pattern
24 Rectangle2D.Float square = new Rectangle2D.Float(0, 0, squareSide, squareSide);
25
26 //Create pen for outlining pattern square
27 BasicStroke stroke = new BasicStroke(2);
28
29 HatchStyle [] hatchStyles = HatchStyle.values();
30
31 //For every hatch pattern style
32 for (int i = 0; i < hatchStyles.length; i++) {
33 //Create a hatch texture pattern by hatch style, foreground and background colors
34 TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(hatchStyles[i], Color.BLACK, Color.WHITE);
35 //Set paint with the current hatch pattern
36 document.setPaint(paint);
37
38 //Calculate a displacement in order to don't go beyond the page bounds
39 int x = squareSide;
40 int y = 0;
41 if (sumX >= width) {
42 x = -(sumX - squareSide);
43 y += squareSide;
44 }
45
46 //Translate current graphics state
47 document.translate(x, y);
48 //Fill pattern square
49 document.fill(square);
50
51 //Set current paint
52 document.setPaint(Color.BLACK);
53 //Set current stroke
54 document.setStroke(stroke);
55 //Draw square outline
56 document.draw(square);
57
58 //Calculate distance from X0
59 if (sumX >= width)
60 sumX = squareSide;
61 else
62 sumX += x;
63 }
64
65 //Restore graphics state
66 document.writeGraphicsRestore();
67
68 //Fill a text with the hatch pattern
69 TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.DiagonalCross, Color.RED, Color.YELLOW);
70 Font font = new Font("Arial", Font.BOLD, 96);
71 document.fillAndStrokeText("ABC", font, 200, 320, paint, Color.BLACK, stroke);
72
73 //Outline the text with the hatch pattern
74 paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.Percent70, Color.BLUE, Color.WHITE);
75 document.outlineText("ABC", font, 200, 420, paint, new BasicStroke(5));
76
77 //Close current page
78 document.closePage();
79
80 //Save the document
81 document.save();See working with a hatch pattern in PS document in .NET.
The result of running this code is appeared as

You can download examples and data files from GitHub.