Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/cab66a968f4ec25cdd2c11e01949b4b4 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/cab66a968f4ec25cdd2c11e01949b4b4 to your computer and use it in GitHub Desktop.
Add graphics elements in PostScript file with .NET

Work with Graphics Elements in PS Examples

These snippets demonstrate drawing and composing graphics in PostScript with Aspose.Page for .NET. Add shapes, text, images, gradients, patterns, apply clipping, transforms, and transparency to build rich PS artwork.

Key Use Cases

  • How to draw and fill shapes in a new PS file
  • How to add text to a new PS file
  • How to add an image to a new PS file
  • How to use gradient fills for painting and stroking shapes and text
  • How to use texture tiling pattern for painting and stroking shapes and text
  • How to use a hatch pattern for painting and stroking shapes and text
  • How to apply transforms to the graphics state
  • How to apply clipping paths to the graphics state
  • How to apply transparency to PS files

How to Run Examples

  1. Reference Aspose.Page for .NET: Aspose.Page on Windows; Aspose.Page.Drawing on non‑Windows.
  2. Copy a snippet into your project or run the related test.
  3. Apply a temporary license as described in the licensing guide.
  4. Build and run.

Restrictions

In evaluation mode, the output PS file is limited to 1Mb and contains a red evaluation warning. Apply a valid license to remove restrictions.

Related Documentation

Related Resources

Requirements

  • .NET 6.0+, .NET Core, or .NET Framework
  • Aspose.Page for .NET library
