Search code examples
javagraphics2dpostscript

Java: How do I build a PostScript file from EPS?


I want to create a multi-page PostScript file from Graphics2D in Java. I presently have Java code that writes to a Graphics2D object for each page. Using the EPSGraphics library, I can use this to have it create a EPS object for each page.

How do I then build up a PostScript file from this collection of EPS objects?

Note: I am not asking what is the best library to do this if that's the approach and several exist. I am just asking how to do this.


Solution

  • Each EPS file has a BoundingBox as a comment (possibly also a HiResBoundingBox) you need to read the comments, that will tell you the size (in PostScript points) of each EPS.

    You then need to decide how you want to draw each EPS (note, an EPS is a single figure not a page). Given the boundingbox you decide where on the page you want to place the figure. You then save the current graphics state, write scale, translate and rotate operators to place teh EPS on the page, then execute a grestore.

    If you want multiple EPS figures per page, then repeat for each figure.

    At the end of the page, write the showpage operator to render and eject the page. Repeat for the next page.

    So your final PostScript program should look something like:

    %!PS
    gsave
    x y translate
    x y scale
    
    % Insert the entire content of the first EPS here
    %
    
    %!PS_Adobe_EPSF....
    ....
    ....
    %%EOF
    
    grestore
    
    
    gsave
    x y translate
    x y scale
    
    % Insert the entire content of the second EPS here (if required)
    %
    
    %!PS_Adobe_EPSF....
    ....
    ....
    %%EOF
    ...
    ...
    
    grestore
    showpage
    
    %% Begin page 2, repeat as above
    
    showpage