在 PS 文件中使用填充图案 | Java

在 PS 文档中添加阴影图案

阴影图案是一种纹理平铺图案,通常用简单的双色(通常为黑白)小图像表示。这些小图像的主要内容是各种阴影。

Java 平台没有单独的类用于使用阴影绘制形状和文本。但是,Aspose.Page for Java 库提供了 com.aspose.eps.HatchPaintLibrary 类,该类可以创建 java.awt.TexturePaint,并使用 com.aspose.eps.HatchStyle 提供的 55 种样式之一定义的阴影填充。

为了在 Aspose.Page for Java 库中使用填充图案绘制图形对象,需要创建填充图案 java.awt.TexturePaint 并指定填充样式,然后将其传递给 setPaint() 或接受 java.awt.Paint 作为参数的 fillText()fillAndStrokeText() 方法之一。

为了在 Aspose.Page for Java 库中使用填充图案勾勒图形对象的轮廓,需要在 PsDocument 中将填充图案设置为当前绘制,创建新的 java.awt.BasicStroke 并将其传递给 setStroke() 或接受 java.awt.Stroke 作为参数的 outlineText()fillAndStrokeText() 方法之一。

在下面的示例中,我们首先演示如何使用填充图案填充形状,然后演示 Java 中各种填充样式,最后演示如何使用填充图案填充和勾勒文本轮廓。

在新的 PS 文档中使用填充图案绘制图形对象的算法包括以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 创建 PsSaveOptions
  3. 使用已创建的输出流和保存选项创建 PsDocument
  4. 根据要填充或勾勒轮廓的对象创建形状或字体。
  5. 使用 com.aspose.eps.HatchPaintLibrary 创建一个具有所需样式的 java.awt.TexturePaint 对象。
  6. 将阴影线绘制设置为 PsDocument 中的当前绘制
  7. 使用当前绘制填充形状或填充文本。如果我们使用接受 java.awt.Paint 作为参数的文本填充方法,则可以忽略前一个点。
  8. 关闭页面。
  9. 保存文档。

如果我们需要使用阴影图案而不是最后四个点来描边(勾勒轮廓)图形对象,则需要执行以下操作:

  1. 将阴影线绘制设置为 PsDocument 中的当前绘制。

  2. 创建 java.awt.BasicStroke 对象。

  3. 将此描边设置为 PsDocument 中的当前描边。

  4. 使用当前绘制勾勒形状的轮廓,并对文本进行描边或勾勒轮廓。如果我们使用接受 java.awt.Stroke 作为参数的文本勾勒轮廓方法,则可以忽略前一个点。

  5. 关闭页面。

  6. 保存文档。

 1// The path to the documents directory.
 2String dataDir = Utils.getDataDir();
 3
 4//Create output stream for PostScript document
 5FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddHatchPattern_outPS.ps");
 6//Create save options with A4 size
 7PsSaveOptions options = new PsSaveOptions();
 8
 9//Create new PS Document with the page opened
10PsDocument document = new PsDocument(outPsStream, options, false);
11
12int x0 = 20;
13int y0 = 100;
14int squareSide = 32;
15int width = 500;
16int sumX = 0;
17
18//Restore graphics state
19document.writeGraphicsSave();
20
21//Translate to initial point
22document.translate(x0, y0);
23
24//Create a square for every pattern
25Rectangle2D.Float square = new Rectangle2D.Float(0, 0, squareSide, squareSide);
26
27//Create pen for outlining pattern square
28BasicStroke stroke = new BasicStroke(2);
29
30HatchStyle [] hatchStyles = HatchStyle.values();
31
32//For every hatch pattern style 
33for (int i = 0; i < hatchStyles.length; i++) {            
34	//Create a hatch texture pattern by hatch style, foreground and background colors
35	TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(hatchStyles[i], Color.BLACK, Color.WHITE);
36	//Set paint with the current hatch pattern
37    document.setPaint(paint);
38
39    //Calculate a displacement in order to don't go beyond the page bounds
40    int x = squareSide;
41    int y = 0;
42    if (sumX >= width) {
43        x = -(sumX - squareSide);
44        y += squareSide;
45    }
46
47    //Translate current graphics state
48    document.translate(x, y);
49    //Fill pattern square
50    document.fill(square);
51
52    //Set current paint
53    document.setPaint(Color.BLACK);
54    //Set current stroke
55    document.setStroke(stroke);
56    //Draw square outline
57    document.draw(square);
58
59    //Calculate distance from X0
60    if (sumX >= width)
61        sumX = squareSide;
62    else
63        sumX += x;
64}
65
66//Restore graphics state
67document.writeGraphicsRestore();
68
69//Fill a text with the hatch pattern
70TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.DiagonalCross, Color.RED, Color.YELLOW);
71Font font = new Font("Arial", Font.BOLD, 96);
72document.fillAndStrokeText("ABC", font, 200, 400, paint, Color.BLACK, stroke);
73
74//Outline the text with the hatch pattern
75paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.Percent70, Color.BLUE, Color.WHITE);
76document.outlineText("ABC", font, 200, 600, paint, new BasicStroke(5));
77
78//Close current page
79document.closePage();
80//Save the document
81document.save();

请参阅在 .NET 中使用 PS 文档中的填充图案。


运行此代码的结果显示为

添加填充图案

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.