Working with Clips in PS file| Python

Contents
[ Hide Show ]

Add Clip in PS Document

In a PS document, a clip is a boundary defined by a path that restricts the visibility of content within the current graphics state in PS viewers or editors. Any content extending beyond this boundary will be truncated.
In Python, clipping paths can be assigned in three ways:

Currently, the Aspose.Page for Python via .NET library supports the first and second methods of clipping. In the example below, we create a circular shape as a clipping path and use it to clip a blue-filled rectangle within the same graphics state.
In the example below we obtain a circle shape as a clipping path and cut off a blue-filled rectangle in the same graphics state.

In order to add a clip to the new PsDocument with Aspose.Page for Python via .NET library in this example we do the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create 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.
  5. Create a circle (aspose.pydrawing.GraphicsPath object) shape.
  6. Set a clip with this path.
  7. Set a paint to the current graphics state of PsDocument.
  8. Fill the rectangle path with the current paint.
  9. Exit from the current graphics state to the upper level one.
  10. Translate to the place of the filled rectangle.
  11. Stroke with a dashed line the bounds of the same rectangle above the filled one to show the bounds of the clipped filled rectangle.
  12. Close the page.
  13. Save the document.
 1# Create an output stream for PostScript document
 2with open(data_dir + "Clipping_outPS.ps", "wb") as out_ps_stream:
 3    # Create the save options with default values
 4    options = PsSaveOptions()
 5    
 6    # Create new 1-paged PS Document
 7    document = PsDocument(out_ps_stream, options, False)
 8    
 9    # Create a graphics path from the rectangle
10    rectange_path = aspose.pydrawing.drawing2d.GraphicsPath()
11    rectange_path.add_rectangle(aspose.pydrawing.RectangleF(0, 0, 300, 200))
12    
13    ##################################### Clipping by the shape //////////////////////////////////////////////////////////////////////
14    
15    # Save the graphics state in order to return back to this state after the transformation
16    document.write_graphics_save()
17    
18    # Displace the current graphics state on 100 points to the right and 100 points to the bottom.
19    document.translate(100, 100)
20    
21    # Create a graphics path from the circle
22    circle_path = aspose.pydrawing.drawing2d.GraphicsPath()
23    circle_path.add_ellipse(aspose.pydrawing.RectangleF(50, 0, 200, 200))
24    
25    # Add clipping by circle to the current graphics state
26    document.clip(circle_path)
27    
28    # Set the paint in the current graphics state
29    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
30    
31    # Fill the rectangle in the current graphics state (with clipping)
32    document.fill(rectange_path)
33    
34    # Restore the graphics state to the previus (upper) level
35    document.write_graphics_restore()
36    
37    # Displace the upper level graphics state on 100 points to the right and 100 points to the bottom.
38    document.translate(100, 100)
39    
40    pen = aspose.pydrawing.Pen(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
41    pen.width = float(2)
42    pen.dash_style = aspose.pydrawing.drawing2d.DashStyle.DASH
43    
44    document.set_stroke(pen)
45    
46    # Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
47    document.draw(rectange_path)
48    
49    ########################################################################################################################
50    
51    # Close the current page
52    document.close_page()
53    
54    # Save the document
55    document.save()

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

The result of running this code is

Clipping

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.