or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

content-stream-processing.mdcos-operations.mddocument-operations.mdindex.mdinteractive-forms.mdmulti-pdf-operations.mdrendering-graphics.mdsecurity-encryption.mdtext-operations.md

rendering-graphics.mddocs/

0

# Rendering and Graphics

1

2

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

3

4

## PDF Rendering

5

6

Core functionality to render PDF pages as BufferedImage objects.

7

8

```java { .api }

9

// Constructor in org.apache.pdfbox.rendering.PDFRenderer

10

public PDFRenderer(PDDocument document);

11

12

// Basic rendering methods

13

public BufferedImage renderImage(int pageIndex) throws IOException;

14

public BufferedImage renderImage(int pageIndex, float scale) throws IOException;

15

public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType) throws IOException;

16

17

// DPI-based rendering methods

18

public BufferedImage renderImageWithDPI(int pageIndex, float dpi) throws IOException;

19

public BufferedImage renderImageWithDPI(int pageIndex, float dpi, ImageType imageType) throws IOException;

20

21

// Graphics-based rendering methods (render directly to Graphics2D)

22

public void renderPageToGraphics(int pageIndex, Graphics2D graphics) throws IOException;

23

public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scale) throws IOException;

24

public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scaleX, float scaleY) throws IOException;

25

public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scaleX, float scaleY, RenderDestination destination) throws IOException;

26

```

27

28

## Image Types

29

30

Supported image formats and color spaces for rendering output.

31

32

```java { .api }

33

// Enum values in org.apache.pdfbox.rendering.ImageType

34

public static final ImageType RGB; // RGB color space

35

public static final ImageType ARGB; // ARGB with alpha channel

36

public static final ImageType BGR; // BGR color space

37

public static final ImageType GRAY; // Grayscale

38

public static final ImageType BINARY; // Black and white

39

```

40

41

## Render Destinations

42

43

Control rendering output destinations and behavior.

44

45

```java { .api }

46

// Enum values in org.apache.pdfbox.rendering.RenderDestination

47

public static final RenderDestination VIEW; // For screen viewing

48

public static final RenderDestination PRINT; // For printing

49

public static final RenderDestination EXPORT; // For export/conversion

50

```

51

52

## Content Drawing and Graphics

53

54

Low-level graphics operations for custom content stream processing.

55

56

```java { .api }

57

// Abstract methods in org.apache.pdfbox.contentstream.PDFGraphicsStreamEngine

58

protected abstract void drawImage(PDImage pdImage) throws IOException;

59

60

// Path operations

61

protected abstract void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException;

62

protected abstract void clip(int windingRule) throws IOException;

63

protected abstract void moveTo(float x, float y) throws IOException;

64

protected abstract void lineTo(float x, float y) throws IOException;

65

protected abstract void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException;

66

protected abstract void closePath() throws IOException;

67

68

// State operations

69

protected abstract void endPath() throws IOException;

70

protected abstract void strokePath() throws IOException;

71

protected abstract void fillPath(int windingRule) throws IOException;

72

protected abstract void fillAndStrokePath(int windingRule) throws IOException;

73

protected abstract void shadingFill(COSName shadingName) throws IOException;

74

```

75

76

## Graphics State

77

78

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

79

80

```java { .api }

81

// Methods in PDFGraphicsStreamEngine for graphics state

82

public Matrix getGraphicsState();

83

public PDColor getStrokingColor();

84

public PDColor getNonStrokingColor();

85

public float getLineWidth();

86

public int getLineCap();

87

public int getLineJoin();

88

public float getMiterLimit();

89

public float[] getLineDashPattern();

90

public float getLineDashPhase();

91

```

92

93

## Page Content Streaming

94

95

Create and manage page content streams for drawing operations.

96

97

