โหลดฟอนต์จากไฟล์ สตรีม และอาร์เรย์ไบต์ใน Java
โหลดและเริ่มต้นใช้งานฟอนต์ TrueType (TTF), OpenType (OTF), WOFF/WOFF2, Embedded OpenType (EOT), Type1 และ Compact Font Format (CFF) ด้วยโปรแกรมโดยใช้ Aspose.Font for Java โหลดฟอนต์ได้อย่างราบรื่นจากพาธบนดิสก์ ไฟล์ระบบ สตรีมไบต์ หรืออาร์เรย์ในหน่วยความจำ โดยไม่ต้องพึ่งพา Java AWT แบบเนทีฟหรือการติดตั้งฟอนต์ที่ขึ้นอยู่กับระบบปฏิบัติการ
หากต้องการเปิดและทำงานกับฟอนต์ใด ๆ ใน Aspose.Font for Java จำเป็นต้องมีพารามิเตอร์หลักสองรายการ:
- รูปแบบฟอนต์ (FontType): ระบุหมวดหมู่ทั่วไป (เช่น FontType.TTF, FontType.Type1, FontType.CFF)
- แหล่งข้อมูลไบนารีของฟอนต์ (StreamSource/FontFileDefinition): ห่อหุ้มสตรีมไบนารีพื้นฐานไว้ ไม่ว่าแหล่งข้อมูลนั้นจะอยู่บนดิสก์ ในฐานข้อมูล หรือภายในสตรีมเอกสาร (เช่น PDF/DOCX)
- คลาสหลัก
FontDefinitionจะเชื่อมโยงพารามิเตอร์เหล่านี้เข้าด้วยกัน ทำให้Font.open(fontDefinition) สามารถส่งคืนออบเจ็กต์com.aspose.font.Fontแบบรวมเป็นหนึ่งเดียวได้
จะโหลดฟอนต์โดยใช้ java.io.File ได้อย่างไร?
วิธีที่ตรงไปตรงมาที่สุดในการโหลดฟอนต์ที่จัดเก็บไว้ในไดรฟ์ภายในเครื่องโดยใช้ FontFileDefinition(File file)
import com.aspose.font.*;
import java.io.File;
import java.nio.file.Paths;
public class LoadFontFromFile {
public static void main(String[] args) {
// Step 1: Define path to font file
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Step 2: Create FontFileDefinition from java.io.File
FontFileDefinition fileDef = new FontFileDefinition(new File(fontPath));
// Step 3: Create FontDefinition (extension is auto-detected from FontFileDefinition)
FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
// Step 4: Open and load font instance
Font font = Font.open(fontDef);
System.out.println("Successfully loaded font: " + font.getFontName());
}
}จะโหลดฟอนต์โดยใช้ FileSystemStreamSource ได้อย่างไร?
รองรับการจัดการนามสกุลไฟล์อย่างชัดเจนเพื่อให้ตรวจหารูปแบบได้อย่างแม่นยำเมื่อโหลดฟอนต์จากพาธบนดิสก์
import com.aspose.font.*;
import java.nio.file.Paths;
public class LoadFontFromStreamSource {
public static void main(String[] args) {
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Pass 'ttf' explicitly to ensure proper format mapping
FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath));
FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);
Font font = Font.open(fontDef);
System.out.println("Loaded Font Name: " + font.getFontName());
}
}จะโหลดฟอนต์จากแหล่งสตรีมโดยตรง (โดยไม่ใช้ FontFileDefinition) ได้อย่างไร?
ส่ง FileSystemStreamSource ไปยัง FontDefinition โดยตรงเพื่อให้การโหลดง่ายขึ้น
import com.aspose.font.*;
import java.nio.file.Paths;
public class LoadFontDirectStream {
public static void main(String[] args) {
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Provide FontType, file extension, and StreamSource directly
FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new FileSystemStreamSource(fontPath));
Font font = Font.open(fontDef);
System.out.println("Loaded Font Name: " + font.getFontName());
}
}จะโหลดฟอนต์จากอาร์เรย์ไบต์ในหน่วยความจำ (byte[]) ได้อย่างไร?
เหมาะอย่างยิ่งสำหรับการโหลดฟอนต์ที่ดึงมาจากฐานข้อมูล คำขอเครือข่าย พื้นที่จัดเก็บบนคลาวด์ หรือทรัพยากร PDF/DOCX ที่ฝังอยู่ โดยใช้ ByteContentStreamSource
import com.aspose.font.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class LoadFontFromByteArray {
public static void main(String[] args) throws Exception {
String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();
// Read font binary data into byte array
byte[] fontBytes = Files.readAllBytes(Paths.get(fontPath));
// Initialize FontDefinition with ByteContentStreamSource
FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new ByteContentStreamSource(fontBytes));
// Open font from memory
Font font = Font.open(fontDef);
System.out.println("Loaded Font from Memory: " + font.getFontName());
}
}การติดตั้งและการตั้งค่า
เพิ่ม Aspose.Font 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-font</artifactId>
<version>Latest</version>
</dependency>
FAQ
1. เหตุใดจึงต้องส่งทั้ง FontType และ fileExtension เมื่อโหลดฟอนต์?
FontType แสดงถึงกลุ่มรูปแบบโดยรวม (เช่น FontType.TTF) แต่มีรูปแบบไฟล์ที่แตกต่างกันหลายรูปแบบอยู่ในกลุ่มนี้ (เช่น .ttf, .eot และ .ttc) การระบุ fileExtension ช่วยให้ไลบรารีใช้ parser ที่ถูกต้องสำหรับรูปแบบคอนเทนเนอร์เฉพาะนั้น
2. สามารถโหลดฟอนต์โดยตรงจากฟิลด์ BLOB ของฐานข้อมูลได้หรือไม่?
ได้ ดึงข้อมูล BLOB เป็นอาร์เรย์ byte[] แล้วใช้ ByteContentStreamSource เพื่อเริ่มต้น FontDefinition โดยไม่ต้องเขียนข้อมูลลงดิสก์
3. จะโหลดฟอนต์ได้อย่างไรเมื่อข้อมูลไบนารีเริ่มต้นที่ออฟเซ็ตเฉพาะภายในไฟล์?
ใช้ FontFileDefinition(String fileExtension, StreamSource streamSource, long offset) constructor โดยส่งออฟเซ็ตเป็นไบต์ที่ข้อมูลฟอนต์เริ่มต้น