javaghostscriptpostscriptghost4j

ghost4j class cast exception during joining two PostScripts


I am trying to join two PostScript files to one with ghost4j 0.5.0 as follows:

final PSDocument[] psDocuments = new PSDocument[2];
psDocuments[0] = new PSDocument();
psDocuments[0].load("1.ps");
psDocuments[1] = new PSDocument();
psDocuments[1].load("2.ps");
psDocuments[0].append(psDocuments[1]);
psDocuments[0].write("3.ps");

During this simplified process I got the following exception message for the above "append" line:

org.ghost4j.document.DocumentException: java.lang.ClassCastException:   
org.apache.xmlgraphics.ps.dsc.events.UnparsedDSCComment cannot be cast to 
org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage

Until now I have not made to find out whats the problem here - maybe some kind of a problem within one of the PostScript files?

So help would be appreciated.

EDIT:

I tested with ghostScript commandline tool:

gswin32.exe -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pswrite -sOutputFile="test.ps" --filename "1.ps" "2.ps"

which results in a document where 1.ps and 2.ps are merged into one(!) page (i.e. overlay). When removing the --filename the resulting document will be a PostScript with two pages as expected.


Solution

  • The exception occurs because one of the 2 documents does not follow the Adobe Document Structuring Convention (DSC), which is mandatory if you want to use the Document append method.

    Use the SafeAppenderModifier instead. There is an example here: http://www.ghost4j.org/highlevelapisamples.html (Append a PDF document to a PostScript document)