Aspose.Page for .NET – Add graphics elements in PS Examples
// Paint rectangle with diagonal gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-gradient/
string outputFileName = "DiagonalGradient_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create graphics path from the first rectangle
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
//Create linear gradient brush with rectangle as a bounds, start and end colors
LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(255, 255, 0, 0),
Color.FromArgb(255, 0, 0, 255), 0f);
//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
//Translation components are offsets of the rectangle
Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
//Rotate gradient, than scale and translate to get visible color transition in required rectangle
brushTransform.Rotate(-45);
float hypotenuse = (float)System.Math.Sqrt(200 * 200 + 100 * 100);
float ratio = hypotenuse / 200;
brushTransform.Scale(-ratio, 1);
brushTransform.Translate(100 / brushTransform.Elements[0], 0);
//Set transform
brush.Transform = brushTransform;
//Set paint
document.SetPaint(brush);
//Fill the rectangle
document.Fill(path);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Add ellipse to PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-shapes/
string outputFileName = "AddEllipse_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
//Create graphics path from the first ellipse
GraphicsPath path = new GraphicsPath();
path.AddEllipse(new RectangleF(250, 100, 150, 100));
//Set paint
document.SetPaint(new SolidBrush(Color.Orange));
//Fill the ellipse
document.Fill(path);
//Create graphics path from the second ellipse
path = new GraphicsPath();
path.AddEllipse(new RectangleF(250, 300, 150, 100));
//Set stroke
document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
//Stroke (outline) the ellipse
document.Draw(path);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Demonstrates all embedded hatch patterns that can be used to paint or outline shapes or text in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-textures/
string outputFileName = "AddHatchPattern_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
int x0 = 20;
int y0 = 100;
int squareSide = 32;
int width = 500;
int sumX = 0;
//Restore graphics state
document.WriteGraphicsSave();
//Translate to initial point
document.Translate(x0, y0);
//Create rectngle path for every pattern square
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0, 0, squareSide, squareSide));
//Create pen for outlining pattern square
Pen pen = new Pen(Color.Black, 2);
//For every hatch pattern style
for (HatchStyle hatchStyle = 0; hatchStyle <= (HatchStyle)52; hatchStyle++)
{
//Set paint with current hatch brush style
document.SetPaint(new HatchBrush(hatchStyle, Color.Black, Color.White));
//Calculate displacement in order to don't go beyond the page bounds
int x = squareSide;
int y = 0;
if (sumX >= width)
{
x = -(sumX - squareSide);
y += squareSide;
}
//Translate current graphics state
document.Translate(x, y);
//Fill pattern square
document.Fill(path);
//Set stroke
document.SetStroke(pen);
//Draw square outline
document.Draw(path);
//Calculate distance from X0
if (sumX >= width)
{
sumX = squareSide;
}
else
sumX += x;
}
//Restore graphics state
document.WriteGraphicsRestore();
//Fill text with hatch pattern
HatchBrush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.Red, Color.Yellow);
System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold);
document.FillAndStrokeText("ABC", font, 200, 300, brush, pen);
//Outline text with hatch pattern
brush = new HatchBrush(HatchStyle.Percent50, Color.Blue, Color.White);
document.OutlineText("ABC", font, 200, 400, new Pen(brush, 5));
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Paint rectangle and text and draw text with horizontal gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-gradient/
string outputFileName = "HorizontalGradient_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create graphics path from the first rectangle
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
//Create linear gradient brush with rectangle as a bounds, start and end colors
LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(150, 0, 0, 0),
Color.FromArgb(50, 40, 128, 70), 0f);
//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
//Translation components are offsets of the rectangle
Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
//Set transform
brush.Transform = brushTransform;
//Set paint
document.SetPaint(brush);
//Fill the rectangle
document.Fill(path);
//Fill text with gradient
System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold);
document.FillAndStrokeText("ABC", font, 200, 300, brush, new Pen(new SolidBrush(Color.Black), 2));
//Set current stroke
document.SetStroke(new Pen(brush, 5));
//Outline text with gradient
document.OutlineText("ABC", font, 200, 400);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Add image to PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-images/
string outputFileName = "AddImage_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
document.WriteGraphicsSave();
document.Translate(100, 100);
//Create a Bitmap object from image file
using (Bitmap image = new Bitmap(DataDir + "TestImage Format24bppRgb.jpg"))
{
//Create image transform
Matrix transform = new Matrix();
transform.Translate(35, 300);
transform.Scale(3, 3);
transform.Rotate(-45);
//Add image to document
document.DrawImage(image, transform, Color.Empty);
}
document.WriteGraphicsRestore();
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Paint a circle with 2-colors radial gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-gradient/
string outputFileName = "RadialGradient1_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 200;
//Create graphics path from the rectangle bounds
RectangleF bounds = new RectangleF(offsetX, offsetY, width, height);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(bounds);
//Create and fill color blend object
Color[] colors = { Color.White, Color.White, Color.Blue };
float[] positions = { 0.0f, 0.2f, 1.0f };
ColorBlend colorBlend = new ColorBlend();
colorBlend.Colors = colors;
colorBlend.Positions = positions;
GraphicsPath brushRect = new GraphicsPath();
brushRect.AddRectangle(new RectangleF(0, 0, width, height));
//Create path gradient brush with rectangle as a bounds
PathGradientBrush brush = new PathGradientBrush(brushRect);
//Set interpolation colors
brush.InterpolationColors = colorBlend;
//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
//Translation components are offsets of the rectangle
Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
//Set transform
brush.Transform = brushTransform;
//Set paint
document.SetPaint(brush);
//Fill the rectangle
document.Fill(path);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Paint a circle with 6-colors radial gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-gradient/
string outputFileName = "RadialGradient2_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 200;
//Create graphics path from the rectangle bounds
RectangleF bounds = new RectangleF(offsetX, offsetY, width, height);
GraphicsPath path = new GraphicsPath();
path.AddRectangle(bounds);
//Create and fill color blend object
Color[] colors = { Color.Green, Color.Blue, Color.Black, Color.Yellow, Color.Beige, Color.Red };
float[] positions = { 0.0f, 0.2f, 0.3f, 0.4f, 0.9f, 1.0f };
ColorBlend colorBlend = new ColorBlend();
colorBlend.Colors = colors;
colorBlend.Positions = positions;
GraphicsPath brushRect = new GraphicsPath();
brushRect.AddRectangle(new RectangleF(0, 0, width, height));
//Create path gradient brush with rectangle as a bounds
PathGradientBrush brush = new PathGradientBrush(brushRect);
//Set interpolation colors
brush.InterpolationColors = colorBlend;
//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
//Translation components are offsets of the rectangle
Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
//Set transform
brush.Transform = brushTransform;
//Set paint
document.SetPaint(brush);
//Fill the rectangle
document.Fill(path);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Add Rectangle to PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-shapes/
string outputFileName = "AddRectangle_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
//Create graphics path from the first rectangle
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(250, 100, 150, 100));
//Set paint
document.SetPaint(new SolidBrush(Color.Orange));
//Fill the rectangle
document.Fill(path);
//Create graphics path from the second rectangle
path = new GraphicsPath();
path.AddRectangle(new RectangleF(250, 300, 150, 100));
//Set stroke
document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
//Stroke (outline) the rectangle
document.Draw(path);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Demonstrates the ways of adding text using system and custom fonts to PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-text/
string outputFileName = "AddText_outPS.ps";
string FONTS_FOLDER = DataDir + @"../necessary_fonts/";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Set custom fonts folder. It will be added to system fonts folders for finding needed font.
options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
//A text to write to PS file
string str = "ABCDEFGHIJKLMNO";
int fontSize = 48;
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
////////////////////////////////////// Using sysem font (located in system fonts folders) for filling text //////////////////
System.Drawing.Font font = new System.Drawing.Font("Times New Roman", fontSize, FontStyle.Bold);
//Fill text with default or already defined color. In given case it is black.
document.FillText(str, font, 50, 100);
//Fill text with Blue color.
document.FillText(str, font, 50, 150, new SolidBrush(Color.Blue));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Using custom font (located in custom fonts folders) for filling text /////////////////
DrFont drFont = ExternalFontCache.FetchDrFont("Palatino Linotype", fontSize, FontStyle.Regular);
//Fill text with default or already defined color. In given case it is black.
document.FillText(str, drFont, 50, 200);
//Fill text with Blue color.
document.FillText(str, drFont, 50, 250, new SolidBrush(Color.Blue));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Using sysem font (located in system fonts folders) for outlining text ////////////////
//Outline text with default or already defined pen. In given case it is black colored 1-points width pen.
document.OutlineText(str, font, 50, 300);
//Outline text with blue-violet colored 2-points width pen.
document.OutlineText(str, font, 50, 350, new Pen(new SolidBrush(Color.BlueViolet), 2));
//Fill text with orange color and stroke with blue colored 2-points width pen.
document.FillAndStrokeText(str, font, 50, 400, new SolidBrush(Color.Yellow), /*new SolidBrush(Color.BlueViolet),*/ new Pen(new SolidBrush(Color.BlueViolet), 2));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Using custom font (located in custom fonts folders) for outlining text /////////////////
//Outline text with default or already defined pen. In given case it is black colored 1-points width pen.
document.OutlineText(str, drFont, 50, 450);
//Outline text with blue-violet colored 2-points width pen.
document.OutlineText(str, drFont, 50, 500, new Pen(new SolidBrush(Color.BlueViolet), 2));
//Fill text with orange color and stroke with blue colored 2-points width pen.
document.FillAndStrokeText(str, drFont, 50, 550, new SolidBrush(Color.Orange), new Pen(new SolidBrush(Color.Blue), 2));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Using custom font (located in custom fonts folders) ang glyphs widths for filling text ////////
drFont = ExternalFontCache.FetchDrFont("Palatino Linotype", fontSize, FontStyle.Regular);
//Glyphs widths
float[] widths = new float[] { 87, 87, 87, 87, 34, 87, 87 };
//Fill ASCII text using with assigning glyphs widths.
document.FillText("BAMBOOK", widths, drFont, 50, 600, new SolidBrush(Color.Blue));
///////////////////////////// Using custom font (located in custom fonts folders) ang glyphs widths for filling unicode text //
//Glyphs widths
widths = new float[] { 87, 34, 87, 87, 87, 87, 87 };
//Fill Unicode text using with assigning glyphs widths.
document.FillText("ЗООПАРК", widths, drFont, 50, 650, new SolidBrush(Color.Orange));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Paint rectangle and text and outline text with tiled image (texture pattern) in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-textures/
string outputFileName = "AddTextureTilingPattern_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
document.WriteGraphicsSave();
document.Translate(200, 100);
//Create a Bitmap object from image file
using (Bitmap image = new Bitmap(DataDir + "TestTexture.bmp"))
{
//Create texture brush from the image
TextureBrush brush = new TextureBrush(image, WrapMode.Tile);
//Add scaling in X direction to the mattern
Matrix transform = new Matrix(2, 0, 0, 1, 0, 0);
brush.Transform = transform;
//Set this texture brush as current paint
document.SetPaint(brush);
}
//Create rectangle path
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0, 0, 200, 100));
//Fill rectangle
document.Fill(path);
//Get current paint
Brush paint = document.GetPaint();
//Set red stroke
document.SetStroke(new Pen(new SolidBrush(Color.Red), 2));
//Stroke the rectangle
document.Draw(path);
document.WriteGraphicsRestore();
//Fill text with texture pattern
System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold);
document.FillAndStrokeText("ABC", font, 200, 300, paint, new Pen(Color.Black, 2));
//Outline text with texture pattern
document.OutlineText("ABC", font, 200, 400, new Pen(paint, 5));
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Demonstrates the ways of adding unicode text to PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-text/
string outputFileName = "AddTextUsingUnicodeString_outPS.ps";
string FONTS_FOLDER = DataDir + @"../necessary_fonts/";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Set custom fonts folder. It will be added to system fonts folders for finding needed font.
options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
//A text to write to PS file
string str = "試してみます。";
int fontSize = 48;
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
////////////////////////////////////// Using custom font (located in custom fonts folders) for filling text /////////////////
DrFont drFont = ExternalFontCache.FetchDrFont("Arial Unicode MS", fontSize, FontStyle.Regular);
//Fill text with default or already defined color. In given case it is black.
document.FillText(str, drFont, 50, 200);
//Fill text with Blue color.
document.FillText(str, drFont, 50, 250, new SolidBrush(Color.Blue));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Using custom font (located in custom fonts folders) for outlining text /////////////////
//Outline text with default or already defined pen. In given case it is black colored 1-points width pen.
document.OutlineText(str, drFont, 50, 450);
//Outline text with blue-violet colored 2-points width pen.
document.OutlineText(str, drFont, 50, 500, new Pen(new SolidBrush(Color.BlueViolet), 2));
//Fill text with orange color and stroke with blue colored 2-points width pen.
document.FillAndStrokeText(str, drFont, 50, 550, new SolidBrush(Color.Orange), new Pen(new SolidBrush(Color.Blue), 2));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Add transparent image to PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-transparency/
string outputFileName = "AddTransparentImage_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
//Set page's background color to see white image on it's own transparent background
options.BackgroundColor = Aspose.Page.Drawing.Color.FromArgb(211, 8, 48);
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
document.WriteGraphicsSave();
document.Translate(20, 100);
//Create bitmap from translucent image file
using (Bitmap image = new Bitmap(DataDir + "mask1.png"))
{
//Add this image to document as usual opaque RGB image
document.DrawImage(image, new Matrix(1, 0, 0, 1, 100, 0), Color.Empty);
}
//Again create bitmap from the same image file
using (Bitmap image = new Bitmap(DataDir + "mask1.png"))
{
//Add this image to document as transparent image image
document.DrawTransparentImage(image, new Matrix(1, 0, 0, 1, 350, 0), 255);
}
document.WriteGraphicsRestore();
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Paint rectangle with vertical gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-gradient/
string outputFileName = "VerticalGradient_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create graphics path from the first rectangle
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
//Create an array of interpolation colors
Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Orange, Color.DarkOliveGreen };
float[] positions = { 0.0f, 0.1873f, 0.492f, 0.734f, 1.0f };
ColorBlend colorBlend = new ColorBlend();
colorBlend.Colors = colors;
colorBlend.Positions = positions;
//Create linear gradient brush with rectangle as a bounds, start and end colors
LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.Beige, Color.DodgerBlue, 0f);
//Set interpolation colors
brush.InterpolationColors = colorBlend;
//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
//Translation components are offsets of the rectangle
Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
//Rotate transform to get colors change in vertical direction from up to down
brushTransform.Rotate(90);
//Set transform
brush.Transform = brushTransform;
//Set paint
document.SetPaint(brush);
//Fill the rectangle
document.Fill(path);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Demonstrates clipping by shape and clipping by text in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-clips/
string outputFileName = "ApplyClipByShape_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
//Create graphics path from the rectangle
GraphicsPath rectanglePath = new GraphicsPath();
rectanglePath.AddRectangle(new RectangleF(0, 0, 300, 200));
////////////////////////////////////// Clipping by shape //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Displace current graphics state on 100 points to the right and 100 points to the bottom.
document.Translate(100, 100);
//Create graphics path from the circle
GraphicsPath circlePath = new GraphicsPath();
circlePath.AddEllipse(new RectangleF(50, 0, 200, 200));
//Add clipping by circle to the current graphics state
document.Clip(circlePath);
//Set paint in the current graphics state
document.SetPaint(new SolidBrush(Color.Blue));
//Fill the rectangle in the current graphics state (with clipping)
document.Fill(rectanglePath);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//Displace upper level graphics state on 100 points to the right and 100 points to the bottom.
document.Translate(100, 100);
Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
pen.DashStyle = DashStyle.Dash;
document.SetStroke(pen);
//Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
document.Draw(rectanglePath);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Demonstrates clipping by text in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-clips/
string outputFileName = "ApplyClipByText_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
//Create graphics path from the rectangle
GraphicsPath rectanglePath = new GraphicsPath();
rectanglePath.AddRectangle(new RectangleF(0, 0, 300, 200));
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Displace current graphics state on 100 points to the right and 100 points to the bottom.
document.Translate(100, 100);
Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
pen.DashStyle = DashStyle.Dash;
int fontSize = 120;
System.Drawing.Font font = new System.Drawing.Font("Arial", fontSize, FontStyle.Bold);
//Clip rectangle by text's outline
document.ClipText("ABC", font, 20, fontSize + 10);
//Set paint in the current graphics state
document.SetPaint(new SolidBrush(Color.Blue));
document.Fill(rectanglePath);
document.WriteGraphicsRestore();
document.Translate(100, 100);
document.SetStroke(pen);
//Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
document.Draw(rectanglePath);
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Apply pseudo-transparency transparent image to PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-transparency/
string outputFileName = "ApplyPseudoTranparency_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
//Set page's background color to see white image on it's own transparent background
options.BackgroundColor = Aspose.Page.Drawing.Color.FromArgb(211, 8, 48);
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
float offsetX = 50;
float offsetY = 100;
float width = 200;
float height = 100;
///////////////////////////////// Create rectangle with opaque gradient fill /////////////////////////////////////////////////////////
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
LinearGradientBrush opaqueBrush = new LinearGradientBrush(new RectangleF(0, 0, 200, 100), Color.FromArgb(0, 0, 0),
Color.FromArgb(40, 128, 70), 0f);
Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
opaqueBrush.Transform = brushTransform;
GradientBrush gradientBrush = new GradientBrush(opaqueBrush);
gradientBrush.WrapMode = WrapMode.Clamp;
document.SetPaint(gradientBrush);
document.Fill(path);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
offsetX = 350;
///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
//Create graphics path from the first rectangle
path = new GraphicsPath();
path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
//Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
LinearGradientBrush translucentBrush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(150, 0, 0, 0),
Color.FromArgb(50, 40, 128, 70), 0f);
//Create a transform for brush.
brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
//Set transform
translucentBrush.Transform = brushTransform;
//Create GradientBrush object containing the linear gradient brush
gradientBrush = new GradientBrush(translucentBrush);
gradientBrush.WrapMode = WrapMode.Clamp;
//Set paint
document.SetPaint(gradientBrush);
//Fill the rectangle
document.Fill(path);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Close current page
document.ClosePage();
//Save the document
document.Save();
// Applying transformations to the graphics states in PS document.
// Learn more: https://docs.aspose.com/page/net/ps/working-with-transformations/
string outputFileName = "ApplyTransforms_outPS.ps";
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 1-paged PS Document
PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
document.Translate(100, 100);
//Create graphics path from the rectangle
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0, 0, 150, 100));
////////////////////////////////////// No transformations ///////////////////////////////////////////////////////////////
//Set paint in graphics state on upper level
document.SetPaint(new SolidBrush(Color.Orange));
//Fill the first rectangle that is on on upper level graphics state and that is without any transformations.
document.Fill(path);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Translation //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Displace current graphics state on 250 to the right. So we add translation component to the current transformation.
document.Translate(250, 0);
//Set paint in the current graphics state
document.SetPaint(new SolidBrush(Color.Blue));
//Fill the second rectangle in the current graphics state (has translation transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Displace on 200 to the bottom.
document.Translate(0, 200);
////////////////////////////////////// Scaling //////////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Scale current graphics state on 0.5 in X axis and on 0.75f in Y axis. So we add scale component to the current transformation.
document.Scale(0.5f, 0.75f);
//Set paint in the current graphics state
document.SetPaint(new SolidBrush(Color.Red));
//Fill the third rectangle in the current graphics state (has scale transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Displace upper level graphics state on 250 to the right.
document.Translate(250, 0);
////////////////////////////////////// Rotation //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Rotate current graphics state on 45 degrees around origin of current graphics state (350, 300). So we add rotation component to the current transformation.
document.Rotate(45);
//Set paint in the current graphics state
document.SetPaint(new SolidBrush(Color.Green));
//Fill the fourth rectangle in the current graphics state (has rotation transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Returns upper level graphics state back to the left and displace on 200 to the bottom.
document.Translate(-250, 200);
////////////////////////////////////// Shearing //////////////////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Shear current graphics state. So we add shear component to the current transformation.
document.Shear(0.1f, 0.2f);
//Set paint in the current graphics state
document.SetPaint(new SolidBrush(Color.Pink));
//Fill the fifth rectangle in the current graphics state (has shear transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Displace upper level graphics state on 250 to the right.
document.Translate(250, 0);
////////////////////////////////////// Complex transformation ////////////////////////////////////////////////////////
//Save graphics state in order to return back to this state after transformation
document.WriteGraphicsSave();
//Transform current graphics state with complex transformation. So we add translation, scale and rotation components to the current transformation.
document.Transform(new Matrix(1.2f, -0.965925f, 0.258819f, 1.5f, 0f, 50));
//Set paint in the current graphics state
document.SetPaint(new SolidBrush(Color.Aquamarine));
//Fill the sixth rectangle in the current graphics state (has complex transformation)
document.Fill(path);
//Restore graphics state to the previus (upper) level
document.WriteGraphicsRestore();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Returns upper level graphics state back to the left and displace on 200 to the bottom.
document.Translate(-250, 200);
////////////////////////////////////// Again no transformation ////////////////////////////////////////////////////////
// Demonstrates that current graphics state's color is orange that was set up at the beginning of the code.
//Fill the seventh rectangle in the current graphics state (has no transformation)
document.Fill(path);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Close current page
document.ClosePage();
//Save the document
document.Save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment