Working with Text in PS file | Java

Contents
[ Hide Show ]

Add Text in PS Document

In this article, we consider the ways how a text can be added to a PS document.
Obviously, the text should be written in a document with some font. Fonts can be stored in system folders, and in this case, we call them system fonts and can be stored in custom folders, which are folders in which someone puts fonts for particular usage. Usually, these fonts are absent in system folders. We call such fonts custom fonts. Aspose.Page for Java library offers methods for using both system and custom fonts.

For using system fonts we just fill or outline a text with native Java’s java.awt.Font. Numbers in the method’s call are the x and y coordinates of the top-left corner of the text.

1document.fillText(str, new java.awt.Font("Times New Roman", java.awt.Font.BOLD, fontSize), 50, 100);

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

1options.setAdditionalFontsFolders(new String[] { FONTS_FOLDER });
2com.aspose.page.font.DrFont drFont = ExternalFontCache.fetchDrFont("Palatino Linotype", fontSize, Font.PLAIN);
3document.fillText(str, drFont, 50, 100);

Another option is working with text that also belongs to a font. The font that is used to print text in the PostScript document can be embedded in this file or not embedded. In the first case, the text will always be rendered in any PostScript viewer or editor because it always lies with the text. In the second case, we should expect that the used font exists in system folders on the target host. If the used font doesn’t exist PostScript interpreter can throw an error.
The third option is also about a font because a font is the main thing in adding text. A font used for filling or drawing (or may be clipping) a text can be embedded in a PS file in various forms. Now TrueType and Type3 font types in embedding are supported.
The fourth option is the possibility to write the glyphs of the text with assigned advances (widths). It allows adding some artistry to the text.

In the example below we demonstrate the usage of various methods of adding text in a PS document with Aspose.Page for Java library.

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

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions. If we use a custom font we add custom fonts folder(s) in the save options.
  3. Create PsDocument with the already created output stream and save options.
  4. Create necessary font, system or custom.
  5. Fill or outline the text with the created font. Here we can assign java.awt.Paint or java.awt.Stroke 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 java.awt.Paint or java.awt.Stroke, the text will be filled or outlined with current paint/stroke in the current graphics state.
  6. Close the page.
  7. Save the document.

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

In this piece of code, we create an output stream 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 fontSize variable that is also used in every used method.

 1//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddText_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5// Set custom fonts folder. It will be added to system fonts folders for finding needed fonts.
 6options.setAdditionalFontsFolders(new String[] { FONTS_FOLDER });
 7//A text to write to the PS file
 8String str = "ABCDEFGHIJKLMNO";
 9int fontSize = 48;
10
11// Create a new 1-paged PS Document
12PsDocument document = new PsDocument(outPsStream, options, false);

Here we demonstrate 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 system font (located in system fonts folders) for filling text //////////////////
2Font font = new Font("Times New Roman", Font.BOLD, fontSize);
3//Fill text with default or already defined color. In the given case it is black.
4document.fillText(str, font, 50, 100);
5//Fill text with Blue color.
6document.fillText(str, font, 50, 150, Color.BLUE);
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image1 Add Text image2

Now we fill the text with the custom font.

1//////////////////////////////////////Using custom font (located in custom fonts folders) for filling text /////////////////
2DrFont drFont = ExternalFontCache.fetchDrFont("Palatino Linotype", fontSize, Font.PLAIN);
3//Fill text with default or already defined color. In the given case it is black.
4document.fillText(str, drFont, 50, 200);
5//Fill text with Blue color.
6document.fillText(str, drFont, 50, 250, Color.BLUE);
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image3 Add Text image4

Here we demonstrate the usage of system font for outlining the text with the current stroke of graphics state (that is black) and with the new Stroke.

 1//Create a stroke with a width of 2 points.
 2Stroke stroke = new BasicStroke(2);
 3//Create stroke's blue-violet color
 4Color strokeColor = new Color(138, 43, 226);
 5
 6//////////////////////////////////////Using system font (located in system fonts folders) for outlining text ////////////////
 7//Outline text with default or already defined pen. In the given case it is a black colored 1-point width pen.
 8document.outlineText(str, font, 50, 300);
 9
10//Outline text with blue-violet colored 2-point width pen.
11document.outlineText(str, font, 50, 350, strokeColor, stroke);
12/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image5 Add Text image6

Now we outline the text with the custom font.

1//////////////////////////////////////Using custom font (located in custom fonts folders) for outlining text /////////////////
2//Outline text with default or already defined pen. In the given case it is a black colored 1-point width pen.
3document.outlineText(str, drFont, 50, 450);
4//Outline text with blue-violet colored 2-point width pen.
5document.outlineText(str, drFont, 50, 500, strokeColor, stroke);
6///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image8 Add Text image9

Here we demonstrate the usage of the system font for filling and outlining the text with new SolidBrush and Pen.

1//Fill text with orange color and stroke with blue-violet 2-point width pen.
2document.fillAndStrokeText(str, font, 50, 400, Color.YELLOW, strokeColor, stroke);

The result

Add Text image7

Now we fill and outline the text with the custom font.

1//Fill the text with orange color and stroke with blue 2-point width pen.
2document.fillAndStrokeText(str, drFont, 50, 550, Color.ORANGE, Color.BLUE, stroke);

The result

Add Text image10

Finally, we fill the text by assigning glyphs widths. The number of widths must be equal to the number of glyphs.

1    //Glyphs widths
2    float[] widths = new float[] { 87, 87, 87, 87, 34, 87, 87 };    
3    //Fill ASCII text using assigning glyphs widths.
4    document.fillText("BAMBOOK", widths, drFont, 50, 600, Color.BLUE);
5    
6    //Glyphs widths
7    widths = new float[] { 87, 34, 87, 87, 87, 87, 87 };
8    //Fill Unicode text using assigning glyphs widths.
9    document.fillText("ЗООПАРК", widths, drFont, 50, 650, Color.ORANGE);

The result

Add Text image11

Close the current page, save the document.

1//Close the current page
2document.closePage();
3
4//Save the document
5document.save();

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


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 the PsDocument class. If you need to change this behavior, please, use PsSaveOptions like the following:

1//Do not embed used fonts.
2options.setEmbedFonts(false);
1//Embed used fonts as Type3 fonts.
2options.setEmbedFontsAs(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.