### PS to PDF Conversion Example
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/index.md
This snippet demonstrates how to load a PostScript document, set PDF conversion options, and convert it to a PDF file using Ghost4J.
```java
//load PostScript document
PSDocument document = new PSDocument();
document.load(new File("input.ps"));
//create OutputStream
fos = new FileOutputStream(new File("rendition.pdf"));
//create converter
PDFConverter converter = new PDFConverter();
//set options
converter.setPDFSettings(PDFConverter.OPTION_PDFSETTINGS_PREPRESS);
//convert
converter.convert(document, fos);
```
--------------------------------
### Convert Postscript to PDF
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/coreapisamples.md
This example demonstrates how to convert a Postscript file to a PDF file using the Ghostscript library. It sets up the necessary interpreter arguments and executes the conversion.
```java
package org.ghost4j.example;
import org.ghost4j.Ghostscript;
import org.ghost4j.GhostscriptException;
/**
* Example showing how to convert a Postscript file to PDF.
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class PDFConvertExample {
public static void main(String[] args) {
//get Ghostscript instance
Ghostscript gs = Ghostscript.getInstance();
//prepare Ghostscript interpreter parameters
//refer to Ghostscript documentation for parameter usage
String[] gsArgs = new String[10];
gsArgs[0] = "-ps2pdf";
gsArgs[1] = "-dNOPAUSE";
gsArgs[2] = "-dBATCH";
gsArgs[3] = "-dSAFER";
gsArgs[4] = "-sDEVICE=pdfwrite";
gsArgs[5] = "-sOutputFile=output.pdf";
gsArgs[6] = "-c";
gsArgs[7] = ".setpdfwrite";
gsArgs[8] = "-f";
gsArgs[9] = "input.ps";
//execute and exit interpreter
try {
gs.initialize(gsArgs);
gs.exit();
} catch (GhostscriptException e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
```
--------------------------------
### PS to PDF Conversion Example
Source: https://github.com/zippy1978/ghost4j/blob/master/README.md
This Java code snippet demonstrates how to convert a PostScript file to a PDF document using Ghost4J. Ensure the necessary file I/O streams are properly handled.
```java
//load PostScript document
PSDocument document = new PSDocument();
document.load(new File("input.ps"));
//create OutputStream
fos = new FileOutputStream(new File("rendition.pdf"));
//create converter
PDFConverter converter = new PDFConverter();
//set options
converter.setPDFSettings(PDFConverter.OPTION_PDFSETTINGS_PREPRESS);
//convert
converter.convert(document, fos);
```
--------------------------------
### Handle Display Callback for Image Output
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/coreapisamples.md
This example shows how to set up a display callback to capture page rasters as images during Ghostscript processing. It uses an ImageWriterDisplayCallback to save each page as a PNG file.
```java
package org.ghost4j.example;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.ghost4j.Ghostscript;
import org.ghost4j.GhostscriptException;
import org.ghost4j.display.ImageWriterDisplayCallback;
/**
* Example showing how to setup a display callback (to interract with the display) for the Ghostscript interpreter.
* In this example, a simple ImageWriterDisplayCallback is used as callback: it converts page rasters into images and stores them.
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class DisplayCallbackExample {
public static void main(String[] args) {
//get Ghostscript instance
Ghostscript gs = Ghostscript.getInstance();
//create display callback (capture display output pages as images)
ImageWriterDisplayCallback displayCallback = new ImageWriterDisplayCallback();
//set display callback
gs.setDisplayCallback(displayCallback);
//prepare Ghostscript interpreter parameters with display device
String[] gsArgs = new String[7];
gsArgs[0] = "-dQUIET";
gsArgs[1] = "-dNOPAUSE";
gsArgs[2] = "-dBATCH";
gsArgs[3] = "-dSAFER";
gsArgs[4] = "-sDEVICE=display";
gsArgs[5] = "-sDisplayHandle=0";
gsArgs[6] = "-dDisplayFormat=16#804";
//run PostScript (also works with PDF) and exit interpreter
try {
gs.initialize(gsArgs);
gs.runFile("input.ps");
gs.exit();
} catch (GhostscriptException e) {
System.out.println("ERROR: " + e.getMessage());
}
//write images captured by the display callback to the disk in PNG format
try {
for (int i = 0; i < displayCallback.getImages().size(); i++) {
ImageIO.write((RenderedImage) displayCallback.getImages().get(i), "png", new File((i + 1) + ".png"));
}
} catch (IOException e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
```
--------------------------------
### File Header for Source Files
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/contributing.md
All source files in the project must include this standard header comment.
```java
/*
* Ghost4J: a Java wrapper for Ghostscript API.
*
* Distributable under LGPL license.
* See terms of license at http://www.gnu.org/licenses/lgpl.html.
*/
```
--------------------------------
### Maven Configuration for Ghost4J
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/index.md
This snippet shows the Maven repository and dependency configuration required to include Ghost4J in your project. Note that since version 1.0.0, artifacts are available from Maven Central.
```xml
...
org.ghost4j.repository.releases
Ghost4J releases
http://repo.ghost4j.org/maven2/releases
org.ghost4j.repository.snapshots
Ghost4J snapshots
http://repo.ghost4j.org/maven2/snapshots
...
...
org.ghost4j
ghost4j
1.0.0
...
```
--------------------------------
### Analyze Ink Coverage of PostScript Document
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/highlevelapisamples.md
Demonstrates how to analyze the ink coverage of a PostScript document using the InkAnalyzer. This method also works for PDF documents. Ensure 'input-2pages.ps' exists in the same directory.
```java
package org.ghost4j.example;
import java.io.File;
import java.util.List;
import org.ghost4j.analyzer.AnalysisItem;
import org.ghost4j.analyzer.InkAnalyzer;
import org.ghost4j.document.PSDocument;
/**
* Example showing how to analyze ink coverage of a PS (works with PDF as well)
* document using the high level API.
*
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class InkAnalyzerExample {
public static void main(String[] args) {
try {
// load PS document
PSDocument document = new PSDocument();
document.load(new File("input-2pages.ps"));
// create analyzer
InkAnalyzer analyzer = new InkAnalyzer();
// analyze
List coverageData = analyzer.analyze(document);
// print result
for (AnalysisItem analysisItem : coverageData) {
System.out.println(analysisItem);
}
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
```
--------------------------------
### Render PDF Document to Images
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/highlevelapisamples.md
Renders pages of a PDF document into images using SimpleRenderer. Sets resolution to 300 DPI and saves each page as a PNG file. Requires input.pdf.
```java
package org.ghost4j.example;
import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import org.ghost4j.analyzer.AnalysisItem;
import org.ghost4j.analyzer.FontAnalyzer;
import org.ghost4j.document.PDFDocument;
import org.ghost4j.renderer.SimpleRenderer;
/**
* Example showing how to render pages of a PDF document using the high level API.
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class SimpleRendererExample {
public static void main(String[] args) {
try {
// load PDF document
PDFDocument document = new PDFDocument();
document.load(new File("input.pdf"));
// create renderer
SimpleRenderer renderer = new SimpleRenderer();
// set resolution (in DPI)
renderer.setResolution(300);
// render
List images = renderer.render(document);
// write images to files to disk as PNG
try {
for (int i = 0; i < images.size(); i++) {
ImageIO.write((RenderedImage) images.get(i), "png", new File((i + 1) + ".png"));
}
} catch (IOException e) {
System.out.println("ERROR: " + e.getMessage());
}
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
```
--------------------------------
### Maven Repositories for Older Releases
Source: https://github.com/zippy1978/ghost4j/blob/master/README.md
Include these repositories in your Maven configuration if you are using a release prior to 1.0.0.
```xml
...
org.ghost4j.repository.releases
Ghost4J releases
http://repo.ghost4j.org/maven2/releases
org.ghost4j.repository.snapshots
Ghost4J snapshots
http://repo.ghost4j.org/maven2/snapshots
...
```
--------------------------------
### Convert PostScript to PDF
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/highlevelapisamples.md
Converts a PostScript file to a PDF document using PDFConverter. Ensure input.ps exists and specify output file path. Uses PDFConverter.OPTION_PDFSETTINGS_PREPRESS for conversion settings.
```java
package org.ghost4j.example;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.io.IOUtils;
import org.ghost4j.converter.PDFConverter;
import org.ghost4j.document.PSDocument;
/**
* Example showing how to convert a Postscript document to PDF using the high level API.
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class PDFConverterExample {
public static void main(String[] args) {
FileOutputStream fos = null;
try{
//load PostScript document
PSDocument document = new PSDocument();
document.load(new File("input.ps"));
//create OutputStream
fos = new FileOutputStream(new File("rendition.pdf"));
//create converter
PDFConverter converter = new PDFConverter();
//set options
converter.setPDFSettings(PDFConverter.OPTION_PDFSETTINGS_PREPRESS);
//convert
converter.convert(document, fos);
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
} finally{
IOUtils.closeQuietly(fos);
}
}
}
```
--------------------------------
### Append PDF to PostScript Document
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/highlevelapisamples.md
Demonstrates how to append a PDF document to an existing PostScript document using the SafeAppenderModifier. Ensure both input files ('input.ps' and 'input.pdf') exist in the same directory as the program.
```java
package org.ghost4j.example;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.ghost4j.document.Document;
import org.ghost4j.document.PDFDocument;
import org.ghost4j.document.PSDocument;
import org.ghost4j.modifier.SafeAppenderModifier;
/**
* Example showing how to append a PostScript document to a PDF document.
*
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class SafeAppenderModifierExample {
public static void main(String[] args) {
try {
// load PS document
PSDocument psDocument = new PSDocument();
psDocument.load(new File("input.ps"));
// load PDF document
PDFDocument pdfDocument = new PDFDocument();
pdfDocument.load(new File("input.pdf"));
// prepare modifier
SafeAppenderModifier modifier = new SafeAppenderModifier();
// prepare modifier parameters
Map parameters = new HashMap();
parameters.put(SafeAppenderModifier.PARAMETER_APPEND_DOCUMENT,
pdfDocument);
// run modifier
Document result = modifier.modify(psDocument, parameters);
// write resulting document to file
result.write(new File("merged.ps"));
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
```
--------------------------------
### Count Pages of a PostScript Document
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/highlevelapisamples.md
Counts the number of pages in a PostScript file using PSDocument. Requires the input.ps file to be present.
```java
package org.ghost4j.example;
import java.io.File;
import org.ghost4j.document.PSDocument;
/**
* Example showing how to count pages of a PostScript.
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class PSPageCountExample {
public static void main(String[] args) {
try {
PSDocument psDocument = new PSDocument();
psDocument.load(new File("input.ps"));
System.out.println("Page count is : " + psDocument.getPageCount());
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
```
--------------------------------
### Author Tag in Class Documentation
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/contributing.md
The initial author of each source file must be specified in the class documentation comment using the @author tag.
```java
@author Gilles Grousset (gi.grousset@gmail.com)
```
--------------------------------
### Maven Dependency for Ghost4J
Source: https://github.com/zippy1978/ghost4j/blob/master/README.md
Add this dependency to your Maven project to include the Ghost4J library.
```xml
...
org.ghost4j
ghost4j
1.0.1
...
```
--------------------------------
### Destroy Ghostscript Instance
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/encoding.md
Call this method before changing the ghost4j.encoding system property at runtime to ensure the change is applied. This is crucial when working with multiple JVMs or when reconfiguring encoding.
```java
Ghostscript.deleteInstance();
```
--------------------------------
### Setting Max Process Count for PDFConverter
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/threadsafetyandmultithreading.md
Configures a PDFConverter component to enable multi-threading by setting the maximum number of concurrent 'slave' JVMs. A value of 0 disables multi-threading.
```java
//create converter
PDFConverter converter = new PDFConverter();
//set multi-threading
converter.setMaxProcessCount(2);
```
--------------------------------
### List Fonts in a PDF Document
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/highlevelapisamples.md
Analyzes a PDF document to list all embedded fonts using FontAnalyzer. The input.pdf file must exist. The output is a list of AnalysisItem objects.
```java
package org.ghost4j.example;
import java.io.File;
import java.util.List;
import org.ghost4j.analyzer.AnalysisItem;
import org.ghost4j.analyzer.FontAnalyzer;
import org.ghost4j.document.PDFDocument;
/**
* Example showing how to list fonts of a PDF document using the high level API.
* @author Gilles Grousset (gi.grousset@gmail.com)
*/
public class FontAnalyzerExample {
public static void main(String[] args) {
try {
// load PDF document
PDFDocument document = new PDFDocument();
document.load(new File("input.pdf"));
// create analyzer
FontAnalyzer analyzer = new FontAnalyzer();
// analyze
List fonts = analyzer.analyze(document);
// print result
for (AnalysisItem analysisItem : fonts) {
System.out.println(analysisItem);
}
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
```
--------------------------------
### Synchronized Ghostscript Instance Access
Source: https://github.com/zippy1978/ghost4j/blob/master/src/site/markdown/threadsafetyandmultithreading.md
Ensures thread safety when calling Ghostscript interpreter operations by synchronizing access to the shared Ghostscript instance. Always use this pattern for thread-safe operations.
```java
Ghostscript gs = Ghostscript.getInstance();
try {
synchronized(gs){
//call interpreter operations here
. 1. The Ghostscript API can only have one interpreter instance in the same native process. This means that when writing a program using the API, operations called on the interpreter instance must be synchronous. To make sure a Ghostscript process is thread safe when using Ghost4J, always call it like this:
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.