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:

  1. Creare un flusso di output per il file PS risultante.
  2. Creare un oggetto PsSaveOptions con le opzioni predefinite.
  3. Creare un PsDocument a pagina singola con un flusso di output già creato e opzioni di salvataggio.
  4. Creare un rettangolo System.Drawing.Drawing2D.GraphicsPath dal rettangolo.
  5. Impostare un colore sullo stato grafico corrente di PsDocument.
  6. Riempire il percorso del rettangolo.
  7. Chiudere la pagina.
  8. 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:

  1. Impostare il contorno allo stato grafico corrente di PsDocument.

  2. 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    }

Vedi come lavorare con le forme nei documenti PS in .NET o Java.


Il risultato dell’esecuzione di questo codice viene visualizzato come

Aggiungi rettangolo

Aggiungi ellisse a PS

Per aggiungere un’ellisse a PsDocument sono necessari anche 8 passaggi:

  1. Creare un flusso di output per il file PS risultante. 2. Crea un oggetto PsSaveOptions con le opzioni predefinite.
  2. Crea un PsDocument a pagina singola con un flusso di output già creato e opzioni di salvataggio.
  3. Crea un’ellisse System.Drawing.Drawing2D.GraphicsPath dal rettangolo.
  4. Imposta il colore sullo stato grafico corrente del PsDocument.
  5. Riempi il tracciato dell’ellisse.
  6. Chiudi la pagina.
  7. 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:

  1. Imposta il contorno sullo stato grafico corrente del PsDocument.
  2. 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

Add Ellipse

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.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.