Working with Postscript in Java

Before working with postscript files, you will need to download the Ghost Script Installer on your Windows machine and install it. This will give you access to gsdll32, which you will need to install to avoid an Java error like “Unable to load library ‘gsdll32’“.

At www.ghost4j.org, you can download the ghost4j-0.5.0.zip jar file.

Check out Adding an External Jar. Including an external dll file is similar, but you can copy the gsdll32.dll file to a Java Path folder such as “C:\Windows\System32” folder.

Example for DLL in Local Maven Repository


    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Program Files\gs\gs9.05\bin\gsdll32.dll" -Dpackaging=dll -DgroupId=org.ghost4j -DartifactId=gsdll32 -Dversion=9.05 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>gsdll32</artifactId>
      <version>9.0.5</version>
      <type>dll</type>
    </dependency>

Entry for pom.xml


    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\ghost4j-0.5.0.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=ghost4j -Dversion=1.4 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>ghost4j</artifactId>
      <version>0.5.0</version>
    </dependency>
    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\lib\xmlgraphics-commons-1.4.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=xmlgraphics-commons -Dversion=1.4 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>xmlgraphics-commons</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\lib\commons-io-1.3.1.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=commons-io -Dversion=1.3.1 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.1</version>
    </dependency>
    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\lib\log4j-1.2.15.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=log4j -Dversion=1.2.15 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.15</version>
    </dependency>
    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\lib\commons-beanutils-1.8.3.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=commons-beanutils -Dversion=1.8.3 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.8.3</version>
    </dependency>
    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\lib\commons-logging-1.1.1.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=commons-logging -Dversion=1.1.1 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\lib\itext-2.1.7.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=itext -Dversion=2.1.7 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>itext</artifactId>
      <version>2.1.7</version>
    </dependency>
    <!-- "C:\apache-maven-3.1.0\bin\mvn" install:install-file -Dfile="C:\Users\Scott\Desktop\ghost4j-0.5.0\lib\jna-3.3.0.jar" -Dpackaging=jar -DgroupId=org.ghost4j -DartifactId=jna -Dversion=3.3.0 -->
    <dependency>
      <groupId>org.ghost4j</groupId>
      <artifactId>jna</artifactId>
      <version>3.3.0</version>
    </dependency>...

Read and Write Postscript Files


package com.mycompany.app.util;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

import org.ghost4j.document.PSDocument;
import org.ghost4j.renderer.SimpleRenderer;

public class PostScriptFileHelper {
	static int SIDE_LENGTH = 200;
	public static void main(String[] args) {
		try {
			String inputFile = "/Users/Scott/workspace/myrepo/my-app/input/bell_206.ps";
			PostScriptFileHelper psfh = new PostScriptFileHelper();
			PSDocument document  = psfh.read(new File(inputFile));

			// Render an image from the PSDocument
			SimpleRenderer renderer = new SimpleRenderer();
			renderer.setResolution(300);
			List images = renderer.render(document);
			Image image = images.get(0);
			System.out.println("IMGLIST:"+images.size());
			
			// Resize the image to display
			BufferedImage resizedImage = new BufferedImage(SIDE_LENGTH, SIDE_LENGTH, BufferedImage.TYPE_INT_ARGB);
			Graphics2D g = resizedImage.createGraphics();
			g.drawImage(image, 0, 0, SIDE_LENGTH, SIDE_LENGTH, null);
			g.dispose();

			// Add the image to a JButton to display
			JButton button = new JButton();
			button.setSize(SIDE_LENGTH, SIDE_LENGTH);
			button.setIcon(new ImageIcon(resizedImage));
		
			// Create a JFrame to add the button with the image to display
			JFrame frame = new JFrame();
			frame.add(button);
			frame.setLocation(0,0);
			frame.setSize(SIDE_LENGTH, SIDE_LENGTH);
			frame.setVisible(true);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public PSDocument read(File inputFile) throws Exception{
		PSDocument document = new PSDocument();
		document.load(inputFile);
		return document;
	}
	
	public void write(File outputFile, PSDocument document) throws Exception{
		document.write(outputFile);
	}
}

PS to PDF Converter


PSDocument document = new PSDocument();
document.load(new File("input.ps"));
fos = new FileOutputStream(new File("rendition.pdf"));
PDFConverter converter = new PDFConverter();
converter.setPDFSettings(PDFConverter.OPTION_PDFSETTINGS_PREPRESS);
converter.convert(document, fos);

It would be great to convert List to PSDocument (see stackoverflow.com).