CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-pdfbox--pdfbox

The Apache PDFBox library is an open source Java tool for working with PDF documents.

Pending
Overview
Eval results
Files

rendering-graphics.mddocs/

Rendering and Graphics

Convert PDF pages to images with precise control over resolution, color spaces, and rendering quality. Includes support for various image formats and rendering customization.

PDF Rendering

Core functionality to render PDF pages as BufferedImage objects.

// Constructor in org.apache.pdfbox.rendering.PDFRenderer
public PDFRenderer(PDDocument document);

// Basic rendering methods
public BufferedImage renderImage(int pageIndex) throws IOException;
public BufferedImage renderImage(int pageIndex, float scale) throws IOException;
public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType) throws IOException;

// DPI-based rendering methods
public BufferedImage renderImageWithDPI(int pageIndex, float dpi) throws IOException;
public BufferedImage renderImageWithDPI(int pageIndex, float dpi, ImageType imageType) throws IOException;

// Graphics-based rendering methods (render directly to Graphics2D)
public void renderPageToGraphics(int pageIndex, Graphics2D graphics) throws IOException;
public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scale) throws IOException;
public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scaleX, float scaleY) throws IOException;
public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scaleX, float scaleY, RenderDestination destination) throws IOException;

Image Types

Supported image formats and color spaces for rendering output.

// Enum values in org.apache.pdfbox.rendering.ImageType
public static final ImageType RGB;      // RGB color space
public static final ImageType ARGB;     // ARGB with alpha channel
public static final ImageType BGR;      // BGR color space  
public static final ImageType GRAY;     // Grayscale
public static final ImageType BINARY;   // Black and white

Render Destinations

Control rendering output destinations and behavior.

// Enum values in org.apache.pdfbox.rendering.RenderDestination
public static final RenderDestination VIEW;    // For screen viewing
public static final RenderDestination PRINT;   // For printing
public static final RenderDestination EXPORT;  // For export/conversion

Content Drawing and Graphics

Low-level graphics operations for custom content stream processing.

// Abstract methods in org.apache.pdfbox.contentstream.PDFGraphicsStreamEngine
protected abstract void drawImage(PDImage pdImage) throws IOException;

// Path operations
protected abstract void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException;
protected abstract void clip(int windingRule) throws IOException;
protected abstract void moveTo(float x, float y) throws IOException;
protected abstract void lineTo(float x, float y) throws IOException;
protected abstract void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException;
protected abstract void closePath() throws IOException;

// State operations
protected abstract void endPath() throws IOException;
protected abstract void strokePath() throws IOException;
protected abstract void fillPath(int windingRule) throws IOException;
protected abstract void fillAndStrokePath(int windingRule) throws IOException;
protected abstract void shadingFill(COSName shadingName) throws IOException;

Graphics State

Manage graphics state including transformations, colors, and line styles.

// Methods in PDFGraphicsStreamEngine for graphics state
public Matrix getGraphicsState();
public PDColor getStrokingColor();
public PDColor getNonStrokingColor();
public float getLineWidth();
public int getLineCap();
public int getLineJoin();
public float getMiterLimit();
public float[] getLineDashPattern();
public float getLineDashPhase();

Page Content Streaming

Create and manage page content streams for drawing operations.

// Constructor in org.apache.pdfbox.pdmodel.PDPageContentStream
public PDPageContentStream(PDDocument document, PDPage page) throws IOException;
public PDPageContentStream(PDDocument document, PDPage page, AppendMode appendMode, boolean compress) throws IOException;
public PDPageContentStream(PDDocument document, PDPage page, AppendMode appendMode, boolean compress, boolean resetContext) throws IOException;

// Text operations
public void beginText() throws IOException;
public void endText() throws IOException;
public void setFont(PDFont font, float fontSize) throws IOException;
public void newLineAtOffset(float tx, float ty) throws IOException;
public void showText(String text) throws IOException;

// Graphics operations
public void moveTo(float x, float y) throws IOException;
public void lineTo(float x, float y) throws IOException;
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException;
public void addRect(float x, float y, float width, float height) throws IOException;
public void stroke() throws IOException;
public void fill() throws IOException;
public void fillAndStroke() throws IOException;

// State management
public void saveGraphicsState() throws IOException;
public void restoreGraphicsState() throws IOException;
public void transform(Matrix matrix) throws IOException;
public void setStrokingColor(Color color) throws IOException;
public void setNonStrokingColor(Color color) throws IOException;
public void setLineWidth(float lineWidth) throws IOException;

// Resource management
public void close() throws IOException;

Image Handling

Work with images within PDF documents.

// Methods in org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject
public static PDImageXObject createFromFile(String imagePath, PDDocument doc) throws IOException;
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name) throws IOException;

public BufferedImage getImage() throws IOException;
public int getWidth();
public int getHeight();
public int getBitsPerComponent();
public PDColorSpace getColorSpace() throws IOException;

// Drawing images in content streams
public void drawImage(PDImageXObject image, float x, float y) throws IOException;
public void drawImage(PDImageXObject image, float x, float y, float width, float height) throws IOException;
public void drawImage(PDImageXObject image, Matrix matrix) throws IOException;

Usage Examples

Basic Page Rendering

PDDocument document = Loader.loadPDF(new File("document.pdf"));
PDFRenderer renderer = new PDFRenderer(document);

// Render first page at default resolution
BufferedImage image = renderer.renderImage(0);

// Render with specific DPI
BufferedImage highResImage = renderer.renderImageWithDPI(0, 300);

// Render as grayscale
BufferedImage grayImage = renderer.renderImage(0, 1.0f, ImageType.GRAY);

// Save rendered image
ImageIO.write(image, "PNG", new File("page-0.png"));

document.close();

Rendering All Pages

PDDocument document = Loader.loadPDF(new File("document.pdf"));
PDFRenderer renderer = new PDFRenderer(document);

for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) {
    BufferedImage image = renderer.renderImageWithDPI(pageIndex, 150, ImageType.RGB);
    
    File outputFile = new File("page-" + pageIndex + ".png");
    ImageIO.write(image, "PNG", outputFile);
    
    System.out.println("Rendered page " + (pageIndex + 1));
}

document.close();

Creating Graphics Content

PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(document, page);

// Draw a rectangle
contentStream.setStrokingColor(Color.BLACK);
contentStream.setLineWidth(2);
contentStream.addRect(100, 100, 200, 150);
contentStream.stroke();

// Fill a circle (approximated with curves)
contentStream.setNonStrokingColor(Color.BLUE);
contentStream.moveTo(300, 300);
// ... curve operations to create circle
contentStream.fill();

// Add text
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 16);
contentStream.newLineAtOffset(100, 400);
contentStream.showText("Hello Graphics!");
contentStream.endText();

contentStream.close();
document.save("graphics-example.pdf");
document.close();

Adding Images to PDF

PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);

// Load image
PDImageXObject image = PDImageXObject.createFromFile("image.jpg", document);

PDPageContentStream contentStream = new PDPageContentStream(document, page);

// Draw image at specific position and size
contentStream.drawImage(image, 100, 500, 300, 200);

// Draw image using transformation matrix
Matrix matrix = new Matrix();
matrix.translate(100, 100);
matrix.scale(0.5f, 0.5f);
contentStream.drawImage(image, matrix);

contentStream.close();
document.save("image-example.pdf");
document.close();

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-pdfbox--pdfbox

docs

content-stream-processing.md

cos-operations.md

document-operations.md

index.md

interactive-forms.md

multi-pdf-operations.md

rendering-graphics.md

security-encryption.md

text-operations.md

tile.json