Lavorare con i modelli di tratteggio in un file PS | Python

Aggiungi un pattern di tratteggio in un documento PS

I pattern di tratteggio, spesso composti da semplici immagini in bianco e nero con vari design di tratteggio, fungono da pattern di texture a mosaico. In Aspose.Page per Python tramite .NET, questi pattern sono rappresentati dalla classe aspose.pydrawing.drawing2d.HatchBrush, derivata da aspose.pydrawing.Brush. A differenza di aspose.pydrawing.TextureBrush, HatchBrush offre stili predefiniti con nome, che definiscono l’immagine utilizzata per il tiling. Aspose.Page per Python fornisce 53 stili di tratteggio, tutti utilizzabili per il riempimento o il contorno in PsDocument.

Per colorare oggetti grafici con un pattern di tratteggio, creare un HatchBrush con lo stile di tratteggio desiderato e passarlo a set_paint() o a uno dei metodi fill_text() o fill_and_stroke_text() che accettano aspose.pydrawing.Brush.

Per delineare oggetti grafici con un motivo di tratteggio, imposta il motivo di tratteggio come disegno corrente in PsDocument, crea un nuovo file aspose.pydrawing.Pen e passalo a set_stroke() o a uno dei metodi outline_text() o fill_and_stroke_text() che accettano aspose.pydrawing.Pen.

L’esempio seguente mostra come riempire una forma con un motivo di tratteggio, mostrando diversi stili di tratteggio in Python e riempiendo e delineando il testo con un motivo di tratteggio.

Ecco l’algoritmo per disegnare oggetti grafici con un motivo di tratteggio in un nuovo documento PS:

  1. Crea un flusso di output per il file PS risultante. 2. Avviare PsSaveOptions.
  2. Creare un PsDocument con il flusso di output già creato e le opzioni di salvataggio.
  3. Creare la forma o un font in base all’oggetto che si desidera riempire o delineare.
  4. Creare un oggetto di tipo aspose.pydrawing.drawing2d.HatchBrush con lo stile desiderato.
  5. Impostare il tratteggio come colore corrente nel PsDocument.
  6. Riempire la forma con il colore corrente o il testo. Se si utilizza uno dei metodi per riempire il testo che accetta aspose.pydrawing.Brush come parametro, è possibile saltare il passaggio precedente.
  7. Chiudere la pagina.
  8. Salvare il documento.

Se abbiamo bisogno di contornare (contornare) oggetti grafici con un pattern di tratteggio, gli ultimi 4 passaggi avranno il seguente aspetto:

  1. Impostare il tratteggio come corrente in PsDocument.
  2. Creare l’oggetto aspose.pydrawing.Pen.
  3. Impostare questo tratto come corrente in PsDocument.
  4. Contornare la forma con il colore corrente e tracciare o contornare il testo. Se utilizziamo uno dei metodi per contornare il testo che accetta aspose.pydrawing.Pen come parametro, il passaggio precedente può essere ignorato.
  5. Chiudere la pagina.
  6. Salvare il documento.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_hatches()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "AddHatchPattern_outPS.ps", "wb") as out_ps_stream:
 6    # Create the 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    x0 = 20.
13    y0 = 100.
14    square_side = 32.
15    width = 500.
16    sum_x = 0.
17    
18    # Restore a graphics state
19    document.write_graphics_save()
20    
21    # Translate to initial point
22    document.translate(x0, y0)
23    
24    # Create a rectngle path for every pattern square
25    path = aspose.pydrawing.drawing2d.GraphicsPath()
26    path.add_rectangle(aspose.pydrawing.RectangleF(0, 0, square_side, square_side))
27    
28    # Create a pen for outlining the pattern square
29    pen = GraphicsFactory.create_pen_by_color_and_width(aspose.pydrawing.Color.black, 2)
30    
31    # For every hatch pattern style 
32    hatch_style = 0
33    while hatch_style <= 52:
34        # Set the paint with the current hatch brush style
35        document.set_paint(aspose.pydrawing.drawing2d.HatchBrush(aspose.pydrawing.drawing2d.HatchStyle(hatch_style),
36            aspose.pydrawing.Color.black, aspose.pydrawing.Color.white))
37        
38        # Calculate the displacement not to go beyond the page bounds
39        x = square_side
40        y = 0
41        if sum_x >= width:
42            x = -(sum_x - square_side)
43            y += square_side
44        # Translate the current graphics state
45        document.translate(x, y)
46        # Fill the pattern square
47        document.fill(path)
48        # Set the stroke
49        document.set_stroke(pen)
50        # Draw the square outline
51        document.draw(path)
52        
53        # Calculate the distance from X0
54        if sum_x >= width:
55            sum_x = square_side
56        else:
57            sum_x += x
58        hatch_style += 1
59    
60    # Restore the graphics state
61    document.write_graphics_restore()
62    
63    # Fill the text with the hatch pattern
64    brush = aspose.pydrawing.drawing2d.HatchBrush(aspose.pydrawing.drawing2d.HatchStyle.DIAGONAL_CROSS,
65    aspose.pydrawing.Color.red, aspose.pydrawing.Color.yellow)
66    font = ExternalFontCache.fetch_dr_font("Arial", 96, aspose.pydrawing.FontStyle.BOLD)
67    document.fill_and_stroke_text("ABC", font, 200, 300, brush, pen)
68    
69    # Outline text with the hatch pattern
70    brush = aspose.pydrawing.drawing2d.HatchBrush(aspose.pydrawing.drawing2d.HatchStyle.PERCENT50,
71    aspose.pydrawing.Color.blue, aspose.pydrawing.Color.white)
72    document.outline_text("ABC", font, 200, 400, GraphicsFactory.create_pen_by_brush_and_width(brush, 5))
73    
74    
75    # Close the current page
76    document.close_page()
77    
78    # Save the document
79    document.save()

Vedi come lavorare con un pattern di tratteggio in un documento PS in .NET, Java.

Il risultato dell’esecuzione di questo codice è

Aggiungi pattern di tratteggio

È 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.