Lavorare con le forme in PostScript | C++
Aggiungi forme in un documento PS
Aggiungi un rettangolo a PS
Per aggiungere un rettangolo a PsDocument con la libreria Aspose.Page per C++, dobbiamo seguire i seguenti passaggi:
- Creare un flusso di output per il file PS risultante.
- Creare un oggetto PsSaveOptions con le opzioni predefinite.
- Creare un PsDocument a pagina singola con un flusso di output già creato e opzioni di salvataggio.
- Creare un rettangolo System.Drawing.Drawing2D.GraphicsPath dal rettangolo.
- Impostare un colore sullo stato grafico corrente di PsDocument.
- Riempire il percorso del rettangolo.
- Chiudere la pagina.
- Salvare il documento.
Se dobbiamo tracciare il contorno di un rettangolo, i primi 4 e gli ultimi 2 passaggi saranno gli stessi, ma i punti 5 e 6 saranno:
Impostare il contorno allo stato grafico corrente di PsDocument.
Tracciare il contorno del rettangolo.
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 }
Il risultato dell’esecuzione di questo codice viene visualizzato come
Aggiungi ellisse a PS
Per aggiungere un’ellisse a PsDocument sono necessari anche 8 passaggi:
- Creare un flusso di output per il file PS risultante. 2. Crea un oggetto PsSaveOptions con le opzioni predefinite.
- Crea un PsDocument a pagina singola con un flusso di output già creato e opzioni di salvataggio.
- Crea un’ellisse System.Drawing.Drawing2D.GraphicsPath dal rettangolo.
- Imposta il colore sullo stato grafico corrente del PsDocument.
- Riempi il tracciato dell’ellisse.
- Chiudi la pagina.
- Salva il documento.
Se dobbiamo tracciare il contorno di un’ellisse, i primi 4 e gli ultimi 2 passaggi saranno gli stessi, ma i punti 5 e 6 saranno:
- Imposta il contorno sullo stato grafico corrente del PsDocument.
- Traccia il contorno del tracciato dell’ellisse.
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 }
Il risultato dell’esecuzione di questo codice appare come
Come possiamo vedere, qualsiasi forma, sia chiusa che aperta, che può essere inserita in System.Drawing.Drawing2D.GraphicsPath può essere riempita o disegnata da PsDocument. Può anche essere ritagliata, ma questo verrà descritto in un altro articolo.
È possibile scaricare esempi e file di dati da GitHub.