Working with Hatch Patterns in PS file | Python

Add Hatch Pattern in PS Document

Hatch patterns, often comprising simple black-and-white images with various hatch designs, serve as texture tiling patterns. In Aspose.Page for Python via .NET, these patterns are represented by the aspose.pydrawing.drawing2d.HatchBrush class, derived from aspose.pydrawing.Brush. Unlike aspose.pydrawing.TextureBrush, HatchBrush offers named predefined styles, defining the image used for tiling. Aspose.Page for Python provides 53 hatch styles, all of which can be utilized for filling or stroking in PsDocument.

To paint graphics objects with a hatch pattern, create a HatchBrush with the desired hatch style and pass it into set_paint() or one of the fill_text() or fill_and_stroke_text() methods accepting aspose.pydrawing.Brush.

To outline graphics objects with a hatch pattern, set the hatch pattern as the current paint in PsDocument, create a new aspose.pydrawing.Pen, and pass it to set_stroke() or one of the outline_text() or fill_and_stroke_text() methods accepting aspose.pydrawing.Pen.

The example below demonstrates filling a shape with a hatch pattern, showcasing various hatch styles in Python, and filling and outlining text with a hatch pattern.

Here’s the algorithm for painting graphics objects with a hatch pattern in a new PS document:

  1. Create an output stream for the resulting PS file.
  2. Initiate PsSaveOptions.
  3. Create a PsDocument with the already created output stream and save options.
  4. Create the shape or a font in dependence on what object we are going to fill or outline.
  5. Create an object of aspose.pydrawing.drawing2d.HatchBrush with the desired style.
  6. Set the hatch paint as a current paint in the PsDocument
  7. Fill the shape with the current paint or fill the text. If we use one of the methods for filling the text that accepts aspose.pydrawing.Brush as a parameter, the previous step can be skipped.
  8. Close the page.
  9. Save the document.

If we need stroking (outlining) graphics objects with a hatch pattern instead of the last 4 steps will have the next look:

  1. Set the hatch paint as a current paint in PsDocument.
  2. Create aspose.pydrawing.Pen object.
  3. Set this stroke as current stroke in PsDocument.
  4. Outline the shape with the current paint and stroke or outline the text. If we use one of the methods for outlining the text that accepts aspose.pydrawing.Pen as a parameter, previous step can be ignored.
  5. Close the page.
  6. Save the document.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_hatches()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "AddHatchPattern_outPS.ps", "wb") as out_ps_stream:
 6    # Create the save options with A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    x0 = 20.
13    y0 = 100.
14    square_side = 32.
15    width = 500.
16    sum_x = 0.
17    
18    # Restore a graphics state
19    document.write_graphics_save()
20    
21    # Translate to initial point
22    document.translate(x0, y0)
23    
24    # Create a rectngle path for every pattern square
25    path = aspose.pydrawing.drawing2d.GraphicsPath()
26    path.add_rectangle(aspose.pydrawing.RectangleF(0, 0, square_side, square_side))
27    
28    # Create a pen for outlining the pattern square
29    pen = GraphicsFactory.create_pen_by_color_and_width(aspose.pydrawing.Color.black, 2)
30    
31    # For every hatch pattern style 
32    hatch_style = 0
33    while hatch_style <= 52:
34        # Set the paint with the current hatch brush style
35        document.set_paint(aspose.pydrawing.drawing2d.HatchBrush(aspose.pydrawing.drawing2d.HatchStyle(hatch_style),
36            aspose.pydrawing.Color.black, aspose.pydrawing.Color.white))
37        
38        # Calculate the displacement not to go beyond the page bounds
39        x = square_side
40        y = 0
41        if sum_x >= width:
42            x = -(sum_x - square_side)
43            y += square_side
44        # Translate the current graphics state
45        document.translate(x, y)
46        # Fill the pattern square
47        document.fill(path)
48        # Set the stroke
49        document.set_stroke(pen)
50        # Draw the square outline
51        document.draw(path)
52        
53        # Calculate the distance from X0
54        if sum_x >= width:
55            sum_x = square_side
56        else:
57            sum_x += x
58        hatch_style += 1
59    
60    # Restore the graphics state
61    document.write_graphics_restore()
62    
63    # Fill the text with the hatch pattern
64    brush = aspose.pydrawing.drawing2d.HatchBrush(aspose.pydrawing.drawing2d.HatchStyle.DIAGONAL_CROSS,
65    aspose.pydrawing.Color.red, aspose.pydrawing.Color.yellow)
66    font = ExternalFontCache.fetch_dr_font("Arial", 96, aspose.pydrawing.FontStyle.BOLD)
67    document.fill_and_stroke_text("ABC", font, 200, 300, brush, pen)
68    
69    # Outline text with the hatch pattern
70    brush = aspose.pydrawing.drawing2d.HatchBrush(aspose.pydrawing.drawing2d.HatchStyle.PERCENT50,
71    aspose.pydrawing.Color.blue, aspose.pydrawing.Color.white)
72    document.outline_text("ABC", font, 200, 400, GraphicsFactory.create_pen_by_brush_and_width(brush, 5))
73    
74    
75    # Close the current page
76    document.close_page()
77    
78    # Save the document
79    document.save()

See working with a hatch pattern in PS document in .NET, Java.

The result of running this code is

Add Hatch 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.