Làm việc với văn bản trong tài liệu PostScript (PS) bằng Java

Thêm, định dạng và tạo kiểu cho các phần tử văn bản trong tài liệu PostScript (PS/EPS) bằng Aspose.Page for Java. Kết xuất văn bản với độ chính xác cao bằng lập trình với toàn quyền kiểm soát việc lựa chọn phông chữ (bao gồm phông chữ hệ thống, TrueType và OpenType), hiệu ứng tô màu và tạo nét viền cho văn bản, các phép biến đổi affine (co giãn, xoay, nghiêng), khoảng cách ký tự tùy chỉnh và cắt văn bản theo đường dẫn vector.

 

Aspose.Page for Java cung cấp các phương thức kết xuất văn bản linh hoạt trong lớp PsDocument, cho phép các nhà phát triển xuất văn bản tiêu chuẩn có tô màu hoặc văn bản dạng đường viền dọc theo các đường dẫn vector.

  • Để kết xuất các đoạn văn bản, tiêu đề và chuỗi nhãn dễ đọc với màu tô đơn sắc hoặc chuyển màu, hãy sử dụng phương thức PsDocument.fillText().
  • Để tạo đường viền văn bản vector trang trí, tiêu đề rỗng hoặc các hiệu ứng kiểu chữ cách điệu, hãy sử dụng phương thức PsDocument.outlineText().
  • Để tải các tệp phông chữ bên ngoài .ttf hoặc .otf nhằm duy trì hình ảnh thương hiệu chính xác giữa các môi trường, hãy sử dụng DrFont/System Fonts.
  • Để thêm góc, xoay hoặc làm nghiêng các phần tử văn bản tương đối với tọa độ trang tài liệu, hãy sử dụng phương thức PsDocument.transform().

Thêm văn bản tiêu chuẩn có tô màu vào trang PostScript

Kết xuất các chuỗi đơn giản trên trang PostScript bằng phông chữ hệ thống tiêu chuẩn và màu tô.

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

Tạo đường viền và tạo nét cho đường dẫn văn bản

Vẽ đường viền văn bản vector không có phần tô bên trong để tạo các tiêu đề nổi bật hoặc các phần tử thiết kế kiểu chữ.

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

Sử dụng phông chữ TrueType/OpenType tùy chỉnh (`.ttf`/`.otf`)

Tích hợp các phông chữ thương hiệu không có trong hệ thống bằng cách khai báo đường dẫn tệp phông chữ hoặc nhúng các thư mục phông chữ tùy chỉnh vào 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.");
    }
}

Cài đặt và thiết lập

Thêm Aspose.Page for Java vào 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. Vị trí văn bản được tính toán như thế nào trong PsDocument?

Các điểm tọa độ văn bản (X, Y) biểu thị điểm bắt đầu của đường cơ sở của chuỗi văn bản trong khung trang PostScript (được đo bằng point PostScript, trong đó 72 point = 1 inch).

2. Tôi có thể kết xuất văn bản với vùng tô màu chuyển sắc thay vì màu đồng nhất không?

Có. Truyền một đối tượng tô màu chuyển sắc (chẳng hạn như java.awt.LinearGradientPaint hoặc java.awt.TexturePaint) vào document.setPaint() trước khi gọi document.fillText() để áp dụng các kiểu tô màu phức tạp cho văn bản.

3. Điều gì xảy ra nếu phông chữ được yêu cầu không có trên máy chủ?

Nếu phông chữ hệ thống được chỉ định không tồn tại và không cung cấp đường dẫn phông chữ tùy chỉnh thông qua PsSaveOptions.setAdditionalFontsFolders(), Aspose.Page sẽ chuyển sang sử dụng các kiểu chữ PostScript mặc định tiêu chuẩn (chẳng hạn như Courier hoặc Helvetica) để đảm bảo quá trình tạo tài liệu hoàn tất mà không bị gián đoạn.