Working with Transparency in PS file | Java

Add transparency in PS document

PostScript doesn’t support transparency in painting vector graphics objects. However, translucent (partially transparent) images can be rendered as a set of fully transparent and fully opaque pixels. Such images are called masks.

Aspose.Page for Java library offers a method that adds the transparent image to the PS document. As for painting vector graphics: shapes or text, we offer “pseudo-transparency”. “Pseudo-transparency” is a process of paling colors that have an Alpha component of less than 255. It is reached by the blending of Red, Green, and Blue components with Alpha one. “Pseudo-transparency”, of course, doesn’t allow us to see the lower colored layer from under the upper transparent layer, but makes an illusion of transparency if the bottom layer is white.

Add transparent image in PS document

As we wrote earlier transparent images can be added to the PS document as a mask and Aspose.Page for Java library offers for this purpose a method addTransparentImage(). This method recognizes whether the image is fully opaque or fully transparent or translucent. If it is fully opaque it is added as the opaque image in addImage() method, if it is fully transparent it is not added to the document at all, if it is the translucent image it is added as a PostScript image mask.

In the example below we demonstrate the difference between adding a transparent image in a PS document with addImage() and addTransparentImage(). In order to see the white translucent image we put big red rectangle under the images.

In order to add any image to a new PsDocument with Aspose.Page for Java 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 java.awt.BufferedImage from image file.
  6. Create the necessary transformation for the image.
  7. Add the image to PsDocument as a fully opaque image (using addImage() method) if we are sure that the image is opaque or add one as a transparent image (using addTransparentImage() method) if we are not sure that the image is opaque.
  8. Exit from the current graphics state to upper level one.
  9. Close the page.
  10. Save the document.
 1// Add transparent image to PS document.
 2
 3String outputFileName = "AddTransparentImage_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7//Set page's background color to see white image on it's own transparent background
 8options.setBackgroundColor(new Color(211, 8, 48));
 9
10// Create new 1-paged PS Document
11PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
12
13//document.setPaint(new Color(211, 8, 48));
14//document.fill(new Rectangle2D.Float(0, 0, (int) options.getPageSize().getWidth(), 300));
15
16document.writeGraphicsSave();
17document.translate(20, 100);
18
19//Create an image from translucent image file
20BufferedImage image = ImageIO.read(new File(getDataDir() + "mask1.png"));
21
22//Add this image to document as usual opaque RGB image
23document.drawImage(image, new AffineTransform(1, 0, 0, 1, 100, 0), null);
24
25//Add this image to document as transparent image
26document.drawTransparentImage(image, new AffineTransform(1, 0, 0, 1, 350, 0), 255);
27
28document.writeGraphicsRestore();
29
30//Close current page
31document.closePage();
32
33//Save the document
34document.save();

See working with transparency in PS document in .NET.

The result of running this code is next

Add Transparent Image

Adding transparent vector graphics object

Earlier we wrote that Aspose.Page for Java library uses a paling algorithm for transparent shapes and text, which we called “pseudo-transparency”.

In the example below we demonstrate a difference between two shapes painted with the same color, but in the first shape without the Alpha component and in the second case with the Alpha component.

 1// Apply pseudo-transparency transparent image to PS document.
 2
 3String outputFileName = "ApplyPseudoTranparency_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7//Set page's background color to see white image on it's own transparent background
 8options.setBackgroundColor(new Color(211, 8, 48));
 9
10// Create new 1-paged PS Document
11PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
12
13float offsetX = 50;
14float offsetY = 100;
15float width = 200;
16float height = 100;
17
18///////////////////////////////// Create rectangle with opaque gradient fill /////////////////////////////////////////////////////////
19GeneralPath path = new GeneralPath();
20path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
21
22LinearGradientPaint opaquePaint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
23        new float[] {0f, 1f}, new Color[] {new Color(0, 0, 0), new Color(40, 128, 70)} , MultipleGradientPaint.CycleMethod.NO_CYCLE,
24        MultipleGradientPaint.ColorSpaceType.SRGB, new AffineTransform(width, 0, 0, height, offsetX, offsetY));
25
26document.setPaint(opaquePaint);
27document.fill(path);
28
29offsetX = 350;
30
31///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
32path = new GeneralPath();
33path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
34
35LinearGradientPaint translucentPaint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(width, height),
36        new float[] {0f, 1f}, new Color[] {new Color(0, 0, 0, 150), new Color(40, 128, 70, 50)}, MultipleGradientPaint.CycleMethod.NO_CYCLE,
37        MultipleGradientPaint.ColorSpaceType.SRGB, new AffineTransform(width, 0, 0, height, offsetX, offsetY));
38
39document.setPaint(translucentPaint);
40document.fill(path);
41
42//Close current page
43document.closePage();
44
45//Save the document
46document.save();

See working with transparency in PS documents in .NET and C++.

The result of running this code is appeared as

Show Pseudo Transparency

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.