Working with PostScript (PS) documents in Java

Create, modify, and manage PostScript (PS) documents programmatically using Aspose.Page for Java. Easily build multi-page PS documents from scratch, add pages, insert vector graphics and text elements, merge multiple PostScript files, and adjust document structure without requiring Adobe Acrobat or Distiller installations.

 

Aspose.Page for Java provides a high-level, standalone object model centered around the PsDocument class to control PostScript document creation and page manipulation.

To create new single or multi-page PostScript print streams use PsDocument(outputStream, options)

  • To structure programmatically individual pages inside a PostScript stream use PsDocument.openPage()/closePage() methods.
  • To save vector graphics, shapes, text, and pages into disk files or streams use PsDocument.save() method.
  • To set page margins, custom sizes (A4, Letter), and print orientation use PsSaveOptions class.

Creating a multi-page PostScript document in Java

Build a multi-page PS document from scratch by configuring save options and explicitly opening/closing pages within the document stream.

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

Adding pages and graphics to an existing document builder

Populate PostScript document pages with vector shapes, text elements, and transformations.

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

Configuring Custom Page Sizes and Margins

Define precise page dimensions (such as Letter, Legal, or custom dimensions) using PsSaveOptions.

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

Installation and Setup

Add Aspose.Page for Java to your 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. Do I need Adobe Acrobat or PostScript driver utilities installed?

No. Aspose.Page for Java is a native, standalone Java library that constructs, renders, and manipulates PS/EPS document structures directly without relying on external printer drivers or native software.

2. Can I convert the created PostScript (PS) document directly to PDF or raster images?

Yes. You can pass a PsDocument instance to PdfDevice or ImageDevice in Aspose.Page to convert your generated PostScript file directly into PDF, PNG, JPEG, or TIFF format.

3. How are coordinates and measurement units defined in PsDocument?

Aspose.Page uses standard PostScript points where 1 inch = 72 points Coordinates are measured relative to the page surface origin.