Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

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

Working with Graphics Elements in PS Examples

These code snippets show how to add and manipulate graphics elements in PostScript (PS) documents using Aspose.Page for Java. The library enables drawing shapes, images, gradients, and text programmatically in PS files.

Key Use Cases

  • How to draw and fill shapes to new PS file
  • How to add text to new PS file
  • How to add image to 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 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 PS files

How to Run Examples

  1. Reference Aspose.Page for Java in your project.
  2. Copy the required code snippet into your project.
  3. Download a temporary license and set it up as described here or use a paid license.
  4. Run the example.

Restrictions

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

Related Documentation

Related Resources

Requirements

  • Java 8 or higher
  • Aspose.Page for Java library
Aspose.Page for Java – Add graphics elements in PS Examples
// Paint rectangle with diagonal gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/java/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(getOutputDir() + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create graphics path from the first rectangle
java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
//Create the gradient transform. Scale components in the transform must be equal to width and heigh of the rectangle.
//Translation components are offsets of the rectangle.
AffineTransform transform = new AffineTransform(200, 0, 0, 100, 200, 100);
//Rotate gradient, than scale and translate to get visible color transition in required rectangle
transform.rotate(-45 * (Math.PI / 180));
float hypotenuse = (float) Math.sqrt(200 * 200 + 100 * 100);
float ratio = hypotenuse / 200;
transform.scale(-ratio, 1);
transform.translate(100 / transform.getScaleX(), 0);
//Create diagonal linear gradient paint.
LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
new float [] {0, 1}, new Color [] {Color.RED, Color.BLUE}, MultipleGradientPaint.CycleMethod.NO_CYCLE,
MultipleGradientPaint.ColorSpaceType.SRGB, transform);
//Set paint
document.setPaint(paint);
//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/java/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(getOutputDir() + outputFileName, options, false);
//Create graphics path from the first ellipse
GeneralPath path = new GeneralPath();
path.append(new java.awt.geom.Ellipse2D.Float(250, 100, 150, 100), false);
//Set paint
document.setPaint(Color.ORANGE);
//Fill the ellipse
document.fill(path);
//Create graphics path from the second ellipse
path = new GeneralPath();
path.append(new java.awt.geom.Ellipse2D.Float(250, 300, 150, 100), false);
//Set stroke
document.setStroke(new java.awt.BasicStroke(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/java/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(getOutputDir() + 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 a square for every pattern
Rectangle2D.Float square = new Rectangle2D.Float(0, 0, squareSide, squareSide);
//Create pen for outlining pattern square
BasicStroke stroke = new BasicStroke(2);
HatchStyle [] hatchStyles = HatchStyle.values();
//For every hatch pattern style
for (int i = 0; i < hatchStyles.length; i++) {
//Create a hatch texture pattern by hatch style, foreground and background colors
TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(hatchStyles[i], Color.BLACK, Color.WHITE);
//Set paint with the current hatch pattern
document.setPaint(paint);
//Calculate a 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(square);
//Set current paint
document.setPaint(Color.BLACK);
//Set current stroke
document.setStroke(stroke);
//Draw square outline
document.draw(square);
//Calculate distance from X0
if (sumX >= width)
sumX = squareSide;
else
sumX += x;
}
//Restore graphics state
document.writeGraphicsRestore();
//Fill a text with the hatch pattern
TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.DiagonalCross, Color.RED, Color.YELLOW);
Font font = new Font("Arial", Font.BOLD, 96);
document.fillAndStrokeText("ABC", font, 200, 320, paint, Color.BLACK, stroke);
//Outline the text with the hatch pattern
paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.Percent70, Color.BLUE, Color.WHITE);
document.outlineText("ABC", font, 200, 420, paint, new BasicStroke(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/java/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(getOutputDir() + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create graphics path from the first rectangle
java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
//Create linear gradient brush with rectangle as a bounds, start and end colors and transformation matrix
LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
new float [] {0, 1}, new Color [] {new Color(0, 0, 0, 150), new Color(40, 128, 70, 50)},
MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
new AffineTransform(width, 0, 0, height, offsetX, offsetY));
//Set paint
document.setPaint(paint);
//Fill the rectangle
document.fill(path);
//Fill text with gradient
java.awt.Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 96);
document.fillAndStrokeText("ABC", font, 200, 300, paint, Color.BLACK, new java.awt.BasicStroke(2));
//Set current stroke
document.setStroke(new java.awt.BasicStroke(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/java/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(getOutputDir() + outputFileName, options, false);
document.writeGraphicsSave();
document.translate(100, 100);
//Create a BufferedImage object from image file
try {
BufferedImage image = ImageIO.read(new java.io.File(getDataDir() + "TestImage Format24bppRgb.jpg"));
//Create image transform
AffineTransform transform = new AffineTransform();
transform.translate(35, 300);
transform.scale(3, 3);
transform.rotate(Math.toRadians(-45));
//Add image to document
document.drawImage(image, transform, Color.WHITE);
} catch (IOException ex) {
}
document.writeGraphicsRestore();
//Close current page
document.closePage();
//Save the document
document.save();
// Paint rectangle with radial gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/java/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(getOutputDir() + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create a circle
Ellipse2D.Float circle = new Ellipse2D.Float(offsetX, offsetY, width, height);
//Create arrays of colors and fractions for the gradient.
Color[] colors = { Color.WHITE, Color.WHITE, Color.BLUE };
float[] fractions = { 0.0f, 0.2f, 1.0f };
//Create horizontal linear gradient paint. Scale components in the transform must be equal to width and heigh of the rectangle.
//Translation components are offsets of the rectangle.
AffineTransform transform = new AffineTransform(width, 0, 0, height, offsetX, offsetY);
//Create radial linear gradient paint.
RadialGradientPaint paint = new RadialGradientPaint(new Point2D.Float(64, 64), 68, new Point2D.Float(24, 24),
fractions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
transform);
//Set paint
document.setPaint(paint);
//Fill the rectangle
document.fill(circle);
//Close current page
document.closePage();
//Save the document
document.save();
// Paint rectangle with radial gradient fill in PS document.
// Learn more: https://docs.aspose.com/page/java/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(getOutputDir() + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create a rectangle
Rectangle2D.Float rectangle = new Rectangle2D.Float(offsetX, offsetY, width, height);
//Create arrays of colors and fractions for the gradient.
Color[] colors = { Color.GREEN, Color.BLUE, Color.BLACK, Color.YELLOW, new Color(245, 245, 220), Color.RED };
float[] fractions = { 0.0f, 0.2f, 0.3f, 0.4f, 0.9f, 1.0f };
//Create horizontal linear gradient paint. Scale components in the transform must be equal to width and heigh of the rectangle.
//Translation components are offsets of the rectangle.
AffineTransform transform = new AffineTransform(width, 0, 0, height, offsetX, offsetY);
//Create radial gradient paint.
RadialGradientPaint paint = new RadialGradientPaint(new Point2D.Float(300, 200), 100, new Point2D.Float(300, 200),
fractions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
transform);
//Set paint
document.setPaint(paint);
//Fill the rectangle
document.fill(rectangle);
//Close current page
document.closePage();
//Save the document
document.save();
// Add Rectangle to PS document.
// Learn more: https://docs.aspose.com/page/java/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(getOutputDir() + outputFileName, options, false);
//Create graphics path from the first rectangle
GeneralPath path = new GeneralPath();
path.append(new Rectangle2D.Float(250, 100, 150, 100), false);
//Set paint
document.setPaint(Color.ORANGE);
//Fill the rectangle
document.fill(path);
//Create graphics path from the second rectangle
path = new GeneralPath();
path.append(new Rectangle2D.Float(250, 300, 150, 100), false);
//Set stroke
document.setStroke(new java.awt.BasicStroke(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/java/ps/working-with-text/
String outputFileName = "AddText_outPS.ps";
String FONTS_FOLDER = getDataDir() + "../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.setAdditionalFontsFolders(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(getOutputDir() + outputFileName, options, false);
////////////////////////////////////// Using sysem font (located in system fonts folders) for filling text //////////////////
Font font = new Font("Times New Roman", Font.BOLD, fontSize);
//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, Color.BLUE);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Using custom font (located in custom fonts folders) for filling text /////////////////
DrFont drFont = ExternalFontCache.fetchDrFont("Palatino Linotype", fontSize, Font.PLAIN);
//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, 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 Color(138, 43, 226), new java.awt.BasicStroke(2));
//Fill text with orange color and stroke with blue colored 2-points width pen.
document.fillAndStrokeText(str, font, 50, 400, Color.YELLOW, new Color(138, 43, 226), new java.awt.BasicStroke(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 Color(138, 43, 226), new java.awt.BasicStroke(2));
//Fill text with orange color and stroke with blue colored 2-points width pen.
document.fillAndStrokeText(str, drFont, 50, 550, Color.ORANGE, Color.BLUE, new java.awt.BasicStroke(2));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Using custom font (located in custom fonts folders) ang glyphs widths for filling text ////////
drFont = ExternalFontCache.fetchDrFont("Palatino Linotype", fontSize, Font.PLAIN);
//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, 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, 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/java/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(getOutputDir() + outputFileName, options, false);
document.writeGraphicsSave();
document.translate(200, 100);
//Create a BufferedImage object from image file
BufferedImage image = ImageIO.read(new File(getDataDir() + "TestTexture.bmp"));
//Create image area doubled in width
Rectangle2D.Float imageArea = new Rectangle2D.Float(0, 0, image.getWidth() * 2, image.getHeight());
//Create texture brush from the image
TexturePaint paint = new TexturePaint(image, imageArea);
//Create rectangle
Rectangle2D.Float shape = new Rectangle2D.Float(0, 0, 200, 100);
//Set this texture brush as current paint
document.setPaint(paint);
//Fill rectangle
document.fill(shape);
document.setPaint(Color.RED);
document.setStroke(new BasicStroke(2));
document.draw(shape);
document.writeGraphicsRestore();
//Fill the text with the texture pattern
Font font = new Font("Arial", Font.BOLD, 96);
document.fillAndStrokeText("ABC", font, 200, 300, paint, Color.BLACK, new BasicStroke(2));
//Outline the text with the texture pattern
document.outlineText("ABC", font, 200, 400, paint, new BasicStroke(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/java/ps/working-with-text/
String outputFileName = "AddTextUsingUnicodeString_outPS.ps";
String FONTS_FOLDER = getDataDir() + "../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.setAdditionalFontsFolders(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(getOutputDir() + outputFileName, options, false);
////////////////////////////////////// Using custom font (located in custom fonts folders) for filling text /////////////////
DrFont drFont = ExternalFontCache.fetchDrFont("Arial Unicode MS", fontSize, Font.PLAIN);
//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, 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 Color(138, 43, 226), new java.awt.BasicStroke(2));
//Fill text with orange color and stroke with blue colored 2-points width pen.
document.fillAndStrokeText(str, drFont, 50, 550, Color.ORANGE, Color.BLUE, new java.awt.BasicStroke(2));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Close current page
document.closePage();
//Save the document
document.save();
// Add transparent image to PS document.
// Learn more: https://docs.aspose.com/page/java/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.setBackgroundColor(new Color(211, 8, 48));
// Create new 1-paged PS Document
PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
//document.setPaint(new Color(211, 8, 48));
//document.fill(new Rectangle2D.Float(0, 0, (int) options.getPageSize().getWidth(), 300));
document.writeGraphicsSave();
document.translate(20, 100);
//Create an image from translucent image file
BufferedImage image = ImageIO.read(new File(getDataDir() + "mask1.png"));
//Add this image to document as usual opaque RGB image
document.drawImage(image, new AffineTransform(1, 0, 0, 1, 100, 0), null);
//Add this image to document as transparent image
document.drawTransparentImage(image, new AffineTransform(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/java/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(getOutputDir() + outputFileName, options, false);
float offsetX = 200;
float offsetY = 100;
float width = 200;
float height = 100;
//Create graphics path from the first rectangle
java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
//Create an array of interpolation colors
Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, new Color(107, 142, 35) }; // DarkOliveGreen
float[] positions = { 0.0f, 0.1873f, 0.492f, 0.734f, 1.0f };
//Create the gradient transform. Scale components in the transform must be equal to width and heigh of the rectangle.
//Translation components are offsets of the rectangle.
AffineTransform transform = new AffineTransform(width, 0, 0, height, offsetX, offsetY);
//Rotate the gradient on 90 degrees around an origin
transform.rotate(Math.toRadians(90));
//Create vertical linear gradient paint.
LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
positions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
transform);
//Set paint
document.setPaint(paint);
//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/java/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(getOutputDir() + outputFileName, options, false);
//Create graphics path from the rectangle
GeneralPath rectanglePath = new GeneralPath();
rectanglePath.append(new Rectangle2D.Float(0, 0, 300, 200), false);
////////////////////////////////////// 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
GeneralPath circlePath = new GeneralPath();
circlePath.append(new Ellipse2D.Float(50, 0, 200, 200), false);
//Add clipping by circle to the current graphics state
document.clip(circlePath);
//Set paint in the current graphics state
document.setPaint(Color.BLUE);
//Fill the rectangle in the current graphics state (with clipping)
document.fill(rectanglePath);
//Restore graphics state to the previous (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);
//Create dashed stroke similar to Pen with DashStyle.Dash
float[] dash = new float[] { 5.0f };
BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
document.setStroke(stroke);
//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/java/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(getOutputDir() + outputFileName, options, false);
//Create graphics path from the rectangle
GeneralPath rectanglePath = new GeneralPath();
rectanglePath.append(new Rectangle2D.Float(0, 0, 300, 200), false);
//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);
float[] dash = new float[] { 5.0f };
BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
int fontSize = 120;
Font font = new Font("Arial", Font.BOLD, fontSize);
//Clip rectangle by text's outline
document.clipText("ABC", font, 20, fontSize + 10);
//Set paint in the current graphics state
document.setPaint(Color.BLUE);
document.fill(rectanglePath);
document.writeGraphicsRestore();
document.translate(100, 100);
//Set paint in the current graphics state
document.setPaint(Color.BLUE);
document.setStroke(stroke);
//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/java/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.setBackgroundColor(new Color(211, 8, 48));
// Create new 1-paged PS Document
PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
float offsetX = 50;
float offsetY = 100;
float width = 200;
float height = 100;
///////////////////////////////// Create rectangle with opaque gradient fill /////////////////////////////////////////////////////////
GeneralPath path = new GeneralPath();
path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
LinearGradientPaint opaquePaint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
new float[] {0f, 1f}, new Color[] {new Color(0, 0, 0), new Color(40, 128, 70)} , MultipleGradientPaint.CycleMethod.NO_CYCLE,
MultipleGradientPaint.ColorSpaceType.SRGB, new AffineTransform(width, 0, 0, height, offsetX, offsetY));
document.setPaint(opaquePaint);
document.fill(path);
offsetX = 350;
///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
path = new GeneralPath();
path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
LinearGradientPaint translucentPaint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(width, height),
new float[] {0f, 1f}, new Color[] {new Color(0, 0, 0, 150), new Color(40, 128, 70, 50)}, MultipleGradientPaint.CycleMethod.NO_CYCLE,
MultipleGradientPaint.ColorSpaceType.SRGB, new AffineTransform(width, 0, 0, height, offsetX, offsetY));
document.setPaint(translucentPaint);
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/java/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(getOutputDir() + outputFileName, options, false);
document.translate(100, 100);
//Create graphics path from the rectangle
java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
path.append(new Rectangle2D.Float(0, 0, 150, 100), false);
////////////////////////////////////// No transformations ///////////////////////////////////////////////////////////////
//Set paint in graphics state on upper level
document.setPaint(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(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(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((float) Math.toRadians(45));
//Set paint in the current graphics state
document.setPaint(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(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 AffineTransform(1.2f, -0.965925f, 0.258819f, 1.5f, 0f, 50));
//Set paint in the current graphics state
document.setPaint(new Color(127, 255, 212)); // 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