Working with Shapes in PostScript | C++
Add Shapes in PS Document
Add Rectangle to PS
In order to add a rectangle to PsDocument with Aspose.Page for C++ library we should do the following steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a rectangle System.Drawing.Drawing2D.GraphicsPath from the rectangle.
- Set a paint to the current graphics state of PsDocument.
- Fill the rectangle path.
- Close the page.
- Save the document.
If we need to stroke (outline) a rectangle the first 4 and the last 2 steps will be the same, but points 5 and 6 will be:
Set the stroke to the current graphics state of PsDocument.
Stroke (outline) the rectangle path.
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 }
The result of running this code is appeared as
Add Ellipse to PS
In order to add ellipse to PsDocument also 8 steps are required:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create an ellipse System.Drawing.Drawing2D.GraphicsPath from the rectangle.
- Set paint to the current graphics state of PsDocument.
- Fill the ellipse path.
- Close the page.
- Save the document.
If we need to stroke (outline) an ellipse the first 4 and the last 2 steps will be the same but points 5 and 6 will be:
- Set stroke to the current graphics state of PsDocument.
- Stroke (outline) the ellipse path.
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 }
The result of running this code is appeared as
As we can see, any shape, both closed and unclosed, that can be put in System.Drawing.Drawing2D.GraphicsPathcan be filled or drawn by PsDocument. It also can be clipped, but it will be described in another article.
You can download examples and data files from GitHub.