在 PS 文件中使用透明度 | Java
在 PS 文档中添加透明度
PostScript 不支持在绘制矢量图形对象时使用透明度。但是,半透明(部分透明)图像可以渲染为一组完全透明和完全不透明的像素。 此类图像称为蒙版。
Aspose.Page for Java 库提供了一种将透明图像添加到 PS 文档的方法。对于绘制矢量图形(例如形状或文本),我们提供**“伪透明”**。 “伪透明” 是一种将 Alpha 值小于 255 的颜色变浅的过程。它是通过将红色、绿色和蓝色成分与 Alpha 值 1 混合来实现的。 “伪透明” 当然不允许我们从上层透明层下方看到下层彩色层,但如果底层为白色,则会造成透明的错觉。
在 PS 文档中添加透明图像
正如我们之前所写,透明图像可以作为蒙版添加到 PS 文档中,Aspose.Page for Java 库为此提供了addTransparentImage()方法。 此方法可以识别图像是完全不透明、完全透明还是半透明。如果是完全不透明,则会在addImage()方法中将其作为不透明图像添加;如果是完全透明,则根本不会添加到文档中;如果是半透明图像,则会将其作为 PostScript 图像蒙版添加。
在下面的示例中,我们演示了使用**addImage()和addTransparentImage()**在 PS 文档中添加透明图像的区别。 为了显示白色半透明图像,我们在图像下方放置了一个大的红色矩形。
为了使用 Aspose.Page for Java 库将任何图像添加到新的 PsDocument,在本例中,我们执行以下步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 使用已创建的输出流和保存选项创建一个单页 PsDocument。
- 创建新的图形状态。
- 从图像文件创建 java.awt.BufferedImage。
- 为图像创建必要的转换。
- 如果确定图像不透明,则使用 addImage() 方法将其作为完全不透明图像添加到 PsDocument;如果不确定图像不透明,则使用 addTransparentImage() 方法将其作为透明图像添加。
- 从当前图形状态退出到上一级图形状态。
- 关闭页面。
- 保存文档。
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();请参阅 .NET 中 PS 文档透明度的使用方法。
运行此代码的结果如下:

添加透明矢量图形对象
之前我们提到,Aspose.Page for Java 库使用一种针对透明形状和文本的渐变算法,我们称之为**“伪透明”**。
在下面的示例中,我们演示了使用相同颜色绘制的两个形状之间的区别,但第一个形状没有 Alpha 分量,而第二个形状有 Alpha 分量。
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();运行此代码的结果显示为

您可以从 GitHub下载示例和数据文件。