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.LinearGradientPaint 또는 java.awt.TexturePaint와 같은 그라데이션 페인트 객체를 document.setPaint()에 전달하여 텍스트에 복잡한 채우기 스타일을 적용할 수 있습니다.
3. 요청한 글꼴이 호스트 서버에 없는 경우 어떻게 되나요?
지정된 시스템 글꼴이 없고 PsSaveOptions.setAdditionalFontsFolders()를 통해 사용자 지정 글꼴 경로가 제공되지 않은 경우, Aspose.Page는 Courier 또는 Helvetica와 같은 표준 기본 PostScript 서체로 대체하여 중단 없이 문서 생성이 완료되도록 합니다.