Lavorare con le forme in un file PS | Python

Aggiungi forme al documento PS

Aggiungi rettangolo a PS

Per inserire un rettangolo in PsDocument utilizzando Aspose.Page per Python tramite la libreria .NET, procedere come segue:

  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 aspose.pydrawing.GraphicsPath dal rettangolo.
  5. Impostare un colore sullo stato grafico corrente di PsDocument.
  6. Riempire il rettangolo. 7. Chiudere la pagina.
  7. Salvare il documento.

Per tracciare il contorno di un rettangolo, i primi 4 e gli ultimi 2 passaggi saranno gli stessi, ma il 5° e il 6° saranno:

  1. Impostare il contorno allo stato grafico corrente del documento PsDocument.
  2. Tracciare il contorno del rettangolo.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "AddRectangle_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first rectangle
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_rectangle(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the rectangle
18    document.fill(path)
19    
20    # Create a graphics path from the second rectangle
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_rectangle(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the rectangle
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

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

Il risultato dell’esecuzione di questo codice è

Aggiungi rettangolo

Aggiungi ellisse a PS

Per aggiungere un’ellisse a PsDocument sono necessari 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 aspose.pydrawing.drawing2d.GraphicsPath dal rettangolo.
  4. Imposta il colore sullo stato grafico corrente di PsDocument.
  5. Riempi il percorso dell’ellisse.
  6. Chiudi la pagina.
  7. Salva il documento.

Per tracciare (contornare) un’ellisse, i primi 4 e gli ultimi 2 passaggi saranno gli stessi, mentre il quinto e il sesto saranno:

  1. Imposta il tratto sullo stato grafico corrente di PsDocument. 6. Traccia (delinea) l’ellisse.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for PostScript document
 5with open(data_dir + "AddEllipse_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first ellipse
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_ellipse(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the ellipse
18    document.fill(path)
19    
20    # Create a graphics path from the second ellipse
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_ellipse(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set the stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the ellipse
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

Il risultato dell’esecuzione di questo codice è

Add Ellipse

Come possiamo vedere, qualsiasi forma, sia chiusa che aperta, può essere riempita o disegnata da PsDocument. Può anche essere ritagliata, ma questo argomento 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.