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:
- 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 aspose.pydrawing.GraphicsPath dal rettangolo.
- Impostare un colore sullo stato grafico corrente di PsDocument.
- Riempire il rettangolo. 7. Chiudere la pagina.
- 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:
- Impostare il contorno allo stato grafico corrente del documento PsDocument.
- 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()
Il risultato dell’esecuzione di questo codice è
Aggiungi ellisse a PS
Per aggiungere un’ellisse a PsDocument sono necessari 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 aspose.pydrawing.drawing2d.GraphicsPath dal rettangolo.
- Imposta il colore sullo stato grafico corrente di PsDocument.
- Riempi il percorso dell’ellisse.
- Chiudi la pagina.
- 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:
- 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 è
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.