在 PostScript 中使用形状 | .NET
在 PS 文档中添加形状
向 PS 添加矩形
为了使用 Aspose.Page for .NET 库将矩形添加到 PsDocument,我们应该执行以下步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 使用已创建的输出流和保存选项创建一个单页 PsDocument。
- 从该矩形创建一个矩形 System.Drawing.Drawing2D.GraphicsPath。
- 将 Paint 设置为 PsDocument 的当前图形状态。
- 填充矩形路径。
- 关闭页面。
- 保存文档。
如果我们需要描边(勾勒)一个矩形,前 4 步和后 2 步相同,但第 5 点和第 6 点是:
将描边设置为 PsDocument 的当前图形状态。
描边(勾勒)矩形路径。
1//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "AddRectangle_outPS.ps", FileMode.Create))
3{
4 //Create save options with A4 size
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 //Create graphics path from the first rectangle
11 GraphicsPath path = new GraphicsPath();
12 path.AddRectangle(new RectangleF(250, 100, 150, 100));
13 //Set paint
14 document.SetPaint(new SolidBrush(Color.Orange));
15 //Fill the rectangle
16 document.Fill(path);
17
18 //Create graphics path from the second rectangle
19 path = new GraphicsPath();
20 path.AddRectangle(new RectangleF(250, 300, 150, 100));
21 //Set stroke
22 document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
23 //Stroke (outline) the rectangle
24 document.Draw(path);
25
26 //Close current page
27 document.ClosePage();
28
29 //Save the document
30 document.Save();
31}
对于 Linux、MacOS 和其他非 Windows 操作系统,我们建议使用我们的 Aspose.Page.Drawing Nuget 软件包。它使用 Aspose.Drawing 后端,而非 System.Drawing 系统库。
因此,请导入 Aspose.Page.Drawing 命名空间,而不是 System.Drawing 命名空间。在以上和以下代码片段中,将使用 Aspose.Page.Drawing.Rectangle 代替 System.Drawing.Rectangle,使用 Aspose.Page.Drawing.Drawing2D.GraphicsPath 代替 System.Drawing.Drawing2D.GraphicsPath,等等。我们在 GitHub 上的代码示例包含所有必要的替换。
请参阅 Java 中 PS 文档中形状的使用方法。
运行此代码的结果显示为:
将椭圆添加到 PS
要将椭圆添加到 PsDocument,还需要执行以下 8 个步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 创建一个单页 PsDocument,并配置已创建的输出流和保存选项。
- 根据矩形创建一个椭圆形 System.Drawing.Drawing2D.GraphicsPath。
- 将 Paint 设置为 PsDocument 的当前图形状态。
- 填充椭圆形路径。
- 关闭页面。
- 保存文档。
如果我们需要描边(勾勒)一个椭圆形,前 4 步和后 2 步相同,但第 5 点和第 6 点将改为:
将描边设置为 PsDocument 的当前图形状态。
描边(勾勒)椭圆路径。
1//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "AddEllipse_outPS.ps", FileMode.Create))
3{
4 //Create save options with A4 size
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 //Create graphics path from the first ellipse
11 GraphicsPath path = new GraphicsPath();
12 path.AddEllipse(new RectangleF(250, 100, 150, 100));
13 //Set paint
14 document.SetPaint(new SolidBrush(Color.Orange));
15 //Fill the ellipse
16 document.Fill(path);
17
18 //Create graphics path from the second ellipse
19 path = new SystemGraphicsPath();
20 path.AddEllipse(new RectangleF(250, 300, 150, 100));
21 //Set stroke
22 document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
23 //Stroke (outline) the ellipse
24 document.Draw(path);
25
26 //Close current page
27 document.ClosePage();
28
29 //Save the document
30 document.Save();
31}
运行此代码的结果如下:
如我们所见,任何形状,无论闭合与否,只要可以放入 System.Drawing.Drawing2D.GraphicsPath,都可以使用 PsDocument 进行填充或绘制。 它也可以进行裁剪,但这将在另一篇文章中介绍。
您可以从 GitHub下载示例和数据文件。