在 PS 文件中处理图像 | Java

Contents
[ Hide Show ]

在 PS 文档中添加图像

Aspose.Page for Java 库提供了两种将图像添加到 PS 文档的方法:

之所以采用这种方法,是因为 PostScript 不支持透明度,而半透明图像可以渲染为一组完全透明和完全不透明的像素。这些图像被称为蒙版

如果我们想将半透明图像在 PS 文档中显示为蒙版,以更好地反映图像的透明度,我们需要对此类图像进行一些检查和预处理。检查和预处理需要时间。因此,如果确定图像完全不透明,最好使用第一种方法,因为它可以节省执行时间。

第二种方法可以识别图像是完全不透明、完全透明还是半透明。如果图像完全不透明,则在第一种方法中将其添加为不透明图像; 如果图像完全透明,则根本不会添加到文档中;如果图像是半透明图像,则将其添加为 PostScript 图像蒙版

在下面的示例中,我们演示了如何添加完全不透明的图像。“使用透明度”文章中将演示如何添加透明图像。

在本例中,为了使用 Aspose.Page for Java 库将图像添加到新的 PsDocument,我们执行以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 使用默认选项创建 PsSaveOptions 对象。
  3. 使用已创建的输出流和保存选项创建一个单页 PsDocument。
  4. 创建新的图形状态。
  5. 从图像文件创建 java.awt.image.BufferedImage
  6. 为图像创建必要的转换。
  7. 将图像添加到 PsDocument 对象。
  8. 从当前图形状态退出到上一级图形状态。
  9. 关闭页面。 11.保存文档。
 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddImage_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
10document.writeGraphicsSave();
11document.translate(100, 100);
12
13//Create a BufferedImage object from image file
14BufferedImage image = ImageIO.read(new File(dataDir + "TestImage Format24bppRgb.jpg"));
15
16//Create image transform
17AffineTransform transform = new AffineTransform();
18transform.translate(35, 300);
19transform.scale(3, 3);
20transform.rotate(-45);
21
22//Add image to document
23document.drawImage(image, transform, null);
24
25document.writeGraphicsRestore();
26
27//Close current page
28document.closePage();
29//Save the document
30document.save();

请参阅在 .NET 中处理 PS 文档中的图像。

运行此代码的结果显示为

添加图像

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.