在 PostScript 中使用形状 | C++
Contents
[
Hide
Show
]在 PS 文档中添加形状
向 PS 添加矩形
为了使用 Aspose.Page for C++ 库向 PsDocument 添加矩形,我们需要执行以下步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 创建一个单页 PsDocument,并使用已创建的输出流和保存选项。
- 从该矩形创建一个矩形 System.Drawing.Drawing2D.GraphicsPath。
- 将 Paint 设置为 PsDocument 的当前图形状态。
- 填充矩形路径。
- 关闭页面。
- 保存文档。
如果我们需要描边(勾勒)一个矩形,前 4 步和后 2 步相同,但第 5 点和第 6 点是:
- 将描边设置为 PsDocument 的当前图形状态。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithShapes();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddRectangle_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 // Create new 1-paged PS Document
17 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18
19 //Create graphics path from the first rectangle
20 System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
21 path->AddRectangle(System::Drawing::RectangleF(250.0f, 100.0f, 150.0f, 100.0f));
22 //Set paint
23 document->SetPaint(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()));
24 //Fill the rectangle
25 document->Fill(path);
26
27 //Create graphics path from the second rectangle
28 path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
29 path->AddRectangle(System::Drawing::RectangleF(250.0f, 300.0f, 150.0f, 100.0f));
30 //Set stroke
31 document->SetStroke(System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Red()), 3.0f));
32 //Stroke (outline) the rectangle
33 document->Draw(path);
34
35 //Close current page
36 document->ClosePage();
37
38 //Save the document
39 document->Save();
40 }
41 catch(...)
42 {
43 __dispose_guard_0.SetCurrentException(std::current_exception());
44 }
45 }
运行此代码的结果如下
将椭圆添加到 PS
要将椭圆添加到 PsDocument,还需要 8 个步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 使用已创建的输出流和保存选项创建一个单页 PsDocument。
- 根据矩形创建一个椭圆 System.Drawing.Drawing2D.GraphicsPath。
- 将 Paint 设置为 PsDocument 的当前图形状态。
- 填充椭圆路径。
- 关闭页面。
- 保存文档。
如果我们需要描边(勾勒)一个椭圆,前 4 步和后 2 步相同,但第 5 点和第 6 点如下:
将描边设置为 PsDocument 的当前图形状态。
描边(勾勒)椭圆路径。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithShapes();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddEllipse_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 // Create new 1-paged PS Document
17 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18
19 //Create graphics path from the first ellipse
20 System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
21 path->AddEllipse(System::Drawing::RectangleF(250.0f, 100.0f, 150.0f, 100.0f));
22 //Set paint
23 document->SetPaint(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()));
24 //Fill the ellipse
25 document->Fill(path);
26
27 //Create graphics path from the second ellipse
28 path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
29 path->AddEllipse(System::Drawing::RectangleF(250.0f, 300.0f, 150.0f, 100.0f));
30 //Set stroke
31 document->SetStroke(System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Red()), 3.0f));
32 //Stroke (outline) the ellipse
33 document->Draw(path);
34
35 //Close current page
36 document->ClosePage();
37
38 //Save the document
39 document->Save();
40 }
41 catch(...)
42 {
43 __dispose_guard_0.SetCurrentException(std::current_exception());
44 }
45 }
运行此代码的结果如下
如我们所见,任何形状,无论闭合与否,只要可以放入 System.Drawing.Drawing2D.GraphicsPath,都可以使用 PsDocument 进行填充或绘制。 它也可以进行裁剪,但这部分内容将在另一篇文章中介绍。
您可以从 GitHub下载示例和数据文件。