# Accessing the contextual PGraphics explicitly when using beginRecord()

**URL:** <https://discourse.processing.org/t/accessing-the-contextual-pgraphics-explicitly-when-using-beginrecord/42189>\
**Category:** Coding Questions\
**Created:** [June 11, 2023, 5:03pm UTC](https://discourse.processing.org/t/accessing-the-contextual-pgraphics-explicitly-when-using-beginrecord/42189 "2023-06-11T17:03:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewcooke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andrewcooke/32/18406_2.png) [@andrewcooke](https://discourse.processing.org/u/andrewcooke)\
**Post date:** [June 11, 2023, 5:03pm UTC](https://discourse.processing.org/t/accessing-the-contextual-pgraphics-explicitly-when-using-beginrecord/42189/1 "2023-06-11T17:03:29Z")

</div>

I am passing `this.g` to some separate Java code, and things are working just fine with the data displayed to the screen, but when I try to copy to PDF using `beginRecord()` and `endRecord()` I don’t see any of the output that is generated via calls to `this.g`.

How do I fix this?

For example:

```auto
import processing.pdf.PGraphicsPDF;

void settings() {
  size(200, 200, P2D);
}

void setup() {  
  noLoop();
}

void draw() {
  beginRecord(PDF, "test.pdf");
  line(0, 0, 100, 100);
  new Test().line(this.g);
  save("test.png");
  endRecord();
}

```

where `Test.java` contains:

```auto
import processing.core.PGraphics;

public class Test {

    public void line(PGraphics g) {
        g.line(200, 0, 100, 100);
    }
    
}

```

will show TWO lines on the display and in the PNG file, but only ONE in the PDF (the second line, generated in the Java code, is missing).

Thanks!

---

<div class="post-metadata">

**Author:** ![andrewcooke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andrewcooke/32/18406_2.png) [@andrewcooke](https://discourse.processing.org/u/andrewcooke)\
**Post date:** [June 11, 2023, 6:49pm UTC](https://discourse.processing.org/t/accessing-the-contextual-pgraphics-explicitly-when-using-beginrecord/42189/2 "2023-06-11T18:49:10Z")

</div>

Just to add to this… the call to `beginRecord()` returns a PGraphics instance. If I use that, instead of `this.g`, in the call to the Java code, then I see the reverse: TWO lines are present in the PDF, but only one on the screen and in the PNG! Is there any way to have both lines present in all the different outputs (without repeating the calls twice, obviously)?

(Fundamentally, I guess I am asking how the recording process is implemented. I can’t find it documented anywhere).

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 11, 2023, 7:18pm UTC](https://discourse.processing.org/t/accessing-the-contextual-pgraphics-explicitly-when-using-beginrecord/42189/3 "2023-06-11T19:18:54Z")

</div>

Hello @andrewcooke,

It works with this:

```auto
  beginRaw(PDF, "test.pdf");
  //...
  endRaw(

```

There is a note about P2D here in the `PDF Files from 3D Geometry (With Screen Display) section:`

> **[PDF Export / Libraries](https://processing.org/reference/libraries/pdf/index.html)**
>
> Create PDF files. These vector graphics files can be scaled to any size and printed at high resolutions.

The source for the PDF library is here to explore:

> **[processing4/java/libraries at main · processing/processing4](https://github.com/processing/processing4/tree/main/java/libraries)**
>
> main/java/libraries

`:)`

---

<div class="post-metadata">

**Author:** ![andrewcooke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/andrewcooke/32/18406_2.png) [@andrewcooke](https://discourse.processing.org/u/andrewcooke)\
**Post date:** [June 11, 2023, 7:34pm UTC](https://discourse.processing.org/t/accessing-the-contextual-pgraphics-explicitly-when-using-beginrecord/42189/4 "2023-06-11T19:34:07Z")

</div>

just to be clear for anyone else, because it took me a minute and i tested it - just replace “Record” with “Raw” in the example above and things work fine.

THANKS!

edit: well, when used on my actual plot, it’s taking forever and a lot of cpu, but i guess it’s working…
