Saving specific frames into one PDF, using frameCount and array

Hi, I am new to Processing and using the examples in the PDF library I am trying to write a code for one of my artworks. I need to save into one PDF a particular selection of frames.

I have adapted the code for the example “PDF Multiple pages” and managed to write something that does the trick.
I’ll add the code below.

My question: You can see from the example that I have created an array with the frame count numbers where I would like to have a new page in my PDF.
What I can’t work out is how to change “fc[5]” on line 31

else if ( frameCount == fc[5] )

into a general test that asks the question: if the frameCount is ANY of the integers listed in the array fc then do pdf.nextPage.
So I would like it to cycle through all the values in the array. I have tried to use the “for” loop but had no success.

My code:

import processing.pdf.*;
PFont font;
int [] fc = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }; // array of framecounts to save in PDF

void setup() {
  size(400, 400, PDF, "filename02_##.pdf"); 
}

void draw() {
  
  line(0, 0, frameCount * 4, height);
  fill(0);
  textSize(8);
  text( frameCount, frameCount*4, (height-10)); 
  println(frameCount);
    
  PGraphicsPDF pdf = (PGraphicsPDF) g;  // Get the renderer
  

  // this creates 5 pages, each with all frames up to a frame count
  if (frameCount == fc[9]) {
    println((frameCount)+ " EXIT" );
    exit();
  } else if ( frameCount == fc[5] ) {//this records frames 1-8 on one page. INSTEAD OF typing fc[5] how can I cycle through the values of the array?
    pdf.nextPage ();
    } else if ( frameCount == fc[6] ) { //this records frames 9-13 on one page
    pdf.nextPage ();
    } else if ( frameCount == fc[7] ) {//this records frames 14-21 on one page
    pdf.nextPage ();
    } else if ( frameCount == fc[8] ) { //this records frame 22-34 
    pdf.nextPage (); // and the last page records frame 35-98, making it 5 page PDF.
    
  } 
}

Many thanks for your time!

1 Like

If I understand correctly, I would change:

for the following:

else if ( inArray(frameCount, fc[5]) )

where

boolean inArray(int frmCtr, int[] newPageFrameArray){

   for(int i=0; i<newPageFrameArray).length; i++){
      if(frmCtr==newPageFrameArray[i]){
        return true;
      }
   }

   return false;
}

As an altrnative, you can use IntList instead of an array and then you can use the method hasValue() to check if the list contains the frame number you want to check. More in the IntList reference.

Kf

2 Likes

Hi @kfrajer thanks for the reply. I have not yet come across the IntList but that looks that that is just what I need. I’ll check it out and let you know.
W.

Hi @kfrajer, it worked out beautifully!
Thanks for pointing out the IntList option!
W.

//Multiple Pages (No Screen Display)

/*BASED ON THE EXAMPLE OF PDF Multiple pages:
Using Intlist for the frames to be saved in the PDF


*/
import processing.pdf.*;
PFont font;
IntList FibonacciList;

void setup() {
  FibonacciList = new IntList (1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144);
  
  println (FibonacciList);
  size(400, 400, PDF, "filename03_##.pdf"); 
}

void draw() {
  
  line(0, 0, frameCount *2, height);
  fill(0);
  textSize(8);
  text( frameCount, frameCount *2.5, (height-10)-(frameCount*2.5)); 
  println(frameCount);
    
  PGraphicsPDF pdf = (PGraphicsPDF) g;  // Get the renderer
  
  if (frameCount == FibonacciList.max() ) {  // exit at end of list
    println((frameCount)+ " EXIT" );
    exit();
  } else if (FibonacciList.hasValue(frameCount)) { // check if a particular frame count is part of the list
      pdf.nextPage (); 
  } 
}

2 Likes