在 PS 文件中使用剪辑 | Java
Contents
[
Hide
Show
]在 PS 文档中添加剪切
PS 文档中的剪切路径是指一条路径,它限定了当前图形状态的内容,这些内容将在 PS 查看器或编辑器中显示。超出边界的内容将被截断。
Java 中的剪切路径可以通过三种方式指定:
- 通过任何实现 java.awt.Shape 的类,该类可以包含任何闭合形状;
- 通过文本轮廓;
- 通过 1 bpp(每像素位数)双色图像作为模板蒙版;
目前,Aspose.Page for Java 库提供了第一种和第二种剪切方式。 在下面的示例中,我们获取一个圆形作为剪切路径,并在相同的图形状态下截断一个蓝色填充的矩形。
在本例中,要使用 Aspose.Page for Java 库将剪辑添加到新的 PsDocument,请执行以下步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 使用已创建的输出流和保存选项创建一个单页 PsDocument。
- 创建新的图形状态。
- 创建一个圆形(java.awt.geom.Ellipse2D 对象)。
- 使用此路径设置剪辑。
- 将绘图设置为 PsDocument 的当前图形状态。
- 使用当前绘图填充矩形路径。
- 从当前图形状态退出到上一级图形状态。
- 平移到已填充矩形的位置。
- 用虚线勾勒已填充矩形上方相同矩形的边界,以显示裁剪后的已填充矩形的边界。
- 关闭页面。
- 保存文档。
1//Create an output stream for the PostScript document
2FileOutputStream outPsStream = new FileOutputStream(dataDir + "Clipping_outPS.ps");
3//Create save options with A4 size
4PsSaveOptions options = new PsSaveOptions();
5
6// Create a new PS Document with the page opened
7PsDocument document = new PsDocument(outPsStream, options, false);
8
9//Create a rectangle
10Shape rectangle = new Rectangle2D.Float(0, 0, 300, 200);
11
12//Set the paint in the upper level graphics state
13document.setPaint(Color.BLUE);
14
15//Save the graphics state to return back to this state after the transformation
16document.writeGraphicsSave();
17
18//Displace the current graphics state on 100 points to the right and 100 points to the bottom.
19document.translate(100, 100);
20
21//Create a circle shape
22Shape circle = new Ellipse2D.Float(50, 0, 200, 200);
23
24//Add clipping by the circle to the current graphics state
25document.clip(circle);
26
27//Fill the rectangle in the current graphics state (with clipping)
28document.fill(rectangle);
29
30//Restore the graphics state to the previus (upper) level
31document.writeGraphicsRestore();
32
33//Displace the upper-level graphics state on 100 points to the right and 100 points to the bottom.
34document.translate(100, 100);
35
36BasicStroke stroke = new BasicStroke(2,
37 BasicStroke.CAP_BUTT,
38 BasicStroke.JOIN_MITER,
39 10.0f, new float []{5.0f}, 0.0f);
40
41document.setStroke(stroke);
42
43//Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
44document.draw(rectangle);
45
46//Close the current page
47document.closePage();
48//Save the document
49document.save();
请参阅如何在 .NET 中使用 PS 文档中的剪辑。
运行此代码的结果显示为
在下一个示例中,我们获取一种字体,用于裁剪带有文本轮廓的蓝色填充矩形。
在本例中,要使用 Aspose.Page for Java 库将文本剪辑添加到新的 PsDocument,请执行以下步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 创建一个单页 PsDocument,并设置已创建的输出流和保存选项。
- 创建新的图形状态。
- 创建字体。
- 设置包含文本和字体的裁剪区域。
- 将绘制设置为 PsDocument 的当前图形状态。
- 使用当前绘制填充矩形路径。
- 从当前图形状态退出到上一级图形状态。
- 平移到已填充矩形的位置。
- 用虚线勾勒已填充矩形上方相同矩形的边界,以显示裁剪后的已填充矩形的边界。
- 关闭页面。
- 保存文档。
1//Create output stream for PostScript document
2FileOutputStream outPsStream = new FileOutputStream(dataDir + "Clipping_outPS.ps");
3//Create save options with A4 size
4PsSaveOptions options = new PsSaveOptions();
5
6// Create a new PS Document with the page opened
7PsDocument document = new PsDocument(outPsStream, options, false);
8
9//Create a rectangle
10Shape rectangle = new Rectangle2D.Float(0, 0, 300, 200);
11
12//Set paint in the upper-level graphics state
13document.setPaint(Color.BLUE);
14
15//Save graphics state to return back to this state after the transformation
16document.writeGraphicsSave();
17
18//Displace current graphics state on 100 points to the right and 100 points to the bottom.
19document.translate(100, 100);
20
21int fontSize = 120;
22Font font = new Font("Arial", Font.BOLD, fontSize);
23
24//Clip rectangle by text's outline
25document.clipText("ABC", font, 20, fontSize + 10);
26document.fill(rectangle);
27
28//Restore graphics state to the previous (upper) level
29document.writeGraphicsRestore();
30
31//Displace upper-level graphics state on 100 points to the right and 100 points to the bottom.
32document.translate(100, 100);
33
34BasicStroke stroke = new BasicStroke(2,
35 BasicStroke.CAP_BUTT,
36 BasicStroke.JOIN_MITER,
37 10.0f, new float []{5.0f}, 0.0f);
38
39document.setStroke(stroke);
40
41//Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
42document.draw(rectangle);
43
44//Close the current page
45document.closePage();
46//Save the document
47document.save();
请参阅 .NET 中 PS 文档中的剪辑操作。
运行此代码的结果显示为
您可以从 GitHub下载示例和数据文件。