public void beginDraw() {
    //    long t0 = System.currentTimeMillis();

    if (document == null) {
      document = new Document(new Rectangle(width, height));
      try {
        if (file != null) {
          // BufferedOutputStream output = new BufferedOutputStream(stream, 16384);
          output = new BufferedOutputStream(new FileOutputStream(file), 16384);

        } else if (output == null) {
          throw new RuntimeException(
              "PGraphicsPDF requires a path " + "for the location of the output file.");
        }
        writer = PdfWriter.getInstance(document, output);
        document.open();
        content = writer.getDirectContent();
        //        template = content.createTemplate(width, height);

      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Problem saving the PDF file.");
      }

      //      System.out.println("beginDraw fonts " + (System.currentTimeMillis() - t));
      g2 = content.createGraphics(width, height, getMapper());
      //      g2 = template.createGraphics(width, height, mapper);
    }
    //    System.out.println("beginDraw " + (System.currentTimeMillis() - t0));
    super.beginDraw();
  }
  public void textFont(PFont which) {
    super.textFont(which);

    // Make sure a native version of the font is available.
    if (textFont.getFont() == null) {
      throw new RuntimeException(
          "Use createFont() instead of loadFont() " + "when drawing text using the PDF library.");
    }
    // Make sure that this is a font that the PDF library can deal with.
    if (!checkFont(which.getName())) {
      System.err.println("Use PGraphicsPDF.listFonts() to get a list of available fonts.");
      throw new RuntimeException(
          "The font “" + which.getName() + "” cannot be used with PDF Export.");
    }
  }
 protected void textLineImpl(char buffer[], int start, int stop, float x, float y) {
   Font font = textFont.getFont();
   if (font == null) {
     throw new RuntimeException(
         "Use createFont() instead of loadFont() " + "when drawing text using the PDF library.");
   } else if (textFont.isStream() && textMode != SHAPE) {
     throw new RuntimeException(
         "Use textMode(SHAPE) with when loading " + ".ttf and .otf files with createFont().");
   } else if (!checkFont(textFont.getName())) {
     System.err.println(
         "Use PGraphicsPDF.listFonts() to get a list of " + "fonts that can be used with PDF.");
     throw new RuntimeException(
         "The font “" + textFont.getName() + "” " + "cannot be used with PDF Export.");
   }
   super.textLineImpl(buffer, start, stop, x, y);
 }