How katjas/pdfrenderer works
1. Overall purpose
This repository is a Java PDF rendering library, forked from pdf-renderer. Its core job is to take PDF bytes, parse the document structure, interpret page content, and paint pages onto a Graphics2D target.
2. Primary runtime flow
The main data flow is: raw PDF bytes → PDFFile / PDFParser / PDFObject graph → PDFPage → PDFRenderer → Java2D drawing commands. The example in the README shows the typical boundary: application code owns the byte source and the destination Graphics2D, while the library handles parsing and rendering.
3. Document parsing layer
PDFFile, PDFParser, PDFXref, PDFObject, PDFDestination, NameTree, and OutlineNode make up the document model and navigation layer. This subsystem reads the PDF structure, cross-reference tables, outlines, destinations, and page/page-tree information before any rendering happens.
4. Rendering and page composition
PDFRenderer, PDFPage, PDFCmd, PDFShapeCmd, PDFPaint, and PDFTextFormat form the page interpretation layer. Content streams are converted into drawing commands, text operations, fills/strokes, and image placements, then executed against Java2D. This is where page appearance is assembled.
5. Image decoding pipeline
The decode package handles PDF filter streams and image-related decompression: ASCII85, ASCIIHex, Flate, LZW, RunLength, DCT/JPEG, JPX, JBIG2, CCITT Fax, and predictor handling. This is a major subsystem because PDF pages often carry compressed images and mask data that must be decoded before painting.
6. Encryption and decryption boundary
The decrypt package isolates PDF security handling. PDFDecrypterFactory, StandardDecrypter, CryptFilterDecrypter, and IdentityDecrypter decide how encrypted objects and streams are read. The repository also includes explicit error types for unsupported encryption and authentication failures, so encryption is a distinct parse-time concern.
7. Font subsystem
The font package is one of the largest subsystems and handles built-in fonts, Type 1/1C, Type 3, TrueType, CID fonts, and font descriptors/encodings. It also contains TTF parsing (font/ttf) and CID mapping resources. Font resolution is a separate pipeline from page parsing because text rendering depends on font lookup, glyph mapping, and fallback behavior.
8. Color management
The colorspace package implements PDF color spaces beyond basic RGB/gray, including CMYK, CalRGB, CalGray, Lab, Indexed, Pattern, alternate/mask spaces, and ICC-related resources. This layer sits between decoded page/image data and final Java2D colors, and the README notes color handling as an area with known limitations and fixes.
9. PDF functions and PostScript evaluation
The function package supports sampled and procedural PDF functions, including Type 0, 2, 3, and 4. Type 4 uses a small PostScript interpreter under function/postscript, which matters for alternate color spaces, shading, and other PDF features that compute values dynamically rather than storing them directly.
10. Patterns, shadings, and graphics effects
The pattern package contains tiling patterns and shading support, which feed into rendering when fills are not simple solid colors. This is part of the graphics interpretation layer and works alongside the color space and function subsystems.
11. Actions and annotations
The action and annotation packages cover interactive PDF features such as links, go-to actions, launch/URI actions, widgets, stamps, free text, and markup annotations. These are mostly document-level features that are parsed from page metadata and then exposed or partially rendered, rather than part of core page painting.
12. Resources and external dependencies
The repo bundles font resources, ICC profiles, and stdclasses assets, plus a third-party JBIG2 decoder JAR under stdclasses/unpublic. The README also mentions use of ImageIO-based JPEG handling and local TTF font lookup on the host system, so rendering can depend on both bundled assets and environment-installed fonts.
13. Testing and examples
src/test/myrenderer contains a simple rendering demo harness, and test/src/.../PostScriptOPTest.java exercises the PostScript function subsystem. This suggests the project’s test surface is focused on rendering behavior and function evaluation rather than a large application-level test suite.
14. Build and packaging
The repository includes both pom.xml and build.xml, so it supports Maven and Ant-based builds. That reinforces that this is a library artifact, not a standalone application: downstream code is expected to instantiate PDFFile, obtain pages, and render them into its own UI or image pipeline.
Main entry points: PDFObject.java PDFObject · PDFRenderer.java PDFRenderer · PDFDecoder.java Decode filters · PDFFont.java Font core
Components 25
Core parse/render
Document model, page parsing, and Java2D rendering pipeline.
- Host app · Downstream code supplies PDF bytes and a Graphics2D target.
- PDFFile · Top-level document wrapper that opens and exposes PDF structure.
src/com/sun/pdfview/PDFFile.java - PDFParser · Reads the PDF byte stream and builds object data.
src/com/sun/pdfview/PDFParser.java - PDFXref · Resolves cross-reference entries and object locations.
src/com/sun/pdfview/PDFXref.java - PDFObject · Represents parsed PDF dictionaries, arrays, streams, and primitives.
src/com/sun/pdfview/PDFObject.java - PDFPage · Holds a page's content stream and page-level state for rendering.
src/com/sun/pdfview/PDFPage.java - PDFRenderer · Interprets page content and issues Java2D drawing commands.
src/com/sun/pdfview/PDFRenderer.java - PDFCmd · Base command abstraction for page painting operations.
src/com/sun/pdfview/PDFCmd.java - Shape cmd · Geometry and path drawing commands used by page composition.
src/com/sun/pdfview/PDFShapeCmd.java - PDFPaint · Bridges PDF paint state into Java2D fill and stroke behavior.
src/com/sun/pdfview/PDFPaint.java - Text format · Tracks PDF text state, spacing, matrices, and glyph placement.
src/com/sun/pdfview/PDFTextFormat.java
Decode pipeline
Stream filters and image decompression used during parsing and rendering.
- Decode filters · Central dispatcher for PDF stream and image filter decoding.
src/com/sun/pdfview/decode/PDFDecoder.java - Image info · Carries image dimensions, masks, and decoded image state.
src/com/sun/pdfview/ImageInfo.java
Fonts
Font resolution, glyph mapping, and embedded font resources.
- Font core · Resolves PDF font definitions into usable glyph sources.
src/com/sun/pdfview/font/PDFFont.java - TTF parser · Parses TrueType tables and glyph data for embedded or host fonts.
src/com/sun/pdfview/font/ttf/TrueTypeFont.java
Graphics model
Color spaces, functions, patterns, and other rendering effects.
- Color space · Implements PDF color spaces and conversion into renderable colors.
src/com/sun/pdfview/colorspace/PDFColorSpace.java - PDF function · Evaluates sampled and procedural PDF functions used by colors and shadings.
src/com/sun/pdfview/function/PDFFunction.java - PostScript eval · Small interpreter for Type 4 PDF function operators.
src/com/sun/pdfview/function/postscript/PostScriptParser.java - Patterns · Tiling patterns and shading objects used during fill rendering.
src/com/sun/pdfview/pattern/PDFPattern.java
Security
PDF encryption and decryption boundary.
- Decrypter · Selects the appropriate decryption strategy for encrypted objects.
src/com/sun/pdfview/decrypt/PDFDecrypterFactory.java
Interactive PDF
Actions and annotations parsed from page/document metadata.
- Actions · PDF link and navigation actions such as GoTo and URI.
src/com/sun/pdfview/action/PDFAction.java - Annotations · Page annotations including links, widgets, stamps, and markup.
src/com/sun/pdfview/annotation/PDFAnnotation.java
Bundled assets
Embedded resources required by the renderer.
- Base fonts · Embedded base font outlines and encoding resources.
src/com/sun/pdfview/font/res/BaseFonts.properties - ICC profile · Bundled grayscale profile used by color management.
src/com/sun/pdfview/colorspace/sGray.icc - JBIG2 jar · Bundled third-party JBIG2 decoder used for image streams.
stdclasses/unpublic/jbig2.jar
Connections 25
- Host app → PDFFile: open PDF · Application code hands raw bytes to the library's document entry point.
- PDFFile → PDFParser: parse · The document wrapper delegates byte-stream parsing.
- PDFParser → PDFXref: resolve xref · Parsing uses the cross-reference table to locate indirect objects.
- PDFXref → PDFObject: load object · Cross-reference entries materialize parsed PDF objects.
- PDFObject → PDFPage: build page · Page-tree and page content are assembled from PDF objects.
- PDFPage → PDFRenderer: render page · A page is interpreted into a sequence of drawing operations.
- PDFRenderer → PDFCmd: execute cmds · The renderer dispatches page commands in order.
- PDFRenderer → Shape cmd: draw paths · Shape commands handle path and geometry painting.
- PDFRenderer → PDFPaint: apply paint · Fill and stroke state is translated into Java2D paint behavior.
- PDFRenderer → Text format: place text · Text state drives glyph positioning and spacing.
- PDFObject → Decode filters: decode streams · Stream and image data are filtered during object parsing.
- Decode filters → Image info: produce image · Decoded image data is packaged for later painting.
- PDFObject → Decrypter: decrypt objects · Encrypted objects and streams are routed through the security layer.
- PDFObject → Font core: resolve fonts · Font dictionaries and descriptors are turned into usable font objects.
- Font core → TTF parser: parse TTF · TrueType fonts are parsed for tables, glyphs, and metrics.
- PDFObject → Color space: map colors · Color-space resources are resolved from PDF object graphs.
- Color space → PDF function: evaluate funcs · Some color spaces and shadings depend on PDF functions.
- PDF function → PostScript eval: type 4 · Type 4 functions are executed by the embedded PostScript interpreter.
- PDFObject → Patterns: resolve pattern · Pattern and shading resources are loaded from document objects.
- Patterns → PDF function: shade values · Shadings can use functions to compute color transitions.
- PDFObject → Actions: parse actions · Interactive link and navigation actions are extracted from objects.
- PDFObject → Annotations: parse annots · Page annotations are loaded from document metadata.
- Font core → Base fonts: base fonts · Built-in font resources back the font subsystem.
- Color space → ICC profile: profile data · Bundled ICC resources support color conversion.
- Decode filters → JBIG2 jar: jbig2 decode · JBIG2 images rely on the bundled third-party decoder.