在 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// Demonstrates clipping by shape and clipping by text in PS document.
2String outputFileName = "ApplyClipByShape_outPS.ps";
3
4//Create save options with A4 size
5PsSaveOptions options = new PsSaveOptions();
6
7// Create new 1-paged PS Document
8PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
9
10//Create graphics path from the rectangle
11GeneralPath rectanglePath = new GeneralPath();
12rectanglePath.append(new Rectangle2D.Float(0, 0, 300, 200), false);
13
14////////////////////////////////////// Clipping by shape ///////////////////////////////
15
16//Save graphics state in order to return back to this state after transformation
17document.writeGraphicsSave();
18
19//Displace current graphics state on 100 points to the right and 100 points to the bottom.
20document.translate(100, 100);
21
22//Create graphics path from the circle
23GeneralPath circlePath = new GeneralPath();
24circlePath.append(new Ellipse2D.Float(50, 0, 200, 200), false);
25
26//Add clipping by circle to the current graphics state
27document.clip(circlePath);
28
29//Set paint in the current graphics state
30document.setPaint(Color.BLUE);
31
32//Fill the rectangle in the current graphics state (with clipping)
33document.fill(rectanglePath);
34
35//Restore graphics state to the previous (upper) level
36document.writeGraphicsRestore();
37
38//Displace upper level graphics state on 100 points to the right and 100 points to the bottom.
39document.translate(100, 100);
40
41//Create dashed stroke similar to Pen with DashStyle.Dash
42float[] dash = new float[] { 5.0f };
43BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
44
45document.setStroke(stroke);
46
47//Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
48document.draw(rectanglePath);
49
50//Close current page
51document.closePage();
52
53//Save the document
54document.save();请参阅如何在 .NET 中使用 PS 文档中的剪辑。
运行此代码的结果显示为

在下一个示例中,我们获取一种字体,用于裁剪带有文本轮廓的蓝色填充矩形。
在本例中,要使用 Aspose.Page for Java 库将文本剪辑添加到新的 PsDocument,请执行以下步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 创建一个单页 PsDocument,并设置已创建的输出流和保存选项。
- 创建新的图形状态。
- 创建字体。
- 设置包含文本和字体的裁剪区域。
- 将绘制设置为 PsDocument 的当前图形状态。
- 使用当前绘制填充矩形路径。
- 从当前图形状态退出到上一级图形状态。
- 平移到已填充矩形的位置。
- 用虚线勾勒已填充矩形上方相同矩形的边界,以显示裁剪后的已填充矩形的边界。
- 关闭页面。
- 保存文档。
1// Demonstrates clipping by text in PS document.
2String outputFileName = "ApplyClipByText_outPS.ps";
3
4//Create save options with A4 size
5PsSaveOptions options = new PsSaveOptions();
6
7// Create new 1-paged PS Document
8PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
9
10//Create graphics path from the rectangle
11GeneralPath rectanglePath = new GeneralPath();
12rectanglePath.append(new Rectangle2D.Float(0, 0, 300, 200), false);
13
14//Save graphics state in order to return back to this state after transformation
15document.writeGraphicsSave();
16
17//Displace current graphics state on 100 points to the right and 100 points to the bottom.
18document.translate(100, 100);
19
20float[] dash = new float[] { 5.0f };
21BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
22
23int fontSize = 120;
24Font font = new Font("Arial", Font.BOLD, fontSize);
25
26//Clip rectangle by text's outline
27document.clipText("ABC", font, 20, fontSize + 10);
28
29//Set paint in the current graphics state
30document.setPaint(Color.BLUE);
31
32document.fill(rectanglePath);
33
34document.writeGraphicsRestore();
35
36document.translate(100, 100);
37
38//Set paint in the current graphics state
39document.setPaint(Color.BLUE);
40
41document.setStroke(stroke);
42//Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
43document.draw(rectanglePath);
44
45//Close current page
46document.closePage();
47
48//Save the document
49document.save();请参阅 .NET 中 PS 文档中的剪辑操作。
运行此代码的结果显示为

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