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.
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);
PDFCanvas pdfCanvas = page.getCanvas();
// do drawing commands as on any other Canvas
Paint paint = new Paint();
pdfCanvas.drawLine(0, 0,100, 100, paint);
pdfCanvas.drawRect(120, 180, 400, 400, paint);
pdfCanvas.drawText(“Some text”, 200, 600, paint);
pdfCanvas.drawCircle(300, 300, 100, paint);
// write the PDF content for the page to a file
BufferedOutputStream fileStream = new BufferedOutputStream(new FileOutputStream(file));
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.
- Drawing operations, including
- 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
- 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 of basic graphics operations
- Example of basic text operations
- Example of matrix transformation operations
- Example of clipping operations
- Example of image-handling operations
- Example of advanced bitmap-mesh support (stretching a bitmap across a rectangular mesh)
- Example of advanced draw-vertices support (Gouraud shading across a triangular mesh)
A test project is available that illustrates how to use the library and can also be used to test its functionality.
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.
|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|