Android PDF Library (GnuAPDF)

The GnuAPDF library provides support for generation of PDF files using standard Android “draw” commands. The library provides a special Canvas subclass, PDFCanvas, that is associated with a PDF document and that renders draw commands into PDF syntax. A sequence of Android Canvas drawing method calls will thus create a structured PDF document that, when viewed with a standard PDF reader, will display the corresponding image. Note that Android version 5.0 has this functionality as part of the standard API; however, earlier versions did not provide PDF generation, and the Android support library didn’t include this as of the time of this writing.

This library is derived from the GnuJPDF library, which provides similar functionality for the Java AWT graphics system, and as such is similarly released under the LGPL License.

 

Basic Workflow

The class PDFCanvas implements the graphics operations defined by the standard Android Canvas class. To draw something into a PDF document, you use the standard Android Canvas drawing calls (including drawing, transformations, clips, etc.) on an instance of PDFCanvas. The PDFCanvas will encode these as PDF drawing commands.

A PDFCanvas is associated with a PDFPage, which is part of a PDFDocument. The basic workflow is:

// Create a PDF document, a page in the document, and get that page’s PDFCanvas
PDFDocument pdfDoc = new PDFDocument();
PDFPage page = new PDFPage(PageFormat.LETTER);
pdfDoc.add(page);
PDFCanvas pdfCanvas = page.getCanvas();

// do drawing commands as on any other Canvas
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTypeface(Typeface.SERIF);
paint.setTextSize(38);
paint.setStyle(Style.STROKE);
pdfCanvas.drawLine(0, 0,100, 100, paint);
pdfCanvas.drawRect(120, 180, 400, 400, paint);
pdfCanvas.drawText(“Some text”, 200, 600, paint);
paint.setStyle(Style.FILL);
pdfCanvas.drawCircle(300, 300, 100, paint);

// write the PDF content for the page to a file
BufferedOutputStream fileStream = new BufferedOutputStream(new FileOutputStream(file));
pdfDoc.write(outStream);
fileStream.flush();
fileStream.close();

Features

The GnuAPDF library provides most of the drawing operations available in the standard Canvas class. However, certain methods are unimplemented due to lack of support in PDF; these will throw an UnsupportedOperationException if used. Some additional features currently have no or limited support, as indicated below.

Supported operations

  • Drawing operations, including
    • drawLine
    • drawArc
    • drawCircle
    • drawOval
    • drawRect
    • drawRoundRect
    • drawPoints
    • drawColor
    • drawPaint
  • Image drawing through drawBitmap
  • Grid and 3D drawing through drawBitmapMesh and drawVertices
  • Text operations drawText, drawPosText, drawTextOnPath
  • Clipping through clipRect
  • Matrix transformation operations including scale, skew, translate, rotate and concat
  • State save and restore

Not supported

  • Dotted lines
  • Underlined and strikethrough text
  • quickReject() methods (always return false)
  • drawTextRun method (throws UnsupportedOperationException)
  • clipPath method (throws UnsupportedOperationException)
  • clipRect method with operation other than intersect (throws UnsupportedOperationException)
  • clipRegion method (throws UnsupportedOperationException)
  • drawPath method (throws UnsupportedOperationException)
  • drawPicture method (throws UnsupportedOperationException)
  • drawPatch method (throws UnsupportedOperationException)
  • Paint xfermodes
  • State-save flags and layer information are ignored; save(flags), saveLayer, and saveLayerAlpha all behave the same as plain save()

Example PDF Files

Some example files generated with GnuAPDF are at the links below. The source code for these is available in the Example Code section.

Example Code

A test project is available that illustrates how to use the library and can also be used to test its functionality.

Download example source

Documentation

Browse Javadoc documentation

Downloads

The downloads are supplied as compressed 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.

Version
Library (.jar)
Source
Version 1.3 gnuapdf_lib_v1.3.zip gnuapdf_src_v1.3.zip
Version 1.2 gnuapdf_lib_v1.2.tar.gz gnuapdf_src_v1.2.tar.gz
Version 1.1 gnuapdf_lib_v1.1.tar.gz gnuapdf_src_v1.1.tar.gz

Revision History

Version
Changes
1.0 Initial release
1.1 Bug fix for clipRect
1.2 Add support for extended ASCII (ISO-8859-1) characters
1.3 Add internationalization support for number conventions using commas in place of decimal points

Computing, lutherie, mathematics, finance, and other resources