Working with text in PostScript (PS) documents in Java

Add, format, and style text elements in PostScript (PS/EPS) documents using Aspose.Page for Java. Render high-precision text programmatically with full control over font selection (including system, TrueType, and OpenType fonts), text fill and stroke effects, affine transformations (scaling, rotation, shearing), custom character spacing, and vector text clipping.

 

Aspose.Page for Java provides flexible text rendering methods within the PsDocument class, enabling developers to output standard filled text or outline text along vector paths.

  • To render standard readable text paragraphs, titles, and label strings with solid or gradient fills use PsDocument.fillText() method.
  • To create decorative vector text outlines, hollow titles, or stylized typographic effects use PsDocument.outlineText() method.
  • To load external .ttf or .otf font files to maintain exact visual branding across environments use DrFont/System Fonts.
  • To add angles, rotates, or slants text elements relative to document page coordinates use PsDocument.transform() method.

Adding standard filled Text to a PostScript page

Render simple strings on a PostScript page using standard system fonts and color paints.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;

public class AddTextToPS {
    public static void main(String[] args) throws Exception {
        // Initialize output stream and PS document
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/simple_text.ps");
        PsSaveOptions options = new PsSaveOptions();
        PsDocument document = new PsDocument(outStream, options, 1);

        document.openPage(null);

        // Define font and color paint
        Font font = new Font("Times New Roman", Font.BOLD, 20);
        document.setPaint(Color.BLUE);

        // Fill text at specified X, Y coordinates
        document.fillText("Aspose.Page for Java - Text Rendering", font, 100, 150);

        // Close page and save document
        document.closePage();
        document.save();
        outStream.close();

        System.out.println("Text successfully added to PostScript document.");
    }
}

Outlining and stroking text paths

Draw vector text outlines without interior fills to build high-impact headers or typographic design elements.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;

public class StrokeTextInPS {
    public static void main(String[] args) throws Exception {
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/stroked_text.ps");
        PsSaveOptions options = new PsSaveOptions();
        PsDocument document = new PsDocument(outStream, options, 1);

        document.openPage(null);

        // Configure font, stroke width, and outline paint
        Font font = new Font("Arial", Font.BOLD, 36);
        document.setStroke(new BasicStroke(2.0f));
        document.setPaint(Color.RED);

        // Outline text string
        document.outlineText("Outlined Vector Text", font, 100, 200);

        document.closePage();
        document.save();
        outStream.close();

        System.out.println("Outlined text rendered successfully.");
    }
}

Using custom TrueType/OpenType fonts (`.ttf`/`.otf`)

Incorporate non-system brand fonts by declaring font file paths or embedding custom font folders into PsSaveOptions.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;

public class CustomFontTextPS {
    public static void main(String[] args) throws Exception {
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/custom_font_text.ps");

        // Add custom font folder search path in options
        PsSaveOptions options = new PsSaveOptions();
        options.setAdditionalFontsFolders(new String[] { "C:/PSProject/Fonts/" });

        PsDocument document = new PsDocument(outStream, options, 1);

        document.openPage(null);

        // Reference custom font by its registered family name
        Font customFont = new Font("CustomBrandFont", Font.PLAIN, 24);
        document.setPaint(Color.DARK_GRAY);

        document.fillText("Text rendered using external TrueType font", customFont, 100, 250);

        document.closePage();
        document.save();
        outStream.close();

        System.out.println("Custom font text rendered successfully.");
    }
}

Installation and Setup

Add Aspose.Page for Java to your Maven:

Package Manager Console Command

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-page</artifactId>
    <version>Latest</version>
</dependency>

FAQ

1. How are text positions calculated in PsDocument?

Text coordinate points (X, Y) represent the baseline start of the text string within the PostScript page frame (measured in PostScript points, where 72 points = 1 inch).

2. Can I render text with gradient fills instead of solid colors?

Yes. Pass a gradient paint object (such as java.awt.LinearGradientPaint or java.awt.TexturePaint) to document.setPaint() before calling document.fillText() to apply complex fill styles to text.

3. What happens if a requested font is missing on the host server?

If a specified system font is absent and no custom font path is provided via PsSaveOptions.setAdditionalFontsFolders(), Aspose.Page falls back to standard default PostScript typefaces (such as Courier or Helvetica) to ensure document generation completes without breaking.