Most common computer images are stored as arrays of pixel data or raster graphics. Formats such as PNG, and JPEG are classic examples of this. Vector Graphics are stored as operations that are instead “code” for drawing the image on the fly.

In this article we will explore using a few different open source libraries to convertEMF, Postscript and PCL files. The libraries we will explore are FreeHEP, Ghost4J, and OpenPCL.

FreeHEP has a vector graphics package that supports EMF, Postscript and PDF. This library turns out to have pretty good support for EMF. It has not completely implemented the EMF spec (http://msdn.microsoft.com/en-us/library/cc230514). Using the spec, you can enhance the code if needed, but it serves well for our purposes. Below is an example of code used to convert from EMF to PDF.

import org.freehep.graphicsio.emf.EMFRenderer;
import org.freehep.graphicsio.emf.EMFInputStream;
import org.freehep.graphicsio.emf.EMFPanel;
import org.freehep.graphicsio.pdf.PDFGraphics2D;

import java.awt.Dimension;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class EMF2PDF
{
    public static void main(String[] args)
    {
        FileInputStream input = null;
        FileOutputStream output = null;

        try
        {
            // read the EMF file
            input = new FileInputStream(args[0]);
            EMFRenderer emfRenderer = new EMFRenderer(new EMFInputStream(input));
            EMFPanel emfPanel = new EMFPanel();
            emfPanel.setRenderer(emfRenderer);

            // Print EMF file to PDFGraphics device
            Properties properties = new Properties();
            properties.setProperty(PDFGraphics2D.TEXT_AS_SHAPES, "false");
            properties.setProperty(PDFGraphics2D.CLIP, "false");

            output = new FileOutputStream(args[1]);
            PDFGraphics2D graphics = new PDFGraphics2D(output, new Dimension(3644, 1536));
            graphics.setProperties(properties);
            graphics.startExport();
            emfPanel.print(graphics);
            graphics.endExport();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (input != null)
                {
                    input.close();
                }
                if (output != null)
                {   
                    output.close();
                }
            }
            catch (Exception ex) {}
        }
    }
}

Now on to Postscript. Unfortunately, FreeHEP does not have enough there to support rendering complex documents. Ghostscript is an extremely mature package with robust support for PostScript. Using Ghost4J’s PDFConverter example, we can convert a PostScript document to PDF. Below is an example of code used to convert from PostScript to PDF.

import java.io.File;
import java.io.FileOutputStream;
import net.sf.ghost4j.document.PSDocument;
import net.sf.ghost4j.converter.PDFConverter;
import org.apache.commons.io.IOUtils;

public class PS2PDF
{
    public static void main(String[] args)
    {
        FileOutputStream fos = null;
        try
        {
            if (args.length < 2)
            {
                System.err.println("Usage: PS2PDF <input file> <output file>");
                System.exit(1);
            }

            //load PostScript document
            PSDocument document = new PSDocument();
            document.load(new File(args[0]));

            //create OutputStream
            fos = new FileOutputStream(new File(args[1]));

            // Run the converter
            PDFConverter converter = new PDFConverter();
            converter.convert(document, fos);
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            IOUtils.closeQuietly(fos);
        }

    }
}

Lastly, lets look at PCL. In the OpenPCL library, they have a component called PclRenderImage. This code can be used to render a PCL file to a BufferedImage. Below is sample code that converts the first page to a PNG image.

import com.openpcl.pclrenderimage.PclRenderImage;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class PCL2PNG
{
    public static void main(String[] args)
    {
        if (args.length < 2)
        {
            System.err.println("Usage: PCL2PNG <input file> <output file>");
            System.exit(1);
        }

        try
        {       
            PclRenderImage renderer = new PclRenderImage();
            renderer.loadFromFileSplitIntoSeparatePclPages(args[0]);

            // Get the first page as an image
            BufferedImage page = renderer.getImageForPage(1, null, true, "LTR", 1.0);

            // Write the image to file
            ImageIO.write(page, "png", new File(args[1]));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

If all you need is to display a PCL document, then writing out the images and wrapping them as a PDF or leaving them as is should suffice. If you would like a fully text searchable document, the PclRenderImage library can be modified to write to a Graphics2D device instead of a BufferedImage. Once this is accomplished, FreeHep’s PDFGraphics2D could be used to render PCL to PDF.