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:
- 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.
- Create java.awt.BufferedImage from image file.
- Create the necessary transformation for the image.
- 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.
- Exit from the current graphics state to upper level one.
- Close the page.
- Save the document.
1//Create an output stream for PostScript document
2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddTransparentImage_outPS.ps");
3//Create save options with A4 size
4PsSaveOptions options = new PsSaveOptions();
5
6//Create new PS Document with the page opened
7PsDocument document = new PsDocument(outPsStream, options, false);
8
9//Add big red rectangle under images to see the difference between addImage() and addTransparentImage() methods
10document.setPaint(new Color(211, 8, 48));
11document.fill(new Rectangle2D.Float(0, 0, (int) options.getPageSize().getWidth(), 300));
12
13document.writeGraphicsSave();
14document.translate(20, 100);
15
16//Create an image from translucent image file
17BufferedImage image = ImageIO.read(new File(dataDir + "mask1.png"));
18
19//Add this image to document as usual opaque RGB image
20document.drawImage(image, new AffineTransform(1, 0, 0, 1, 100, 0), null);
21
22//Add this image to document as transparent image
23document.drawTransparentImage(image, new AffineTransform(1, 0, 0, 1, 350, 0), 255);
24
25document.writeGraphicsRestore();
26
27//Close current page
28document.closePage();
29//Save the document
30document.save();
See working with transparency in PS document in .NET.
The result of running this code is next
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//Create an output stream for PostScript document
2FileOutputStream outPsStream = new FileOutputStream(dataDir + "ShowPseudoTransparency_outPS.ps");
3//Create save options with A4 size
4PsSaveOptions options = new PsSaveOptions();
5
6//Create new PS Document with the page opened
7PsDocument document = new PsDocument(outPsStream, options, false);
8
9float offsetX = 50;
10float offsetY = 100;
11float width = 200;
12float height = 100;
13
14///////////////////////////////// Create rectangle with opaque gradient fill ////////////////////////////////////////////////////////
15Rectangle2D.Float rectangle = new Rectangle2D.Float(offsetX, offsetY, width, height);
16
17LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
18 new float [] {0, 1}, new Color [] {new Color(0, 0, 0), new Color(40, 128, 70)},
19 MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
20 new AffineTransform(width, 0, 0, height, offsetX, offsetY));
21
22//Set paint
23document.setPaint(paint);
24//Fill the rectangle
25document.fill(rectangle);
26/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27
28offsetX = 350;
29
30///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
31rectangle = new Rectangle2D.Float(offsetX, offsetY, width, height);
32
33paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
34new float [] {0, 1}, new Color [] {new Color(0, 0, 0, 150), new Color(40, 128, 70, 50)},
35MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
36new AffineTransform(width, 0, 0, height, offsetX, offsetY));
37
38//Set paint
39document.setPaint(paint);
40//Fill the rectangle
41document.fill(rectangle);
42/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43
44//Close current page
45document.closePage();
46//Save the document
47document.save();
The result of running this code is appeared as
You can download examples and data files from GitHub.