在 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//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddRectangle_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
 9//Set paint for filling rectangle
10document.setPaint(Color.ORANGE);        
11//Fill the first rectangle
12document.fill(new Rectangle2D.Float(250, 100, 150, 100));
13
14//Set paint for stroking rectangle
15document.setPaint(Color.RED);
16//Set stroke
17document.setStroke(new BasicStroke(3));
18//Stroke (outline) the second rectangle
19document.draw(new Rectangle2D.Float(250, 300, 150, 100));
20
21//Close current page
22document.closePage();
23//Save the document
24document.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//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddEllipse_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new multipaged PS Document with one page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Set paint for filling rectangle
10document.setPaint(Color.ORANGE);        
11//Fill the first ellipse
12document.fill(new Ellipse2D.Float(250, 100, 150, 100));
13
14//Set paint for stroking rectangle
15document.setPaint(Color.RED);
16//Set stroke
17document.setStroke(new BasicStroke(3));
18//Stroke (outline) the second ellipse
19document.draw(new Ellipse2D.Float(250, 300, 150, 100));
20
21//Close current page
22document.closePage();
23//Save the document
24document.save();

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

添加椭圆

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

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.