在 PS 文件中处理文本 | Python
在 PS 文档中添加文本
本文探讨了在 PS 文档中添加文本的方法。
文本需要使用指定的字体在文档中呈现。字体可以从系统文件夹(称为系统字体)或自定义文件夹中获取,自定义文件夹用于存放特定用途的字体。通常,自定义字体不存在于系统文件夹中。Aspose.Page for Python via .NET 库提供了使用系统和自定义字体的方法。
我们使用 aspose.pydrawing 来填充或勾勒文本轮廓,从而利用系统 fonts.Font 类。方法调用中的数字表示文本左上角的 x 和 y 坐标
1font = aspose.page.ExternalFontCache.create_font_by_family_name("Times New Roman", font_size, aspose.pydrawing.FontStyle.BOLD)
2document.fillText(str, font, 50, 100);
要使用自定义字体,我们首先需要在 PsSaveOptions 中添加 custom fonts 文件夹,然后获取 aspose.page.font.DrFont。 最后使用此 DrFont 对象填充或勾勒文本轮廓。
1options.setAdditionalFontsFolders(new String[] { FONTS_FOLDER });
2dr_font = aspose.page.ExternalFontCache.fetch_dr_font("Palatino Linotype", font_size, aspose.pydrawing.FontStyle.REGULAR)
3document.fillText(str, dr_font, 50, 100);
处理文本的另一种方法是字体嵌入。用于在 PostScript 文档中显示文本的字体可以嵌入到文件中,也可以不嵌入。嵌入后,字体会随文档一起移动,确保在 PostScript 查看器或编辑器之间实现一致的渲染。但是,如果未嵌入字体,PostScript 解释器将依赖于目标主机系统文件夹中是否存在该字体,如果字体缺失,可能会导致错误。
此外,用于填充、绘制或剪切文本的字体可以以不同的形式嵌入到 PS 文件中。目前,嵌入支持 TrueType 和 Type3 字体类型。
在下面的示例中,我们展示了使用 Aspose.Page for Python via .NET 库向 PS 文档添加文本的各种方法。
向新 PS 文档添加文本的算法包括以下步骤:
向新 PS 文档添加文本的算法包括以下步骤:
- 为生成的 PS 文件创建输出流。
- 创建 PsSaveOptions。如果我们使用自定义字体,则需要在保存选项中添加自定义字体文件夹。
- 使用已创建的输出流和保存选项创建 PsDocument。
- 创建必要的字体,系统 或 自定义。
- 使用创建的字体填充或勾勒文本轮廓。我们可以根据填充还是绘制文本来指定 aspose.pydrawing.Brush 或 aspose.pydrawing.Pen。或者,我们可以在一个方法中同时填充和勾勒文本轮廓。如果我们使用的方法没有使用 aspose.pydrawing.Brush 或 aspose.pydrawing.Pen,文本将使用当前图形状态下的当前画笔/描边进行填充或勾勒轮廓。
- 关闭页面。
- 保存文档。
我们将示例代码分成几个小的代码片段,以便区分 PS 文档的初始准备、每个添加文本方法的使用以及文档的完成。
在这里,我们创建了一个输出流和 PsSaveOptions,添加了一个自定义字体文件夹以便在代码中使用自定义字体,创建了一个 PsDocument 对象,将所有方法的通用文本设置为字符串变量,并创建了一个 fontSize 变量,该变量也在每个使用的方法中使用。
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_text()
3
4fonts_folder = Util.get_data_dir_data() + """necessary_fonts/"""
5
6# Create an output stream for the PostScript document
7out_ps_stream = open(data_dir + "AddText_outPS.ps", "wb")
8# Create the save options with A4 size
9options = PsSaveOptions()
10# Set the custom fonts folder. It will be added to system fonts folders for finding the specific font.
11options.additional_fonts_folders = [ fonts_folder ]
12# A text to write to the PS file
13str = "ABCDEFGHIJKLMNO"
14font_size: float = 48
15
16# Create new 1-paged PS Document
17document = PsDocument(out_ps_stream, options, False)
在这里您可以看到使用系统字体用图形状态的当前颜色(即黑色)和新的SolidBrush填充文本。
1##################################### Using a sysem font (located in system fonts folders) for filling text ##################
2font = aspose.page.ExternalFontCache.create_font_by_family_name("Times New Roman", font_size, aspose.pydrawing.FontStyle.BOLD)
3# Fill the text with a default or already defined color. In the given case it is black.
4document.fill_text(str, font, 50, 100)
5# Fill the text with the Blue color.
6document.fill_text(str, font, 50, 150, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
7############################################################################################################################
运行此代码的结果是:
现在我们用自定义字体填充文本。
1##################################### Using a custom font (located in custom fonts folders) for filling text ##################
2dr_font = aspose.page.ExternalFontCache.fetch_dr_font("Palatino Linotype", font_size, aspose.pydrawing.FontStyle.REGULAR)
3# Fill the text with the default or already defined color. In the given case it is black.
4document.fill_text(str, dr_font, 50, 200)
5# Fill text with the Blue color.
6document.fill_text(str, dr_font, 50, 250, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
7############################################################################################################################
运行此代码的结果是:
在这里,您可以看到使用系统字体,通过图形状态的当前笔触(即黑色)和新的画笔来勾勒文本轮廓。
1##################################### Using sysem font (located in system fonts folders) for outlining text ##################
2# Outline the text with the default or already defined aspose.pydrawing.Pen. In the given case it is black colored 1-points width aspose.pydrawing.Pen.
3document.outline_text(str, font, 50, 300)
4# Outline the text the with blue-violet colored 2-points width aspose.pydrawing.Pen.
5pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2)
6document.outline_text(str, font, 50, 350, pen)
7############################################################################################################################
运行此代码的结果是:
现在我们用自定义字体勾勒出文本轮廓。
1##################################### Using a custom font (located in custom fonts folders) for outlining text /////////////////
2# Outline the text with the default or already defined aspose.pydrawing.Pen. In the given case it is a black colored 1-points width aspose.pydrawing.Pen.
3document.outline_text(str, dr_font, 50, 450)
4# Outline the text with the blue-violet colored 2-points width aspose.pydrawing.Pen.
5pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2)
6document.outline_text(str, dr_font, 50, 500, pen)
7##############################################################################################################################
运行此代码的结果是:
在这里,您可以观察到如何使用系统字体通过新的SolidBrush和Pen来填充和勾勒文本轮廓。
1# Fill the text with an orange color and stroke with a blue colored 2-points width aspose.pydrawing.Pen.
2pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2)
3document.fill_and_stroke_text(str, font, 50, 400, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.yellow), pen)
运行此代码的结果是:
最后,我们用自定义字体填充文本并勾勒出轮廓。
1# Fill the text with the orange color and stroke with the blue colored 2-points width aspose.pydrawing.Pen.
2pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue), 2)
3document.fill_and_stroke_text(str, dr_font, 50, 550, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange), pen)
运行此代码的结果是:
关闭当前页面并保存文档。
1#Close current page
2document.close_page()
3
4#Save the document
5document.save()
在上面的示例中,使用的字体是 PostScript 文件中嵌入的 TrueType 字体,因为这是 PsDocument 类中保存字体的默认行为。如果您需要更改此行为,请按以下方式使用 PsSaveOptions:
1# Do not embed used fonts.
2options.embed_fonts = false;
1# Embed used fonts as Type3 fonts.
2options.embed_fonts_ss(FontConstants.EMBED_FONTS_TYPE3);
您可以从 GitHub下载示例和数据文件。