在 PS 文件中使用填充图案 | Python

在 PS 文档中添加阴影图案

阴影图案通常由简单的黑白图像和各种阴影图案组成,用作纹理平铺图案。在通过 .NET 运行的 Aspose.Page for Python 中,这些图案由 aspose.pydrawing.drawing2d.HatchBrush 类表示,该类派生自 aspose.pydrawing.Brush。与 aspose.pydrawing.TextureBrush** 不同,HatchBrush 提供指定的预定义样式,用于定义用于平铺的图像。Aspose.Page for Python 提供了 53 种阴影样式,所有这些样式都可以在 PsDocument 中用于填充或描边。

要使用阴影图案绘制图形对象,请创建一个具有所需阴影样式的 HatchBrush,并将其传递给 set_paint() 或接受 aspose.pydrawing.Brushfill_text()fill_and_stroke_text() 方法之一。

要使用填充图案勾勒图形对象的轮廓,请在 PsDocument 中将填充图案设置为当前绘制对象,创建一个新的 aspose.pydrawing.Pen 对象,并将其传递给 set_stroke() 函数或接受 aspose.pydrawing.Pen 参数的 outline_text()fill_and_stroke_text() 方法之一。

以下示例演示了如何使用填充图案填充形状、在 Python 中展示各种填充样式以及如何使用填充图案填充和勾勒文本轮廓。

以下是在新的 PS 文档中使用填充图案绘制图形对象的算法:

  1. 为生成的 PS 文件创建输出流。
  2. 启动 PsSaveOptions
  3. 使用已创建的输出流和保存选项创建 PsDocument
  4. 根据我们要填充或勾勒轮廓的对象,创建形状或字体。
  5. 创建具有所需样式的 aspose.pydrawing.drawing2d.HatchBrush 对象。
  6. 将阴影绘制设置为 PsDocument 中的当前绘制。
  7. 使用当前绘制填充形状或填充文本。如果我们使用接受 aspose.pydrawing.Brush 作为参数的文本填充方法,则可以跳过上一步。
  8. 关闭页面。
  9. 保存文档。

如果我们需要使用填充图案来*描边(勾勒)*图形对象,而不是执行最后四个步骤,则需要执行以下步骤:

  1. 将填充图案设置为 PsDocument 中的当前描边。
  2. 创建 aspose.pydrawing.Pen 对象。
  3. 将这个描边设置为 PsDocument 中的当前描边。
  4. 使用当前描边勾勒形状,然后描边或勾勒文本。如果我们使用其中一种接受 aspose.pydrawing.Pen 作为参数的文本描边方法,则可以忽略上一步。
  5. 关闭页面。
  6. 保存文档。
 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()

请参阅如何在 .NETJava 中使用 PS 文档中的填充图案。

运行此代码的结果是:

添加填充图案

您可以从 GitHub 下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.