在 PS 文件中使用形状 | Java

在 PS 文档中添加形状

向 PS 添加矩形

为了使用 Aspose.Page for Java 库将矩形添加到 PsDocument,我们需要执行以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 使用默认选项创建 PsSaveOptions 对象。
  3. 使用已创建的输出流和保存选项创建一个单页 PsDocument。
  4. 创建一个矩形(java.awt.geom.Rectangle2D 对象)。
  5. 将 Paint 设置为 PsDocument 的当前图形状态。
  6. 填充矩形。
  7. 关闭页面。
  8. 保存文档。

如果我们需要描边(勾勒)一个矩形,前 4 步和后 2 步相同,但第 5 点和第 6 点是:

  1. 将描边设置为 PsDocument 的当前图形状态。

  2. 描边(勾勒)矩形。

 1// Add Rectangle to PS document.
 2
 3String outputFileName = "AddRectangle_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11//Create graphics path from the first rectangle
12GeneralPath path = new GeneralPath();
13path.append(new Rectangle2D.Float(250, 100, 150, 100), false);
14//Set paint
15document.setPaint(Color.ORANGE);
16//Fill the rectangle
17document.fill(path);
18
19//Create graphics path from the second rectangle
20path = new GeneralPath();
21path.append(new Rectangle2D.Float(250, 300, 150, 100), false);
22//Set stroke
23document.setStroke(new java.awt.BasicStroke(3));
24//Stroke (outline) the rectangle
25document.draw(path);
26
27//Close current page
28document.closePage();
29
30//Save the document
31document.save();

请参阅在 .NET 中如何在 PS 文档中使用形状。


运行此代码的结果显示为:

添加矩形

将椭圆添加到 PS

要将椭圆添加到 PsDocument,还需要执行 8 个步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 使用默认选项创建 PsSaveOptions 对象。
  3. 使用已创建的输出流和保存选项创建一个单页 PsDocument。
  4. 创建一个椭圆(java.awt.geom.Ellipse2D 对象)。
  5. 将 Paint 设置为 PsDocument 的当前图形状态。
  6. 填充椭圆路径。
  7. 关闭页面。
  8. 保存文档。

如果我们需要描边(勾勒)一个椭圆,前 4 步和后 2 步相同,但第 5 步和第 6 步将改为:

  1. 将 stroke 设置为 PsDocument 的当前图形状态。
  2. 描边(勾勒)椭圆:
 1// Add ellipse to PS document.
 2
 3String outputFileName = "AddEllipse_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11//Create graphics path from the first ellipse
12GeneralPath path = new GeneralPath();
13path.append(new java.awt.geom.Ellipse2D.Float(250, 100, 150, 100), false);
14//Set paint
15document.setPaint(Color.ORANGE);
16//Fill the ellipse
17document.fill(path);
18
19//Create graphics path from the second ellipse
20path = new GeneralPath();
21path.append(new java.awt.geom.Ellipse2D.Float(250, 300, 150, 100), false);
22//Set stroke
23document.setStroke(new java.awt.BasicStroke(3));
24//Stroke (outline) the ellipse
25document.draw(path);
26
27//Close current page
28document.closePage();
29
30//Save the document
31document.save();

T运行此代码的结果如下:

添加椭圆

如我们所见,任何形状,无论闭合与否,都可以用 PsDocument 填充或绘制。

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.