# Multiple page PDF and draw to screen?

**URL:** <https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719>\
**Category:** Coding Questions\
**Created:** [September 21, 2018, 6:39pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719 "2018-09-21T18:39:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stirman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stirman/32/1321_2.png) [@stirman](https://discourse.processing.org/u/stirman)\
**Post date:** [September 21, 2018, 6:39pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/1 "2018-09-21T18:39:46Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [September 22, 2018, 4:18pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/2 "2018-09-22T16:18:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stirman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stirman/32/1321_2.png) [@stirman](https://discourse.processing.org/u/stirman)\
**Post date:** [September 22, 2018, 5:05pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/3 "2018-09-22T17:05:53Z")

</div>

```auto
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++) {
    line(i,i,5-i,5-i);    
    pdf.nextPage();
  }
  endRecord();
  exit();
}

```

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

Thanks for taking a look!

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [September 22, 2018, 11:34pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/4 "2018-09-22T23:34:11Z")

</div>

What do you declared `g`?

---

<div class="post-metadata">

**Author:** ![stirman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stirman/32/1321_2.png) [@stirman](https://discourse.processing.org/u/stirman)\
**Post date:** [September 22, 2018, 11:50pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/5 "2018-09-22T23:50:40Z")

</div>

Copied that line right from the reference page: [https://processing.org/reference/libraries/pdf/index.html](https://processing.org/reference/libraries/pdf/index.html)

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 23, 2018, 9:31am UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/6 "2018-09-23T09:31:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 23, 2018, 10:34am UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/7 "2018-09-23T10:34:18Z")

</div>

> [@tony](#):
>
> What do you declared `g` ?

`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.

---

<div class="post-metadata">

**Author:** ![stirman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stirman/32/1321_2.png) [@stirman](https://discourse.processing.org/u/stirman)\
**Post date:** [September 24, 2018, 9:43pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/8 "2018-09-24T21:43:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Freek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/freek/32/17437_2.png) [@Freek](https://discourse.processing.org/u/Freek)\
**Post date:** [November 23, 2022, 5:04pm UTC](https://discourse.processing.org/t/multiple-page-pdf-and-draw-to-screen/3719/9 "2022-11-23T17:04:10Z")

</div>

The `PGraphicsPDF` object gets returned by the `beginRecord` method:

```auto
import processing.pdf.*;

PGraphicsPDF graphicsPDF;

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

void draw() {
  for(int i=0; i<5; i++) {
    line(i,i,5-i,5-i);    
    graphicsPDF.nextPage();
  }
  endRecord();
  exit();
}

```
