Trabalhar com Formas em PostScript | C++
Adicionar Formas no Documento PS
Adicionar Retângulo ao PS
Para adicionar um rectângulo ao PsDocument com a biblioteca Aspose.Page para C++, devemos seguir os seguintes passos:
- Criar um fluxo de saída para o ficheiro PS resultante.
- Criar o objeto PsSaveOptions com as opções padrão.
- Criar um PsDocument de 1 página com um fluxo de saída já criado e opções de guardar.
- Criar um retângulo System.Drawing.Drawing2D.GraphicsPath a partir do retângulo.
- Definir uma pintura para o estado gráfico atual do PsDocument.
- Preencher o percurso do retângulo.
- Fechar a página.
- Guarde o documento.
Se precisarmos de traçar (contornar) um retângulo, os primeiros 4 e os últimos 2 passos serão os mesmos, mas os pontos 5 e 6 serão:
Defina o traçado para o estado gráfico atual do PsDocument.
Trace (contorne) o percurso do retângulo.
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 }
O resultado da execução deste código é apresentado como
Adicionar Elipse ao PS
Para adicionar uma elipse ao PsDocument, são também necessários 8 passos:
- Criar um fluxo de saída para o ficheiro PS resultante.
- Crie o objeto PsSaveOptions com as opções padrão.
- Crie um PsDocument de 1 página com um fluxo de saída já criado e opções de guardar.
- Crie uma elipse System.Drawing.Drawing2D.GraphicsPath a partir do retângulo.
- Defina a pintura para o estado gráfico atual do PsDocument.
- Preencha o percurso da elipse.
- Feche a página.
- Guarde o documento.
Se precisarmos de traçar (contornar) uma elipse, os primeiros 4 e os últimos 2 passos serão os mesmos, mas os pontos 5 e 6 serão:
Defina o traçado para o estado gráfico atual do PsDocument.
Trace (contorne) o percurso da elipse.
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 }
O resultado da execução deste código é apresentado como
Como podemos ver, qualquer forma, fechada ou não, que possa ser colocada em System.Drawing.Drawing2D.GraphicsPath pode ser preenchida ou desenhada por PsDocument. Também pode ser recortada, mas isso será descrito noutro artigo.
Pode descarregar exemplos e ficheiros de dados do GitHub.