Travailler avec des formes dans PostScript | C++

Ajouter des formes dans un document PS

Ajouter un rectangle à PS

Afin d’ajouter un rectangle à PsDocument avec la bibliothèque Aspose.Page pour C++, nous devons suivre les étapes suivantes :

  1. Créez un flux de sortie pour le fichier PS résultant.
  2. Créez un objet PsSaveOptions avec les options par défaut.
  3. Créez un PsDocument d’une page avec un flux de sortie déjà créé et enregistrez les options.
  4. Créez un rectangle System.Drawing.Drawing2D.GraphicsPath à partir du rectangle.
  5. Définissez une peinture sur l’état graphique actuel de PsDocument.
  6. Remplissez le chemin du rectangle.
  7. Fermez la page.
  8. Enregistrez le document.

Si nous devons tracer (contourner) un rectangle, les 4 premières et les 2 dernières étapes seront les mêmes, mais les points 5 et 6 seront :

  1. Définissez le trait sur l’état graphique actuel de PsDocument.

  2. Tracez (décrivez) le chemin du rectangle.

 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    }

Voir Travailler avec des formes dans des documents PS dans .NET ou Java.


Le résultat de l’exécution de ce code apparaît comme

Ajouter un rectangle

Ajouter Ellipse à PS

Afin d’ajouter une ellipse à PsDocument, 8 étapes sont également requises :

  1. Créez un flux de sortie pour le fichier PS résultant.
  2. Créez un objet PsSaveOptions avec les options par défaut.
  3. Créez un PsDocument d’une page avec un flux de sortie déjà créé et enregistrez les options.
  4. Créez une ellipse System.Drawing.Drawing2D.GraphicsPath à partir du rectangle.
  5. Définissez Paint sur l’état graphique actuel de PsDocument.
  6. Remplissez le chemin de l’ellipse.
  7. Fermez la page.
  8. Enregistrez le document.

Si nous devons tracer (contourer) une ellipse, les 4 premières et les 2 dernières étapes seront les mêmes mais les points 5 et 6 seront :

  1. Définissez le trait sur l’état graphique actuel de PsDocument.

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

Le résultat de l’exécution de ce code apparaît comme

Ajouter une ellipse

Comme nous pouvons le voir, toute forme, fermée ou non, qui peut être placée dans System.Drawing.Drawing2D.GraphicsPath peut être remplie ou dessinée par PsDocument. Il peut également être coupé, mais cela sera décrit dans un autre article.

Vous pouvez télécharger des exemples et des fichiers de données à partir de GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.