Working with Text in PS file | Python

Contents
[ Hide Show ]

Add Text in PS Document

This article explores the methods of adding text to a PS document.
Text needs to be rendered in a document with a specified font. Fonts can be sourced from system folders, known as system fonts, or from custom folders, where fonts are placed for specific usage. Typically, custom fonts are not present in system folders. The Aspose.Page for Python via .NET library provides methods for utilizing system and custom fonts.

We fill or outline text using the aspose.pydrawing to utilize system fonts.Font class. The numbers in the method call represent the x and y coordinates of the top-left corner of the text.

1font = aspose.page.ExternalFontCache.create_font_by_family_name("Times New Roman", font_size, aspose.pydrawing.FontStyle.BOLD)
2document.fillText(str, font, 50, 100);

For using custom fonts we first need to add custom fonts folder in PsSaveOptions, then fetch aspose.page.font.DrFont. And finally fill or outline the text with this DrFont object.

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);

Another way to work with text involves font embedding. The font used for displaying text in a PostScript document can either be embedded within the file or not. When embedded, the font travels with the document, ensuring consistent rendering across PostScript viewers or editors. However, if the font is not embedded, the PostScript interpreter will rely on the font’s existence in the system folders of the target host, potentially resulting in errors if the font is missing.

Furthermore, fonts used for filling, drawing, or clipping text can be embedded in a PS file in different forms. As of now, embedding supports TrueType and Type3 font types.

In the example below, we showcase various methods for adding text to a PS document using the Aspose.Page for Python via .NET library.

The algorithm for adding text to a new PS document involves the following steps:

An algorithm for adding text to a new PS document includes the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create the PsSaveOptions. If we use a custom font we need to add a custom fonts folder(s) in the save options.
  3. Create a PsDocument with the already created output stream and save options.
  4. Create a necessary font, system or custom.
  5. Fill or outline the text with the created font. Here we can assign aspose.pydrawing.Brush or aspose.pydrawing.Pen depending on whether we fill or draw the text. Or we can fill and outline the text in one method. If we use a method without aspose.pydrawing.Brush or aspose.pydrawing.Pen, the text will be filled or outlined with the current paint/stroke in the current graphics state.
  6. Close the page.
  7. Save the document.

We divided the example code into little code snippets in order to separate the initial preparation for the PS document, the usage of every method for adding text, and the completion of the document.

Here, we create an outputstream and PsSaveOptions, add a custom fonts folder for using the custom font in the code, create a PsDocument object, set common for all methods text as a string variable, and create a fontSize variable that is also used in every used method.

 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)

Here you can see the usage of system font for filling the text with the current color of the graphics state (that is black) and with the new 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############################################################################################################################

The result of running this code is

Add Text image1 Add Text image2

Now we fill the text with the custom font.

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############################################################################################################################

The result of running this code is

Add Text image3 Add Text image4

Here you can see the usage of the system font for outlining the text with the current stroke of the graphics state (that is black) and with the new Pen.

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############################################################################################################################

The result of running this code is

Add Text image5 Add Text image6

Now we outline the text with the custom font.

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##############################################################################################################################

The result of running this code is

Add Text image8 Add Text image9

Here you can observe the usage of the system font for filling and outlining the text with a new SolidBrush and 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)

The result of running this code is

Add Text image7

And finally we fill and an outline the text with the custom font.

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)

The result of running this code is

Add Text image10

Close the current page and save the document.

1#Close current page
2document.close_page()
3
4#Save the document
5document.save()

See working with a text in PS documents in .NET, Java.

In the example above, there are used fonts embedded in the PostScript file as TrueType fonts, because it is the default behavior of saving fonts in PsDocument class. If you need to change this behavior, please, use PsSaveOptions in the next way:

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);

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.