在 PS 文件中使用纹理 | Java

在 PS 文档中添加纹理平铺图案

纹理平铺图案是用于填充或绘制对象(例如形状或文本)的图像。如果图像尺寸小于对象尺寸,则会在 X 和 Y 方向上重复,以覆盖所有必要的区域。

在图形对象内重复图像的过程称为平铺。为了在 PsDocument 中设置绘画或描边,我们必须将 java.awt.Paint 类的对象(用于绘画)和 java.awt.Stroke 类的对象(用于描边)分别传递给相应的方法。

Aspose.Page for Java 库处理 Java 平台提供的所有实现 java.awt.Paint 的重要类。这些是 java.awt.Colorjava.awt.TexturePaintjava.awt.LinearGradientPaintjava.awt.RadialGradientPaint。在 Java 中,描边颜色与 java.awt.Stroke 对象中的描边属性分开分配,并再次使用 java.awt.Paint。因此,Aspose.Page for Java 库也可以使用一整套 Paint 实现来绘制线条、勾勒形状和文本的轮廓。

为了在 Aspose.Page for Java 库中为带有纹理图案的图形对象“绘制”,只需将 java.awt.TexturePaint 传递给 setPaint() 或接受 java.awt.Paint 作为参数的 fillText()fillAndStrokeText() 方法即可。

为了在 Aspose.Page for Java 库中使用纹理图案勾勒图形对象,您还应该将 java.awt.TexturePaint 对象传递给 setPaint() 或接受描边绘制 (strokepaint) 作为参数的 outlineText()fillAndStrokeText() 方法之一。

在下面的示例中,我们演示了如何使用纹理平铺图案填充形状和文本,以及如何勾勒文本的轮廓。

示例中使用纹理图案和 PsDocument 的步骤描述:

  1. 为生成的 PS 文件创建输出流。
  2. 使用默认选项创建 PsSaveOptions 对象。
  3. 使用已创建的输出流和保存选项创建一个单页 PsDocument。
  4. 创建新的图形状态并将其平移到所需位置。
  5. 从图像文件创建 java.awt.image.BufferedImage
  6. 从图像文件创建 java.awt.TexturePaint
  7. 在纹理画笔中设置必要的变换。
  8. 将纹理画笔设置为 PsDocument 当前图形状态中的当前画笔。
  9. 创建矩形路径。
  10. 使用纹理画笔填充矩形。
  11. 将当前画笔保存为局部变量以备将来使用。
  12. 将当前画笔颜色设置为红色。
  13. 使用 java.awt.BasicStroke 将描边设置为 2 磅宽度。
  14. 使用当前描边勾勒矩形轮廓。
  15. 从当前图形状态退出到上一级图形状态。
  16. 创建系统字体。
  17. 填充并描边(勾勒)文本。使用纹理颜料进行填充,使用黑色笔进行描边。
  18. 使用纹理颜料和新的 java.awt.BasicStroke 在另一个位置勾勒出文本轮廓。
  19. 关闭页面。
  20. 保存文档。
 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddTextureTilingPattern_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
 9document.writeGraphicsSave();
10document.translate(200, 100);
11
12//Create a BufferedImage object from image file
13BufferedImage image = ImageIO.read(new File(dataDir + "TestTexture.bmp"));
14
15//Create image area doubled in width
16Rectangle2D.Float imageArea = new Rectangle2D.Float(0, 0, image.getWidth() * 2, image.getHeight());
17//Create texture brush from the image
18TexturePaint paint = new TexturePaint(image, imageArea);
19
20//Create rectangle
21Rectangle2D.Float shape = new Rectangle2D.Float(0, 0, 200, 100);
22
23//Set this texture brush as current paint
24document.setPaint(paint);
25//Fill rectangle
26document.fill(shape);
27
28
29document.setPaint(Color.RED);
30document.setStroke(new BasicStroke(2));
31document.draw(shape);
32
33document.writeGraphicsRestore();
34
35//Fill the text with the texture pattern
36Font font = new Font("Arial", Font.BOLD, 96);
37document.fillAndStrokeText("ABC", font, 200, 300, paint, Color.BLACK, new BasicStroke(2));
38
39//Outline the text with the texture pattern
40document.outlineText("ABC", font, 200, 400, paint, new BasicStroke(5));
41
42//Close current page
43document.closePage();
44//Save the document
45document.save();

请参阅在 .NET 中使用 PS 文档的纹理。


运行此代码的结果显示为

添加纹理平铺图案

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.