在 Java 中处理 PostScript (PS) 文档中的文本

使用 Aspose.Page for Java 在 PostScript (PS/EPS) 文档中添加、格式化和设置文本元素的样式。通过编程实现高精度文本渲染,并完全控制字体选择(包括系统字体、TrueType 和 OpenType 字体)、文本填充和描边效果、仿射变换(缩放、旋转、倾斜)、自定义字符间距以及矢量文本裁剪。

 

Aspose.Page for Java 在 PsDocument 类中提供灵活的文本渲染方法,使开发人员能够输出标准填充文本或沿矢量路径绘制轮廓文本。

  • 要渲染带有纯色或渐变填充的标准可读文本段落、标题和标签字符串,请使用 PsDocument.fillText() 方法。
  • 要创建装饰性矢量文本轮廓、空心标题或风格化的排版效果,请使用 PsDocument.outlineText() 方法。
  • 要加载外部 .ttf.otf 字体文件,以确保在不同环境中保持精确的品牌视觉效果,请使用 DrFont/System Fonts。
  • 要相对于文档页面坐标对文本元素添加角度、旋转或倾斜,请使用 PsDocument.transform() 方法。

向 PostScript 页面添加标准填充文本

使用标准系统字体和彩色填充在 PostScript 页面上渲染简单字符串。

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.");
    }
}

创建文本轮廓并描边文本路径

绘制没有内部填充的矢量文本轮廓,以创建醒目的标题或排版设计元素。

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.");
    }
}

使用自定义 TrueType/OpenType 字体 (`.ttf`/`.otf`)

通过声明字体文件路径或将自定义字体文件夹嵌入 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.");
    }
}

安装和设置

将 Aspose.Page for Java 添加到 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. 如何在 PsDocument 中计算文本位置?

文本坐标点 (X, Y) 表示 PostScript 页面框架内文本字符串基线的起始位置(以 PostScript 点为单位,其中 72 点 = 1 英寸)。

2. 我可以使用渐变填充而不是纯色来渲染文本吗?

可以。在调用 document.fillText() 之前,将渐变绘制对象(例如 java.awt.LinearGradientPaintjava.awt.TexturePaint)传递给 document.setPaint(),即可为文本应用复杂的填充样式。

3. 如果请求的字体在主机服务器上不存在,会发生什么情况?

如果指定的系统字体不存在,并且未通过 PsSaveOptions.setAdditionalFontsFolders() 提供自定义字体路径,Aspose.Page 将回退到标准默认 PostScript 字体(例如 Courier 或 Helvetica),以确保文档生成能够顺利完成而不会中断。