```java { .api }

98

// Constructor in org.apache.pdfbox.pdmodel.PDPageContentStream

99

public PDPageContentStream(PDDocument document, PDPage page) throws IOException;

100

public PDPageContentStream(PDDocument document, PDPage page, AppendMode appendMode, boolean compress) throws IOException;

101

public PDPageContentStream(PDDocument document, PDPage page, AppendMode appendMode, boolean compress, boolean resetContext) throws IOException;

102

103

// Text operations

104

public void beginText() throws IOException;

105

public void endText() throws IOException;

106

public void setFont(PDFont font, float fontSize) throws IOException;

107

public void newLineAtOffset(float tx, float ty) throws IOException;

108

public void showText(String text) throws IOException;

109

110

// Graphics operations

111

public void moveTo(float x, float y) throws IOException;

112

public void lineTo(float x, float y) throws IOException;

113

public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException;

114

public void addRect(float x, float y, float width, float height) throws IOException;

115

public void stroke() throws IOException;

116

public void fill() throws IOException;

117

public void fillAndStroke() throws IOException;

118

119

// State management

120

public void saveGraphicsState() throws IOException;

121

public void restoreGraphicsState() throws IOException;

122

public void transform(Matrix matrix) throws IOException;

123

public void setStrokingColor(Color color) throws IOException;

124

public void setNonStrokingColor(Color color) throws IOException;

125

public void setLineWidth(float lineWidth) throws IOException;

126

127

// Resource management

128

public void close() throws IOException;

129

```

130

131

## Image Handling

132

133

Work with images within PDF documents.

134

135

```java { .api }

136

// Methods in org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject

137

public static PDImageXObject createFromFile(String imagePath, PDDocument doc) throws IOException;

138

public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name) throws IOException;

139

140

public BufferedImage getImage() throws IOException;

141

public int getWidth();

142

public int getHeight();

143

public int getBitsPerComponent();

144

public PDColorSpace getColorSpace() throws IOException;

145

146

// Drawing images in content streams

147

public void drawImage(PDImageXObject image, float x, float y) throws IOException;

148

public void drawImage(PDImageXObject image, float x, float y, float width, float height) throws IOException;

149

public void drawImage(PDImageXObject image, Matrix matrix) throws IOException;

150

```

151

152

## Usage Examples

153

154

### Basic Page Rendering

155

156

```java

157

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

158

PDFRenderer renderer = new PDFRenderer(document);

159

160

// Render first page at default resolution

161

BufferedImage image = renderer.renderImage(0);

162

163

// Render with specific DPI

164

BufferedImage highResImage = renderer.renderImageWithDPI(0, 300);

165

166

// Render as grayscale

167

BufferedImage grayImage = renderer.renderImage(0, 1.0f, ImageType.GRAY);

168

169

// Save rendered image

170

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

171

172

document.close();

173

```

174

175

### Rendering All Pages

176

177

```java

178

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

179

PDFRenderer renderer = new PDFRenderer(document);

180

181

for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) {

182

BufferedImage image = renderer.renderImageWithDPI(pageIndex, 150, ImageType.RGB);

183

184

File outputFile = new File("page-" + pageIndex + ".png");

185

ImageIO.write(image, "PNG", outputFile);

186

187

System.out.println("Rendered page " + (pageIndex + 1));

188

}

189

190

document.close();

191

```

192

193

### Creating Graphics Content

194

195

```java

196

PDDocument document = new PDDocument();

197

PDPage page = new PDPage(PDRectangle.A4);

198

document.addPage(page);

199

200

PDPageContentStream contentStream = new PDPageContentStream(document, page);

201

202

// Draw a rectangle

203

contentStream.setStrokingColor(Color.BLACK);

204

contentStream.setLineWidth(2);

205

contentStream.addRect(100, 100, 200, 150);

206

contentStream.stroke();

207

208

// Fill a circle (approximated with curves)

209

contentStream.setNonStrokingColor(Color.BLUE);

210

contentStream.moveTo(300, 300);

211

// ... curve operations to create circle

212

contentStream.fill();

213

214

// Add text

215

contentStream.beginText();

216

contentStream.setFont(PDType1Font.HELVETICA_BOLD, 16);

217

contentStream.newLineAtOffset(100, 400);

218

contentStream.showText("Hello Graphics!");

219

contentStream.endText();

220

221

contentStream.close();

222

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

223

document.close();

224

```

225

226

### Adding Images to PDF

227

228

```java

229

PDDocument document = new PDDocument();

230

PDPage page = new PDPage(PDRectangle.A4);

231

document.addPage(page);

232

233

// Load image

234

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

235

236

PDPageContentStream contentStream = new PDPageContentStream(document, page);

237

238

// Draw image at specific position and size

239

contentStream.drawImage(image, 100, 500, 300, 200);

240

241

// Draw image using transformation matrix

242

Matrix matrix = new Matrix();

243

matrix.translate(100, 100);

244

matrix.scale(0.5f, 0.5f);

245

contentStream.drawImage(image, matrix);

246

247

contentStream.close();

248

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

249

document.close();

250

```