Font size:      

jcgm -- Developer How-To


The examples presented here are part of the distribution in the net.sf.jcgm.example package (except for the PDF file example).

Reading and Displaying a CGM File

try {
	File cgmFile = new File("test.cgm");
	// create an input stream for the CGM file
	DataInputStream in = new DataInputStream(new FileInputStream(cgmFile));
	// read the CGM file
	CGM cgm = new CGM();
	// display the CGM file
	CGMDisplay display = new CGMDisplay(cgm);
	final CGMPanel cgmPanel = new CGMPanel(display);
catch (IOException e) {

Converting a CGM File to Another Graphic Format

Since the jcgm library implements an ImageIO plugin, it makes it easy to convert from CGM to any format supported by Java.

try {
	File cgmFile = new File("samples/allelm01.cgm");
	File outFile = new File("samples/allelm01.png");
	BufferedImage image = ImageIO.read(cgmFile);
	ImageIO.write(image, "PNG", outFile);
catch (IOException e) {


Producing a PDF file containing CGM graphics

Assumption is made that a PDF file can be produced via XSL/FO already, Apache FOP is a convenient processor for this.
  • You need to include the jcgm-image library in your class path.

  • In your XSLT, somewhere you will add:

    <fo:external-graphic src="url('samples/allelm01.cgm')"/>
  • Done
version 43