การทำงานกับข้อความในเอกสาร PostScript (PS) ด้วย Java

เพิ่ม จัดรูปแบบ และกำหนดสไตล์ให้กับองค์ประกอบข้อความในเอกสาร PostScript (PS/EPS) โดยใช้ Aspose.Page for Java เรนเดอร์ข้อความความแม่นยำสูงด้วยโปรแกรม พร้อมควบคุมการเลือกฟอนต์ได้อย่างเต็มที่ (รวมถึงฟอนต์ระบบ 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. ฉันสามารถเรนเดอร์ข้อความด้วยการเติมสีแบบไล่ระดับแทนสีทึบได้หรือไม่?

ได้ ส่งออบเจ็กต์การระบายสีแบบไล่ระดับ เช่น java.awt.LinearGradientPaint หรือ java.awt.TexturePaint ไปยัง document.setPaint() ก่อนเรียกใช้ document.fillText() เพื่อใช้สไตล์การเติมสีที่ซับซ้อนกับข้อความ

3. จะเกิดอะไรขึ้นหากไม่มีฟอนต์ที่ร้องขอบนเซิร์ฟเวอร์โฮสต์?

หากไม่มีฟอนต์ระบบที่ระบุและไม่ได้ระบุเส้นทางฟอนต์แบบกำหนดเองผ่าน PsSaveOptions.setAdditionalFontsFolders() Aspose.Page จะใช้แบบอักษร PostScript มาตรฐานเริ่มต้น เช่น Courier หรือ Helvetica เพื่อให้การสร้างเอกสารเสร็จสมบูรณ์โดยไม่หยุดชะงัก