Working with Clips in PostScript | .NET

Contents
[ Hide Show ]

Add Clip in PS Document

A clip in a PS document is a path that bounds the content of the current graphics state that will be shown in the PS viewer or editor. The content that is left beyond the bounds will be cut off.
A clipping path in .NET can be assigned in three ways:

At this moment Aspose.Page for .NET library offers only the first way of clipping. In the example below we obtain a circle System.Drawing.Drawing2D.GraphicsPath from a rectangle 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 .NET library in this example we do the following steps:

  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.
  5. Create a circle System.Drawing.Drawing2D.GraphicsPath from the rectangle.
  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 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
 2using (Stream outPsStream = new FileStream(dataDir + "Clipping_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with default values
 5    PsSaveOptions options = new PsSaveOptions();
 6
 7    // Create new 1-paged PS Document
 8    PsDocument document = new PsDocument(outPsStream, options, false);
 9
10    //Create a graphics path from the rectangle
11    GraphicsPath rectangePath = new GraphicsPath();
12    rectangePath.AddRectangle(new RectangleF(0, 0, 300, 200));
13
14    //Save the graphics state in order to return back to this state after transformation
15    document.WriteGraphicsSave();
16
17    //Displace current graphics state on 100 points to the right and 100 points to the bottom.
18    document.Translate(100, 100);
19
20    //Create a graphics path from the circle
21    GraphicsPath circlePath = new GraphicsPath();
22    circlePath.AddEllipse(new RectangleF(50, 0, 200, 200));
23
24    //Add a clipping by the circle to the current graphics state
25    document.Clip(circlePath);
26
27    //Set the paint in the current graphics state
28    document.SetPaint(new SolidBrush(Color.Blue));
29
30    //Fill the rectangle in the current graphics state (with the clipping)
31    document.Fill(rectangePath);
32
33    //Restore the graphics state to the previus (upper) level
34    document.WriteGraphicsRestore();
35
36    //Displace upper level graphics state on 100 points to the right and 100 points to the bottom.
37    document.Translate(100, 100);
38
39    Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
40    pen.DashStyle = DashStyle.Dash;
41
42    document.SetStroke(pen);
43
44    //Draw the rectangle in the current graphics state (has no clipping) above the clipped rectngle
45    document.Draw(rectangePath);
46
47    //Close current page
48    document.ClosePage();
49
50    //Save the document
51    document.Save();
52}

For Linux, MacOS and other non-Windows operation systems we offer to use our Aspose.Page.Drawing Nuget package. It uses Aspose.Drawing backend instead of System.Drawing system library. So import Aspose.Page.Drawing namespace instead of System.Drawing one. In the above code snippet Aspose.Page.Drawing.Rectangle will be used instead of System.Drawing.Rectangle, Aspose.Page.Drawing.Drawing2D.GraphicsPath will be used instead of System.Drawing.Drawing2D.GraphicsPath and so on. Our examples code on github contain all necessary substitutions.

See working with clips in PS documents in Java and C++.


The result of running this code is appeared as

Clipping

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.