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 は Courier や Helvetica などの標準の既定 PostScript 書体にフォールバックし、処理を中断することなくドキュメント生成を完了できるようにします。