JDXF: Java DXF Library

The Java JDXF library provides support for generation of DXF files for use with CAD programs using standard Java AWT Graphics “draw” and “fill” commands. The library provides a special Graphics2D subclass, DXFGraphics, that is associated with a DXF document and that renders draw commands into DXF syntax. A sequence of standard Java Graphics drawing method calls on a DXFGraphics instance will thus create a structured DXF document that, when opened with a standard CAD program, will display the Java image as a CAD design that can be modified within the CAD program.

This library is released under the MIT License.

Basic Workflow

The class DXFGraphics implements the graphics operations defined by the standard Java Graphics2D class. To create a DXF drawing, you use the standard Java Graphics drawing calls (including drawing, transformations, etc.) on an instance of DXFGraphics. The DXFGraphics will encode these Java drawing commands as DXF objects in an associated DXFDocument, which can then be retrieved as a text string and saved in a DXF file.

A DXFGraphics is associated with a DXFDocument. The basic workflow is:

/* Create a DXF document and get its associated DXFGraphics instance */

DXFDocument dxfDocument = new 
DXFGraphics dxfGraphics = 

/* Do drawing commands as on any other Graphics. If you have a paint(Graphics) method, you can just use it with the DXFGraphics instance since it's a subclass of Graphics. */
/* Get the DXF output as a string - it's just text - and  save  in a file for use with a CAD package */
String dxfText = dxfDocument.toDXFString();
String filePath = “path/to/file.dxf”;
FileWriter fileWriter = new FileWriter(filePath);

/* For drawing, just use standard Java
   drawing operations */
public void paint(Graphics graphics)
  // set pen characteristics
  graphics.setStroke(new BasicStroke(3));
  // draw stuff - line, rectangles, ovals, ...
  graphics.drawLine(0, 0, 1000, 500);
  graphics.drawRect(1000, 500, 150, 150);
  graphics.drawRoundRect(20, 200, 130, 100, 20, 
  graphics.drawOval(200, 800, 200, 400);
  graphics.drawArc(100, 1900, 400, 200, 60, 150);

  // can draw filled shapes, which get 
  // implemented as DXF hatches
  graphics.fillRect(100, 100, 100, 50);
  int[] xPoints = {200, 300, 250};
  int[] yPoints = {200, 250, 300};
  graphics.fillPolygon(xPoints, yPoints, 

  // text too
  graphics.setFont(new Font(Font.MONOSPACED, 
                            Font.PLAIN, 38));
  graphics.drawString("Some 38-point monospaced   
      blue text at position 480, 400", 480, 400);

  // and even transformations
  graphics.shear(0.1f, 0.2f);
  graphics.drawRect(100, 100, 200, 200);



The JDXF library provides most of the drawing operations available in the standard Java Graphics and Graphics2D classes. However, certain methods are unimplemented at present (some due to lack of support in DXF). These unimplemented operations will be ignored or throw an UnsupportedOperationException if used, as indicated below. Some additional features currently have no or limited support, as indicated below.

Supported operations

  • Drawing operations, including
    • drawLine, drawPolyline
    • drawPolygon, fillPolygon
    • drawArc, fillArc
    • drawOval, fillOval
    • drawRect, fillRect, clearRect
    • drawRoundRect, fillRoundRect
    • draw(Shape), fill(Shape), clear(Shape)
  • Text operations
    • drawString, drawChars, drawBytes
    • registerFont
  • Drawing properties
    • Color
    • Line width
    • Font properties: serif/sans-serif, size, bold, italic
  • Affine transformation operations
    • scale
    • shear
    • translate
    • rotate


The library provides methods for creating and drawing B-splines on the standard Java Graphics, as well as generating DXF code for splines.

Not supported

  • Image drawing – throws UnsupportedOperationException
  • Paint xfermodes – Ignored
  • Clipping, including clipRect, clipRegion, clipPath, etc. – Ignored

Enhancements for version 1.1

  • Added method setUnits in DXFDocument for specifying the units that measurements should be interpreted as in the DXF file
  • Added the ability to load external fonts through a new DXFGraphicsEnvironment class for use in addition to the default fonts. When these external font files (TrueType, e.g.) are then loaded into the CAD program along with the DXF file, the font sizing and spacing will be much more consistent between the Java screen representation and the CAD program representation.

Enhancements for version 1.2

  • Configure to generate true DXF circles and arcs; earlier versions used the more general elliptical arcs for all such curves. Includes the method generateCircularArcs(boolean) in DXFDocument to control this behavior in case the previous behavior is desired (default is to use the new approach and generate circles and arcs when appropriate).
  • Added the method setPrecisionDigits(int) in DXFDocument to control the number of decimal digits to be output in the DXF file, to deal with rounding issues; default is to use 10 digits.

Enhancements for version 1.3

  • Configure to generate true DXF points from zero-length Java lines and zero-radius Java circles; earlier versions simply inserted the corresponding degenerate lines or circles into the DXF output. Includes the method generatePoints((boolean) in DXFDocument to control this behavior in case the previous behavior is desired (default is to use the new approach and generate DXF points in place of degenerate circles and lines).

Example DXF Files

Some example files generated with JDXF are at the links below. The Java source code used to generate these through the JDXF Library is available in the Example Code section.

Example Code

A test project is available that illustrates how to use the library and that can also be used to test its functionality. The class JDXFTestPanel illustrates the use of the JDXFGraphics class and its draw methods.

  • JDXFTest.java
  • JDXFTestPanel.java

Download example source



The downloads are supplied as gzipped tar archives; the library archive contains the compiled gnuapdf.jar file for inclusion in a project, while the source archive contains the Java source files that can be used to rebuild the .jar file or included as source files in a referencing project. The test app source contains source for a simple test app that uses the classes in the JDXF library.

Library (.jar)
Test App Source
 1.0 jdxf_lib_v1.0.tar.gz jdxf_src_v1.0.tar.gz jdxf_test_v1.0.tar.gz
1.1.1 jdxf_lib_v1.1.1.tar.gz jdxf_src_v1.1.1.tar.gz jdxf_test_v1.1.tar.gz
1.2 jdxf_lib_v1.2.zip jdxf_src_v1.2.zip jdxf_test_v1.2.zip
1.3 jdxf_lib_v1.3.zip jdxf_src_v1.3.zip jdxf_test_v1.3.zip

Revision History

Date Changes
1.0 2018-05-22
  • Initial release
1.1 2019-05-18
  • Loadable font support
  • Units specification
  • Bug fix for quadratic and cubic path iterator segments
1.1.1 2019-06-10
  • Bug fixes for LibreCAD, Kabeja
  • Closed flag for closed polylines
  • Double precision throughout
1.2 2020-03-03
  • Configure to generate DXF circles and arcs where appropriate in place of general elliptical arcs
  • Add call to configure number of decimal digits of precision to use
1.3 2020-05-08
  • Configure to generate DXF points for zero-length Java lines and zero-radius circles
  • Fix bug in draw(Shape) method for Line2D handling



The JDXF library adds DXF elements that are not strictly required by the standard, but are required in order to open the files in AutoCAD.

Computing, lutherie, mathematics, finance, and other resources