Multiple page PDF and draw to screen?

Is it possible to render a drawing to a multiple page PDF and draw the image to the screen?

I can do either or, obviously, but the combination errors out on this line in the draw loop…

PGraphicsPDF pdf = (PGraphicsPDF) g;

With this error…

ClassCastException: processing.awt.PGraphicsJava2D cannot be cast to processing.pdf.PGraphicsPDF

Any suggestions?! Many thanks in advance :slight_smile:

Can I ask you to provide enough code to reproduce the error?

1 Like
import processing.pdf.*;

void setup() {
  size(200, 200);
  beginRecord(PDF, "test.pdf");

void draw() {
  PGraphicsPDF pdf = (PGraphicsPDF) g;  // Get the renderer

  for(int i=0; i<5; i++) {

Error: “ClassCastException: processing.awt.PGraphicsJava2D cannot be cast to processing.pdf.PGraphicsPDF” on the first line in draw().

Thanks for taking a look!

1 Like

What do you declared g?

Copied that line right from the reference page:

You are not using the PDF library properly. Look at the examples that come with the library, play with them so you understand how PDF Export works.

You can find the examples

  1. select File > Examples from the menus
  2. in the Java Examples window expand the Libraries tab and then the PDF Export tab

g is the graphics renderer used with this sketch and is declared in the PApplet class. It is of type PGraphics so can reference any object of type PGraphics or a child class of PGraphics.

The sketch code above uses the PGraphicsJava2D renderer so g references a PGraphicsJava2D object. If the size() method specified P2D or P3D it would reference a PGraphicsOpenGL object and in JavaFX mode it would reference a PGraphicsFX2D object.


I didn’t know the Examples existed! Very helpful, thank you!

1 Like