Working with Textures in PS file | Python

Add Texture Tiling Pattern in PS Document

A texture tiling pattern refers to an image utilized for filling or drawing objects such as shapes or text. When the image’s size is smaller than that of the object, it’s repeated in both the X and Y directions to cover the required areas adequately.

This repetition of the image within graphics objects is known as tiling. To set paint or stroke in a PsDocument it’s necessary to provide an object of the aspose.pydrawing.Brush class for painting and an object of the aspose.pydrawing.Pen class for stroking in their respective methods.

Aspose.Page for Python via .NET library processes all important classes implemented by aspose.pydrawing.Brush that is offered by the Python platform. These are aspose.pydrawing.Color, aspose.pydrawing.TextureBrush, aspose.pydrawing.drawing2d.LinearGradientBrush, and aspose.pydrawing.drawing2d.PathGradientBrush. A stroke color in Python is assigned separately from the stroke properties in aspose.pydrawing.Pen object with using again aspose.pydrawing.Brush. Thus, Aspose.Page for Python via .NET library can also use a complete set of paint’s implementations for drawing lines and outlining shapes and text.

In order to paint graphics objects with a textured pattern in Aspose.Page for Python via .NET library it is enough to simply pass aspose.pydrawing.TextureBrush into set_paint() or one of the fill_text() or fill_and_stroke_text() methods that accept aspose.pydrawing.Brush as a parameter.

In order to outline graphics objects with a textured pattern you should pass aspose.pydrawing.TextureBrush object also into set_paint() or one of the outline_text() or fill_and_stroke_text() methods that accept 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.

Here is a description of steps of working with Texture Pattern and PsDocument:

  1. Create an output stream for the resulting PS file.
  2. Initiate a PsSaveOptions object with the 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 aspose.pydrawing.Bitmap from an image file.
  6. Create aspose.pydrawing.TextureBrush from the image.
  7. Set the necessary transformation in the texture brush.
  8. Set the texture paint as a current paint in the current graphics state of the PsDocument.
  9. Create a rectangle path.
  10. Fill the rectangle with the texture paint.
  11. Save the current paint as a local variable for the future use.
  12. Set the current paint with a red color.
  13. Set a stroke with the 2-point width aspose.pydrawing.Pen
  14. Outline the rectangle with the 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 a new aspose.pydrawing.Pen.
  19. Close the page.
  20. Save the document.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_textures()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "AddTextureTilingPattern_outPS.ps", "wb") as out_ps_stream:
 6    # Create the save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    
13    document.write_graphics_save()
14    document.translate(200, 100)
15    
16    #Create a Bitmap object from an image file
17    with aspose.pydrawing.Bitmap(data_dir + "TestTexture.bmp") as image:
18        # Create a texture brush from the image
19        brush = aspose.pydrawing.TextureBrush(image, aspose.pydrawing.drawing2d.WrapMode.TILE)
20        
21        # Add scaling in the X direction to the mattern
22        transform = aspose.pydrawing.drawing2d.Matrix(float(2), float(0), float(0), float(1), float(0), float(0))
23        brush.transform = transform
24        
25        # Set this texture brush as a current paint
26        document.set_paint(brush)
27    
28    # Create a rectangle path
29    path = aspose.pydrawing.drawing2d.GraphicsPath()
30    path.add_rectangle(aspose.pydrawing.RectangleF(0, 0, 200, 100))
31    
32    # Fill the rectangle
33    document.fill(path)
34    
35    # Get the current paint
36    paint = document.get_paint()
37    
38    # Set a red stroke
39    pen1 = aspose.pydrawing.Pen(aspose.pydrawing.Color.red)
40    pen1.width = float(2)
41    document.set_stroke(pen1)
42
43    # Stroke the rectangle
44    document.draw(path)
45    
46    document.write_graphics_restore()
47    
48    # Fill the text with the texture pattern                
49    font = aspose.page.ExternalFontCache.create_font_by_family_name("Arial", 96, aspose.pydrawing.FontStyle.BOLD)
50    pen2 = aspose.pydrawing.Pen(aspose.pydrawing.Color.black)
51    pen2.width = float(2)
52    document.fill_and_stroke_text("ABC", font, 200, 300, paint, pen2)
53    
54    # Outline the text with the texture pattern
55    pen3 = aspose.pydrawing.Pen(aspose.pydrawing.Color.black)
56    pen3.brush = paint
57    pen3.width = float(5)
58    document.outline_text("ABC", font, 200, 400, pen3)
59    
60    # Close the current page
61    document.close_page()
62    
63    # Save the document
64    document.save()

See working with textures in PS documents in .NET, Java.

The result of running this code is

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.