Working with Hatch Patterns in PostScript | .NET

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.

For painting by hatches, the .NET platform has a separate class, derived from System.Drawing.Brush, System.Drawing.HatchBrush. Its difference from System.Drawing.TextureBrush is that it has named predefined styles defining what image to use for tiling. .NET platform offers 53 hatch styles and all 52 styles can be used for filling or stroking (outlining) in PsDocument.

In order to paint graphics objects with a hatch pattern in Aspose.Page for .NET library it is enough simply to pass System.Drawing.HatchBrush to SetPaint() or one of the FillText() or FillAndStrokeText() methods that accept System.Drawing.Brush as a parameter.
In order to outline graphics objects with a hatch pattern in Aspose.Page for .NET library someone should create new System.Drawing.Pen with System.Drawing.HacthBrush and pass it to SetStroke() or one of the OutlineText() or FillAndStrokeText() methods that accept System.Drawing.Pen 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 .NET, 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:

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

If we need stroking (outlining) graphics objects with a hatch pattern instead of the last 4 points following will be:

     6. Create System.Drawing.Pen object with the hatch brush.
     7. Set this pen as current stroke in PsDocument.
     8. Outline the graphics path with current stroke or outline the text. If we use one of the methods for outlining the text that accepts System.Drawing.Pen as a parameter, previous point can be ignored.
     9. Close the page.
     10. Save the document.

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddHatchPattern_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6
 7    // Create new 1-paged PS Document
 8    PsDocument document = new PsDocument(outPsStream, options, false);
 9
10    int x0 = 20;
11    int y0 = 100;
12    int squareSide = 32;
13    int width = 500;
14    int sumX = 0;
15
16    //Create a graphics state
17    document.WriteGraphicsSave();
18
19    //Translate the graphics state to initial point
20    document.Translate(x0, y0);
21
22    //Create a rectangle path for every pattern square
23    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
24    path.AddRectangle(new System.Drawing.RectangleF(0, 0, squareSide, squareSide));
25
26    //Create a pen for outlining pattern square
27    Pen pen = new Pen(Color.Black, 2);
28
29    //For every hatch pattern style 
30    for (HatchStyle hatchStyle = 0; hatchStyle <= (HatchStyle)52; hatchStyle++)
31    {
32        //Set the paint with current hatch brush style
33        document.SetPaint(new HatchBrush(hatchStyle, Color.Black, Color.White));
34
35        //Calculate a displacement in order to don't go beyond the page bounds
36        int x = squareSide;
37        int y = 0;
38        if (sumX >= width)
39        {
40            x = -(sumX - squareSide);
41            y += squareSide;
42        }
43        //Translate current graphics state
44        document.Translate(x, y);
45        //Fill the pattern square
46        document.Fill(path);
47        //Set the stroke
48        document.SetStroke(pen);
49        //Draw the square outline
50        document.Draw(path);
51
52        //Calculate a distance from X0
53        if (sumX >= width)
54        {
55            sumX = squareSide;
56        }
57        else
58            sumX += x;
59    }
60
61    //Exit from current graphics state to upper level graphics state
62    document.WriteGraphicsRestore();
63
64    //Fill the text with the hatch pattern
65    HatchBrush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.Red, Color.Yellow);
66    System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold);
67    document.FillAndStrokeText("ABC", font, 200, 300, brush, pen);
68
69    //Outline the text with hatch pattern
70    brush = new HatchBrush(HatchStyle.Percent50, Color.Blue, Color.White);                
71    document.OutlineText("ABC", font, 200, 400, new Pen(brush, 5));
72
73
74    //Close current page
75    document.ClosePage();
76
77    //Save the document
78    document.Save();
79}

See working with a hatch pattern in PS document in Java and C++.


The result of running this code is appeared as

Add hatch pattern

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.