Working with Textures in PostScript | .NET
Add Texture Tiling Pattern in PS Document
A texture tiling pattern is an image that is used for filling or drawing objects: shapes or text. If the size of the image is less than the size of the object it is repeated in X and Y directions for covering all necessary areas.
The process of repetition of the image inside graphics objects is called tiling. In order to set paint or stroke in PsDocument we must pass an object of System.Drawing.Brush class for a painting and an object of System.Drawing.Pen for stroking into respective methods.
Aspose.Page for .NET library processes all subclasses of System.Drawing.Brush that is offered by the .NET platform. These are System.Drawing.SolidBrush, System.Drawing.TextureBrush, System.Drawing.LinearGradientBrush, System.Drawing.PathGradientBrush and System.Drawing.HatchBrush. System.Drawing.Pen class cannot be extended because it is sealed, but it contains System.Drawing.Brush as a property and, thus, Aspose.Page for .NET library can also use a complete set of brushes also for drawing lines and outlining shapes and text.
In order to paint graphics objects with a textured pattern in Aspose.Page for .NET library it is enough simply to pass System.Drawing.TextureBrush to SetPaint() or one of the FillText() or FillAndStrokeText() methods which accepts System.Drawing.Brush as a parameter.
In order to outline graphics objects with a textured pattern in Aspose.Page for .NET library you should create new System.Drawing.Pen with System.Drawing.TextureBrush and pass it to SetStroke() or one of the OutlineText() or FillAndStrokeText() methods which accepts System.Drawing.Pen 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.
Description of steps of working with Texture Pattern and PsDocument in the example:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a new graphics state and translate it to the necessary position.
- Create System.Drawing.Bitmap from image file.
- Create System.Drawing.TextureBrush from the image.
- Set the necessary transformation in the texture brush.
- Set the texture brush as current paint in the current graphics state of PsDocument.
- Create a rectangle path.
- Fill the rectangle with the texture brush.
- Save current paint as a local variable for future use.
- Set the current stroke with a red pen.
- Outline the rectangle with a current pen.
- Exit from the current graphics state to the upper-level graphics state.
- Create system font.
- Fill and stroke (outline) text. For filling the texture brush is used, and for stroking black pen is used.
- Outline the text in the other position with the new System.Drawing.Pen created with texture brush as Brush.
- Close the page.
- Save the document.
1// Paint rectangle and text and outline text with tiled image (texture pattern) in PS document. 2 3string outputFileName = "AddTextureTilingPattern_outPS.ps"; 4 5//Create save options with A4 size 6PsSaveOptions options = new PsSaveOptions(); 7 8// Create new 1-paged PS Document 9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false); 10 11document.WriteGraphicsSave(); 12document.Translate(200, 100); 13 14//Create a Bitmap object from image file 15using (Bitmap image = new Bitmap(DataDir + "TestTexture.bmp")) 16{ 17 //Create texture brush from the image 18 TextureBrush brush = new TextureBrush(image, WrapMode.Tile); 19 20 //Add scaling in X direction to the mattern 21 Matrix transform = new Matrix(2, 0, 0, 1, 0, 0); 22 brush.Transform = transform; 23 24 //Set this texture brush as current paint 25 document.SetPaint(brush); 26} 27 28//Create rectangle path 29GraphicsPath path = new GraphicsPath(); 30path.AddRectangle(new RectangleF(0, 0, 200, 100)); 31 32//Fill rectangle 33document.Fill(path); 34 35//Get current paint 36Brush paint = document.GetPaint(); 37 38//Set red stroke 39document.SetStroke(new Pen(new SolidBrush(Color.Red), 2)); 40//Stroke the rectangle 41document.Draw(path); 42 43document.WriteGraphicsRestore(); 44 45//Fill text with texture pattern 46System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold); 47document.FillAndStrokeText("ABC", font, 200, 300, paint, new Pen(Color.Black, 2)); 48 49//Outline text with texture pattern 50document.OutlineText("ABC", font, 200, 400, new Pen(paint, 5)); 51 52//Close current page 53document.ClosePage(); 54 55//Save the document 56document.Save();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.RectangleF will be used instead of System.Drawing.RectangleF, Aspose.Page.Drawing.Drawing2D.GraphicsPath will be used instead of System.Drawing.Drawing2D.GraphicsPath and so on. Our code examples on GitHub contain all the necessary substitutions.
See working with textures in PS documents in Java.
The result of running this code is appeared as

You can download examples and data files from GitHub.