Java에서 PostScript(PS) 문서 작업

Aspose.Page for Java를 사용하여 PostScript(PS) 문서를 프로그래밍 방식으로 생성, 수정 및 관리하세요. 여러 페이지로 구성된 PS 문서를 처음부터 손쉽게 만들고, 페이지를 추가하고, 벡터 그래픽과 텍스트 요소를 삽입하고, 여러 PostScript 파일을 병합하고, Adobe Acrobat 또는 Distiller를 설치하지 않고도 문서 구조를 조정할 수 있습니다.

 

Aspose.Page for Java는 PsDocument 클래스를 중심으로 하는 고수준의 독립형 객체 모델을 제공하여 PostScript 문서 생성 및 페이지 조작을 제어할 수 있습니다.

새로운 단일 페이지 또는 여러 페이지의 PostScript 인쇄 스트림을 만들려면 PsDocument(outputStream, options)을 사용합니다.

  • PostScript 스트림 내에서 개별 페이지를 프로그래밍 방식으로 구성하려면 PsDocument.openPage()/closePage() 메서드를 사용합니다.
  • 벡터 그래픽, 도형, 텍스트 및 페이지를 디스크의 파일 또는 스트림에 저장하려면 PsDocument.save() 메서드를 사용합니다.
  • 페이지 여백, 사용자 지정 크기(A4, Letter) 및 인쇄 방향을 설정하려면 PsSaveOptions 클래스를 사용합니다.

Java에서 여러 페이지로 구성된 PostScript 문서 만들기

저장 옵션을 구성하고 문서 스트림 내에서 페이지를 명시적으로 열고 닫아 여러 페이지로 구성된 PS 문서를 처음부터 만듭니다.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.io.FileOutputStream;

public class CreateMultiPagePSDocument {
    public static void main(String[] args) throws Exception {
        // Initialize output stream for the new PS file
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/multipage_document.ps");

        // Set save options (Default page size A4, 72 DPI)
        PsSaveOptions options = new PsSaveOptions();

        // Create a multi-page PS document instance
        PsDocument document = new PsDocument(outStream, options, 2); // Explicitly specifying 2 pages

        // --- PAGE 1 ---
        document.openPage("Page 1");
        // Add content to Page 1 (e.g., text or graphics)
        document.closePage();

        // --- PAGE 2 ---
        document.openPage("Page 2");
        // Add content to Page 2
        document.closePage();

        // Save and flush the document
        document.save();
        outStream.close();

        System.out.println("Multi-page PostScript document created successfully.");
    }
}

기존 문서 빌더에 페이지 및 그래픽 추가

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 AddPageContentToPS {
    public static void main(String[] args) throws Exception {
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/styled_document.ps");
        PsSaveOptions options = new PsSaveOptions();

        PsDocument document = new PsDocument(outStream, options, 1);

        // Open page
        document.openPage(null);

        // Add text element
        Font font = new Font("Arial", Font.BOLD, 24);
        document.fillText("Hello World from Aspose.Page Java!", font, 100, 100, Color.BLACK);

        // Draw a vector rectangle
        document.setPaint(Color.RED);
        document.drawRect(100, 150, 300, 200);

        // Close page and save
        document.closePage();
        document.save();
        outStream.close();

        System.out.println("Styled PS document with vector content saved.");
    }
}

사용자 지정 페이지 크기 및 여백 구성

PsSaveOptions를 사용하여 정확한 페이지 크기(Letter, Legal 또는 사용자 지정 크기 등)를 정의합니다.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.io.FileOutputStream;

public class CustomPageSizePS {
    public static void main(String[] args) throws Exception {
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/custom_size.ps");

        // Create options and set custom size (e.g., Letter dimensions in points)
        PsSaveOptions options = new PsSaveOptions();
        options.setPageSize(new java.awt.Dimension(612, 792)); // 8.5 x 11 inches in points

        PsDocument document = new PsDocument(outStream, options, 1);

        document.openPage("Main Page");
        // Page drawing commands...
        document.closePage();

        document.save();
        outStream.close();

        System.out.println("PS document with custom page size saved.");
    }
}

설치 및 설정

Maven에 Aspose.Page for Java를 추가합니다:

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. Adobe Acrobat 또는 PostScript 드라이버 유틸리티를 설치해야 하나요?

아니요. Aspose.Page for Java는 외부 프린터 드라이버나 네이티브 소프트웨어에 의존하지 않고 PS/EPS 문서 구조를 직접 구성, 렌더링 및 조작하는 네이티브 독립형 Java 라이브러리입니다.

2. 생성된 PostScript (PS) 문서를 PDF 또는 래스터 이미지로 직접 변환할 수 있나요?

예. Aspose.Page의 PdfDevice 또는 ImageDevice에 PsDocument 인스턴스를 전달하여 생성된 PostScript 파일을 PDF, PNG, JPEG 또는 TIFF 형식으로 직접 변환할 수 있습니다.

3. PsDocument에서 좌표와 측정 단위는 어떻게 정의되나요?

Aspose.Page는 표준 PostScript 포인트를 사용하며 1인치 = 72포인트입니다. 좌표는 페이지 표면의 원점을 기준으로 측정됩니다.