Wednesday, July 25, 2007

Specifying PDF page size in Processing

I've always been wondering if there's no easier way than trial & error to figure out the correct dimensions one has to specify in the size() command to match a certain paper size for the generated PDF. Today I finally had a quick look at the source of the underlying iText library on which Processing's PDF wrapper is built. As expected it actually includes a convenience class which has presets for all common page formats. This is great, but to make this even slightly more user-friendly you can take this snippet and keep it:
* Convenience method to be used instead of the normal size() command.
* Creates window matched to a given paper size
* @param r a predefined constant of the iText PageSize class
* @param isLandscape true, if you want landscape orientation
* @param renderer class name of the Processing renderer to use
void pageSize(com.lowagie.text.Rectangle r, boolean isLandscape, String renderer) {
if (isLandscape) {
} else {
And here're a couple of examples how this snippet would be used (make sure you import the PDF library):
void setup() {
// create window @ A3 landscape using OPENGL

void setup() {
// create window @ US Letter size, portrait using default renderer