Beispiel #1
0
 /**
  * Method to export charts as PDF files using the defined path.
  *
  * @param path The filename and absolute path.
  * @param chart The JFreeChart object.
  * @param width The width of the PDF file.
  * @param height The height of the PDF file.
  * @param mapper The font mapper for the PDF file.
  * @param title The title of the PDF file.
  * @throws IOException If writing a PDF file fails.
  */
 @SuppressWarnings("deprecation")
 public static void exportPdf(
     String path, JFreeChart chart, int width, int height, FontMapper mapper, String title)
     throws IOException {
   File file = new File(path);
   FileOutputStream pdfStream = new FileOutputStream(file);
   BufferedOutputStream pdfOutput = new BufferedOutputStream(pdfStream);
   Rectangle pagesize = new Rectangle(width, height);
   Document document = new Document();
   document.setPageSize(pagesize);
   document.setMargins(50, 50, 50, 50);
   document.addAuthor("OMSimulationTool");
   document.addSubject(title);
   try {
     PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfOutput);
     document.open();
     PdfContentByte contentByte = pdfWriter.getDirectContent();
     PdfTemplate template = contentByte.createTemplate(width, height);
     Graphics2D g2D = template.createGraphics(width, height, mapper);
     Double r2D = new Rectangle2D.Double(0, 0, width, height);
     chart.draw(g2D, r2D);
     g2D.dispose();
     contentByte.addTemplate(template, 0, 0);
   } catch (DocumentException de) {
     JOptionPane.showMessageDialog(
         null,
         "Failed to write PDF document.\n" + de.getMessage(),
         "Failed",
         JOptionPane.ERROR_MESSAGE);
     de.printStackTrace();
   }
   document.close();
 }
  public static void buildPdf(PrinterJob job, OutputStream os) throws DocumentException {
    Document doc = new Document(getPageSizeFromPaper(job.getPaper()));
    PdfWriter pdfWriter = PdfWriter.getInstance(doc, os);
    doc.open();

    for (int i = 0; job.getNumberOfPages() < 0 || i < job.getNumberOfPages(); i++) {

      if (job.getNumberOfPages() < 0) {
        if (!job.render(DUMMYRENDERSPACE, i)) {
          break;
        }
      }

      PdfContentByte cb = pdfWriter.getDirectContent();
      PdfTemplate pdfTemplate =
          cb.createTemplate(doc.getPageSize().getWidth(), doc.getPageSize().getHeight());
      Graphics2D g2d =
          pdfTemplate.createGraphics(
              doc.getPageSize().getWidth(), doc.getPageSize().getHeight(), new DefaultFontMapper());
      try {
        if (job.render(g2d, i)) {
          g2d.dispose();
          if (i > 0) {
            doc.newPage();
          }
          cb.addTemplate(pdfTemplate, 0, 0);
        } else {
          throw new PrinterException("Unknow error occured.");
        }
      } catch (PrinterException ex) {
        break;
      } finally {

      }
    }
    doc.close();
  }
Beispiel #3
0
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
      Rectangle rect = writer.getBoxSize("art");

      Image imghead = null;
      PdfContentByte cbhead = null;

      try {
        imghead = Image.getInstance("LogoSapito5.png");
        imghead.setAbsolutePosition(0, 0);
        imghead.setAlignment(Image.ALIGN_CENTER);
        imghead.scalePercent(10f);
        cbhead = writer.getDirectContent();
        PdfTemplate tp = cbhead.createTemplate(100, 150);
        tp.addImage(imghead);
        cbhead.addTemplate(tp, 100, 715);

      } catch (BadElementException e) {
        e.printStackTrace();
      } catch (DocumentException e) {
        e.printStackTrace();
      } catch (IOException ex) {
        Logger.getLogger(Cuentaspagar.class.getName()).log(Level.SEVERE, null, ex);
      }

      Phrase headPhraseImg =
          new Phrase(cbhead + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.NORMAL));

      Calendar c1 = Calendar.getInstance();
      Calendar c2 = new GregorianCalendar();
      String dia, mes, annio;
      dia = Integer.toString(c1.get(Calendar.DATE));
      mes = Integer.toString(c1.get(Calendar.MONTH));
      annio = Integer.toString(c1.get(Calendar.YEAR));
      java.util.Date fecha = new Date();
      String fechis = dia + "/" + mes + "/" + annio;
      Paragraph parrafo5 =
          new Paragraph(
              fechis,
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo5),
          rect.getRight(450),
          rect.getTop(-80),
          0);

      Paragraph parrafo7 =
          new Paragraph(
              "Empresa Sapito S.A. de C.V.",
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 16, Font.BOLD, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo7),
          rect.getBottom(250),
          rect.getTop(-60),
          0);

      Paragraph parrafo8 =
          new Paragraph(
              "Cuentas por cobrar",
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo8),
          rect.getBottom(250),
          rect.getTop(-40),
          0);

      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_BOTTOM,
          new Phrase(
              "      _________________________________________________________________________________    "),
          rect.getBorder(),
          rect.getTop(-24),
          0);

      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_BOTTOM,
          new Phrase(
              "      _________________________________________________________________________________    "),
          rect.getBorder(),
          rect.getTop(-20),
          0);

      Paragraph parrafo6 =
          new Paragraph(
              String.format("Pág %d", writer.getPageNumber()),
              FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLACK));
      ColumnText.showTextAligned(
          writer.getDirectContent(),
          Element.ALIGN_CENTER,
          new Phrase(parrafo6),
          rect.getRight(-35),
          rect.getTop(-80),
          0);
    }
  private static TableAndHighlighting createTable(
      PdfWriter docWriter,
      Document doc,
      float sideMargins,
      Dimension scrambleImageSize,
      String[] scrambles,
      Puzzle scrambler,
      HashMap<String, Color> colorScheme,
      String scrambleNumberPrefix,
      boolean forceHighlighting)
      throws DocumentException {
    PdfContentByte cb = docWriter.getDirectContent();

    PdfPTable table = new PdfPTable(3);

    int charsWide = scrambleNumberPrefix.length() + 1 + (int) Math.log10(scrambles.length);
    String wideString = "";
    for (int i = 0; i < charsWide; i++) {
      // M has got to be as wide or wider than the widest digit in our font
      wideString += "M";
    }
    wideString += ".";
    float col1Width = new Chunk(wideString).getWidthPoint();
    // I don't know why we need this, perhaps there's some padding?
    col1Width += 5;

    float availableWidth = doc.getPageSize().getWidth() - sideMargins;
    float scrambleColumnWidth =
        availableWidth - col1Width - scrambleImageSize.width - 2 * SCRAMBLE_IMAGE_PADDING;
    int availableScrambleHeight = scrambleImageSize.height - 2 * SCRAMBLE_IMAGE_PADDING;

    table.setTotalWidth(
        new float[] {
          col1Width, scrambleColumnWidth, scrambleImageSize.width + 2 * SCRAMBLE_IMAGE_PADDING
        });
    table.setLockedWidth(true);

    String longestScramble = "";
    String longestPaddedScramble = "";
    for (String scramble : scrambles) {
      if (scramble.length() > longestScramble.length()) {
        longestScramble = scramble;
      }

      String paddedScramble = padTurnsUniformly(scramble, "M");
      if (paddedScramble.length() > longestPaddedScramble.length()) {
        longestPaddedScramble = paddedScramble;
      }
    }
    // I don't know how to configure ColumnText.fitText's word wrapping characters,
    // so instead, I just replace each character I don't want to wrap with M, which
    // should be the widest character (we're using a monospaced font,
    // so that doesn't really matter), and won't get wrapped.
    char widestCharacter = 'M';
    longestPaddedScramble = longestPaddedScramble.replaceAll("\\S", widestCharacter + "");
    boolean tryToFitOnOneLine = true;
    if (longestPaddedScramble.indexOf("\n") >= 0) {
      // If the scramble contains newlines, then we *only* allow wrapping at the
      // newlines.
      longestPaddedScramble = longestPaddedScramble.replaceAll(" ", "M");
      tryToFitOnOneLine = false;
    }
    boolean oneLine = false;
    Font scrambleFont = null;

    try {
      BaseFont courier = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.EMBEDDED);
      Rectangle availableArea =
          new Rectangle(
              scrambleColumnWidth - 2 * SCRAMBLE_PADDING_HORIZONTAL,
              availableScrambleHeight
                  - SCRAMBLE_PADDING_VERTICAL_TOP
                  - SCRAMBLE_PADDING_VERTICAL_BOTTOM);
      float perfectFontSize =
          fitText(
              new Font(courier),
              longestPaddedScramble,
              availableArea,
              MAX_SCRAMBLE_FONT_SIZE,
              true);
      if (tryToFitOnOneLine) {
        String longestScrambleOneLine = longestScramble.replaceAll(".", widestCharacter + "");
        float perfectFontSizeForOneLine =
            fitText(
                new Font(courier),
                longestScrambleOneLine,
                availableArea,
                MAX_SCRAMBLE_FONT_SIZE,
                false);
        oneLine = perfectFontSizeForOneLine >= MINIMUM_ONE_LINE_FONT_SIZE;
        if (oneLine) {
          perfectFontSize = perfectFontSizeForOneLine;
        }
      }
      scrambleFont = new Font(courier, perfectFontSize, Font.NORMAL);
    } catch (IOException e) {
      l.log(Level.INFO, "", e);
    } catch (DocumentException e) {
      l.log(Level.INFO, "", e);
    }

    boolean highlight = forceHighlighting;
    for (int i = 0; i < scrambles.length; i++) {
      String scramble = scrambles[i];
      String paddedScramble =
          oneLine ? scramble : padTurnsUniformly(scramble, NON_BREAKING_SPACE + "");
      Chunk ch = new Chunk(scrambleNumberPrefix + (i + 1) + ".");
      PdfPCell nthscramble = new PdfPCell(new Paragraph(ch));
      nthscramble.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
      table.addCell(nthscramble);

      Phrase scramblePhrase = new Phrase();
      int nthLine = 1;
      LinkedList<Chunk> lineChunks =
          splitScrambleToLineChunks(paddedScramble, scrambleFont, scrambleColumnWidth);
      if (lineChunks.size() >= MIN_LINES_TO_ALTERNATE_HIGHLIGHTING) {
        highlight = true;
      }

      for (Chunk lineChunk : lineChunks) {
        if (highlight && (nthLine % 2 == 0)) {
          lineChunk.setBackground(HIGHLIGHT_COLOR);
        }
        scramblePhrase.add(lineChunk);
        nthLine++;
      }

      PdfPCell scrambleCell = new PdfPCell(new Paragraph(scramblePhrase));
      // We carefully inserted newlines ourselves to make stuff fit, don't
      // let itextpdf wrap lines for us.
      scrambleCell.setNoWrap(true);
      scrambleCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
      // This shifts everything up a little bit, because I don't like how
      // ALIGN_MIDDLE works.
      scrambleCell.setPaddingTop(-SCRAMBLE_PADDING_VERTICAL_TOP);
      scrambleCell.setPaddingBottom(SCRAMBLE_PADDING_VERTICAL_BOTTOM);
      scrambleCell.setPaddingLeft(SCRAMBLE_PADDING_HORIZONTAL);
      scrambleCell.setPaddingRight(SCRAMBLE_PADDING_HORIZONTAL);
      // We space lines a little bit more here - it still fits in the cell height
      scrambleCell.setLeading(0, 1.1f);
      table.addCell(scrambleCell);

      if (scrambleImageSize.width > 0 && scrambleImageSize.height > 0) {
        PdfTemplate tp =
            cb.createTemplate(
                scrambleImageSize.width + 2 * SCRAMBLE_IMAGE_PADDING,
                scrambleImageSize.height + 2 * SCRAMBLE_IMAGE_PADDING);
        Graphics2D g2 =
            new PdfGraphics2D(tp, tp.getWidth(), tp.getHeight(), new DefaultFontMapper());
        g2.translate(SCRAMBLE_IMAGE_PADDING, SCRAMBLE_IMAGE_PADDING);

        try {
          Svg svg = scrambler.drawScramble(scramble, colorScheme);
          drawSvgToGraphics2D(svg, g2, scrambleImageSize);
        } catch (Exception e) {
          table.addCell("Error drawing scramble: " + e.getMessage());
          l.log(
              Level.WARNING,
              "Error drawing scramble, if you're having font issues, try installing ttf-dejavu.",
              e);
          continue;
        } finally {
          g2.dispose(); // iTextPdf blows up if we do not dispose of this
        }
        PdfPCell imgCell = new PdfPCell(Image.getInstance(tp), true);
        imgCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        imgCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        imgCell.setHorizontalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(imgCell);
      } else {
        table.addCell("");
      }
    }

    TableAndHighlighting tableAndHighlighting = new TableAndHighlighting();
    tableAndHighlighting.table = table;
    tableAndHighlighting.highlighting = highlight;
    return tableAndHighlighting;
  }
  private static void addScrambles(
      PdfWriter docWriter, Document doc, ScrambleRequest scrambleRequest, String globalTitle)
      throws DocumentException, IOException {
    if (scrambleRequest.fmc) {
      Rectangle pageSize = doc.getPageSize();
      for (int i = 0; i < scrambleRequest.scrambles.length; i++) {
        String scramble = scrambleRequest.scrambles[i];
        PdfContentByte cb = docWriter.getDirectContent();
        float LINE_THICKNESS = 0.5f;
        BaseFont bf =
            BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        int bottom = 30;
        int left = 35;
        int right = (int) (pageSize.getWidth() - left);
        int top = (int) (pageSize.getHeight() - bottom);

        int height = top - bottom;
        int width = right - left;

        int solutionBorderTop = bottom + (int) (height * .5);
        int scrambleBorderTop = solutionBorderTop + 40;

        int competitorInfoBottom = top - (int) (height * .15);
        int gradeBottom = competitorInfoBottom - 50;
        int competitorInfoLeft = right - (int) (width * .45);

        int rulesRight = competitorInfoLeft;

        int padding = 5;

        // Outer border
        cb.setLineWidth(2f);
        cb.moveTo(left, top);
        cb.lineTo(left, bottom);
        cb.lineTo(right, bottom);
        cb.lineTo(right, top);

        // Solution border
        cb.moveTo(left, solutionBorderTop);
        cb.lineTo(right, solutionBorderTop);

        // Rules bottom border
        cb.moveTo(left, scrambleBorderTop);
        cb.lineTo(rulesRight, scrambleBorderTop);

        // Rules right border
        cb.lineTo(rulesRight, gradeBottom);

        // Grade bottom border
        cb.moveTo(competitorInfoLeft, gradeBottom);
        cb.lineTo(right, gradeBottom);

        // Competitor info bottom border
        cb.moveTo(competitorInfoLeft, competitorInfoBottom);
        cb.lineTo(right, competitorInfoBottom);

        // Competitor info left border
        cb.moveTo(competitorInfoLeft, gradeBottom);
        cb.lineTo(competitorInfoLeft, top);

        // Solution lines
        int availableSolutionWidth = right - left;
        int availableSolutionHeight = scrambleBorderTop - bottom;
        int lineWidth = 25;
        // int linesX = (availableSolutionWidth/lineWidth + 1)/2;
        int linesX = 10;
        int linesY = (int) Math.ceil(1.0 * WCA_MAX_MOVES_FMC / linesX);

        cb.setLineWidth(LINE_THICKNESS);
        cb.stroke();

        //              int allocatedX = (2*linesX-1)*lineWidth;
        int excessX = availableSolutionWidth - linesX * lineWidth;
        int moveCount = 0;
        solutionLines:
        for (int y = 0; y < linesY; y++) {
          for (int x = 0; x < linesX; x++) {
            if (moveCount >= WCA_MAX_MOVES_FMC) {
              break solutionLines;
            }
            int xPos = left + x * lineWidth + (x + 1) * excessX / (linesX + 1);
            int yPos = solutionBorderTop - (y + 1) * availableSolutionHeight / (linesY + 1);
            cb.moveTo(xPos, yPos);
            cb.lineTo(xPos + lineWidth, yPos);
            moveCount++;
          }
        }

        float UNDERLINE_THICKNESS = 0.2f;
        cb.setLineWidth(UNDERLINE_THICKNESS);
        cb.stroke();

        cb.beginText();
        int availableScrambleSpace = right - left - 2 * padding;
        int scrambleFontSize = 20;
        String scrambleStr = "Scramble: " + scramble;
        float scrambleWidth;
        do {
          scrambleFontSize--;
          scrambleWidth = bf.getWidthPoint(scrambleStr, scrambleFontSize);
        } while (scrambleWidth > availableScrambleSpace);

        cb.setFontAndSize(bf, scrambleFontSize);
        int scrambleY =
            3 + solutionBorderTop + (scrambleBorderTop - solutionBorderTop - scrambleFontSize) / 2;
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, scrambleStr, left + padding, scrambleY, 0);
        cb.endText();

        int availableScrambleWidth = right - rulesRight;
        int availableScrambleHeight = gradeBottom - scrambleBorderTop;
        Dimension dim =
            scrambleRequest.scrambler.getPreferredSize(
                availableScrambleWidth - 2, availableScrambleHeight - 2);
        PdfTemplate tp = cb.createTemplate(dim.width, dim.height);
        Graphics2D g2 = new PdfGraphics2D(tp, dim.width, dim.height, new DefaultFontMapper());

        try {
          Svg svg = scrambleRequest.scrambler.drawScramble(scramble, scrambleRequest.colorScheme);
          drawSvgToGraphics2D(svg, g2, dim);
        } catch (InvalidScrambleException e) {
          l.log(Level.INFO, "", e);
        } finally {
          g2.dispose();
        }

        cb.addImage(
            Image.getInstance(tp),
            dim.width,
            0,
            0,
            dim.height,
            rulesRight + (availableScrambleWidth - dim.width) / 2,
            scrambleBorderTop + (availableScrambleHeight - dim.height) / 2);

        ColumnText ct = new ColumnText(cb);

        int fontSize = 15;
        int marginBottom = 10;
        int offsetTop = 27;
        boolean showScrambleCount = scrambleRequest.scrambles.length > 1;
        if (showScrambleCount) {
          offsetTop -= fontSize + 2;
        }

        cb.beginText();
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            globalTitle,
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        offsetTop += fontSize + 2;
        cb.endText();

        cb.beginText();
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            scrambleRequest.title,
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        cb.endText();

        if (showScrambleCount) {
          cb.beginText();
          offsetTop += fontSize + 2;
          cb.setFontAndSize(bf, fontSize);
          cb.showTextAligned(
              PdfContentByte.ALIGN_CENTER,
              "Scramble " + (i + 1) + " of " + scrambleRequest.scrambles.length,
              competitorInfoLeft + (right - competitorInfoLeft) / 2,
              top - offsetTop,
              0);
          cb.endText();
        }

        offsetTop += fontSize + marginBottom;

        cb.beginText();
        fontSize = 15;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_LEFT,
            "Competitor: __________________",
            competitorInfoLeft + padding,
            top - offsetTop,
            0);
        offsetTop += fontSize + marginBottom;
        cb.endText();

        cb.beginText();

        fontSize = 15;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_LEFT, "WCA ID:", competitorInfoLeft + padding, top - offsetTop, 0);

        cb.setFontAndSize(bf, 19);
        int wcaIdLength = 63;
        cb.showTextAligned(
            PdfContentByte.ALIGN_LEFT,
            "_ _ _ _  _ _ _ _  _ _",
            competitorInfoLeft + padding + wcaIdLength,
            top - offsetTop,
            0);

        offsetTop += fontSize + (int) (marginBottom * 1.8);
        cb.endText();

        cb.beginText();
        fontSize = 11;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            "DO NOT FILL IF YOU ARE THE COMPETITOR",
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        offsetTop += fontSize + marginBottom;
        cb.endText();

        cb.beginText();
        fontSize = 11;
        cb.setFontAndSize(bf, fontSize);
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            "Graded by: _______________ Result: ______",
            competitorInfoLeft + (right - competitorInfoLeft) / 2,
            top - offsetTop,
            0);
        offsetTop += fontSize + marginBottom;
        cb.endText();

        cb.beginText();
        cb.setFontAndSize(bf, 25f);
        int MAGIC_NUMBER = 40; // kill me now
        cb.showTextAligned(
            PdfContentByte.ALIGN_CENTER,
            "Fewest Moves",
            left + (competitorInfoLeft - left) / 2,
            top - MAGIC_NUMBER,
            0);
        cb.endText();

        com.itextpdf.text.List rules = new com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED);
        rules.add("Notate your solution by writing one move per bar.");
        rules.add("To delete moves, clearly erase/blacken them.");
        rules.add("Face moves F, B, R, L, U, and D are clockwise.");
        rules.add("Rotations x, y, and z follow R, U, and F.");
        rules.add("' inverts a move; 2 doubles a move. (e.g.: U', U2)");
        rules.add("w makes a face move into two layers. (e.g.: Uw)");
        rules.add("A [lowercase] move is a cube rotation. (e.g.: [u])");

        ct.addElement(rules);
        int rulesTop = competitorInfoBottom + 55;
        ct.setSimpleColumn(
            left + padding,
            scrambleBorderTop,
            competitorInfoLeft - padding,
            rulesTop,
            0,
            Element.ALIGN_LEFT);
        ct.go();

        rules = new com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED);
        rules.add("You have 1 hour to find a solution.");
        rules.add("Your solution length will be counted in OBTM.");
        int maxMoves = WCA_MAX_MOVES_FMC;
        rules.add("Your solution must be at most " + maxMoves + " moves, including rotations.");
        rules.add(
            "Your solution must not be directly derived from any part of the scrambling algorithm.");
        ct.addElement(rules);
        MAGIC_NUMBER = 150; // kill me now
        ct.setSimpleColumn(
            left + padding,
            scrambleBorderTop,
            rulesRight - padding,
            rulesTop - MAGIC_NUMBER,
            0,
            Element.ALIGN_LEFT);
        ct.go();

        doc.newPage();
      }
    } else {
      Rectangle pageSize = doc.getPageSize();

      float sideMargins = 100 + doc.leftMargin() + doc.rightMargin();
      float availableWidth = pageSize.getWidth() - sideMargins;
      float vertMargins = doc.topMargin() + doc.bottomMargin();
      float availableHeight = pageSize.getHeight() - vertMargins;
      if (scrambleRequest.extraScrambles.length > 0) {
        availableHeight -= 20; // Yeee magic numbers. This should make space for the headerTable.
      }
      int scramblesPerPage =
          Math.min(MAX_SCRAMBLES_PER_PAGE, scrambleRequest.getAllScrambles().size());
      int maxScrambleImageHeight =
          (int) (availableHeight / scramblesPerPage - 2 * SCRAMBLE_IMAGE_PADDING);

      int maxScrambleImageWidth =
          (int)
              (availableWidth / 2); // We don't let scramble images take up more than half the page
      if (scrambleRequest.scrambler.getShortName().equals("minx")) {
        // TODO - If we allow the megaminx image to be too wide, the
        // megaminx scrambles get really tiny. This tweak allocates
        // a more optimal amount of space to the scrambles. This is possible
        // because the scrambles are so uniformly sized.
        maxScrambleImageWidth = 190;
      }

      Dimension scrambleImageSize =
          scrambleRequest.scrambler.getPreferredSize(maxScrambleImageWidth, maxScrambleImageHeight);

      // First do a dry run just to see if any scrambles require highlighting.
      // Then do the real run, and force highlighting on every scramble
      // if any scramble required it.
      boolean forceHighlighting = false;
      for (boolean dryRun : new boolean[] {true, false}) {
        String scrambleNumberPrefix = "";
        TableAndHighlighting tableAndHighlighting =
            createTable(
                docWriter,
                doc,
                sideMargins,
                scrambleImageSize,
                scrambleRequest.scrambles,
                scrambleRequest.scrambler,
                scrambleRequest.colorScheme,
                scrambleNumberPrefix,
                forceHighlighting);
        if (dryRun) {
          if (tableAndHighlighting.highlighting) {
            forceHighlighting = true;
            continue;
          }
        } else {
          doc.add(tableAndHighlighting.table);
        }

        if (scrambleRequest.extraScrambles.length > 0) {
          PdfPTable headerTable = new PdfPTable(1);
          headerTable.setTotalWidth(new float[] {availableWidth});
          headerTable.setLockedWidth(true);

          PdfPCell extraScramblesHeader = new PdfPCell(new Paragraph("Extra scrambles"));
          extraScramblesHeader.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
          extraScramblesHeader.setPaddingBottom(3);
          headerTable.addCell(extraScramblesHeader);
          if (!dryRun) {
            doc.add(headerTable);
          }

          scrambleNumberPrefix = "E";
          TableAndHighlighting extraTableAndHighlighting =
              createTable(
                  docWriter,
                  doc,
                  sideMargins,
                  scrambleImageSize,
                  scrambleRequest.extraScrambles,
                  scrambleRequest.scrambler,
                  scrambleRequest.colorScheme,
                  scrambleNumberPrefix,
                  forceHighlighting);
          if (dryRun) {
            if (tableAndHighlighting.highlighting) {
              forceHighlighting = true;
              continue;
            }
          } else {
            doc.add(extraTableAndHighlighting.table);
          }
        }
      }
    }
    doc.newPage();
  }
  /**
   * Creates a PDF document.
   *
   * @param filename the path to the new PDF document
   * @throws DocumentException
   * @throws IOException
   * @throws DocumentException
   * @throws IOException
   */
  public void createPdf(String filename) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    document.open();
    PdfContentByte canvas = writer.getDirectContent();
    canvas.saveState();
    for (int i = 25; i > 0; i--) {
      canvas.setLineWidth((float) i / 10);
      canvas.moveTo(50, 806 - (5 * i));
      canvas.lineTo(320, 806 - (5 * i));
      canvas.stroke();
    }
    canvas.restoreState();

    canvas.moveTo(350, 800);
    canvas.lineTo(350, 750);
    canvas.moveTo(540, 800);
    canvas.lineTo(540, 750);
    canvas.stroke();

    canvas.saveState();
    canvas.setLineWidth(8);
    canvas.setLineCap(PdfContentByte.LINE_CAP_BUTT);
    canvas.moveTo(350, 790);
    canvas.lineTo(540, 790);
    canvas.stroke();
    canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
    canvas.moveTo(350, 775);
    canvas.lineTo(540, 775);
    canvas.stroke();
    canvas.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
    canvas.moveTo(350, 760);
    canvas.lineTo(540, 760);
    canvas.stroke();
    canvas.restoreState();

    canvas.saveState();
    canvas.setLineWidth(8);
    canvas.setLineJoin(PdfContentByte.LINE_JOIN_MITER);
    canvas.moveTo(387, 700);
    canvas.lineTo(402, 730);
    canvas.lineTo(417, 700);
    canvas.stroke();
    canvas.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
    canvas.moveTo(427, 700);
    canvas.lineTo(442, 730);
    canvas.lineTo(457, 700);
    canvas.stroke();
    canvas.setLineJoin(PdfContentByte.LINE_JOIN_BEVEL);
    canvas.moveTo(467, 700);
    canvas.lineTo(482, 730);
    canvas.lineTo(497, 700);
    canvas.stroke();
    canvas.restoreState();

    canvas.saveState();
    canvas.setLineWidth(3);
    canvas.moveTo(50, 660);
    canvas.lineTo(320, 660);
    canvas.stroke();
    canvas.setLineDash(6, 0);
    canvas.moveTo(50, 650);
    canvas.lineTo(320, 650);
    canvas.stroke();
    canvas.setLineDash(6, 3);
    canvas.moveTo(50, 640);
    canvas.lineTo(320, 640);
    canvas.stroke();
    canvas.setLineDash(15, 10, 5);
    canvas.moveTo(50, 630);
    canvas.lineTo(320, 630);
    canvas.stroke();
    float[] dash1 = {10, 5, 5, 5, 20};
    canvas.setLineDash(dash1, 5);
    canvas.moveTo(50, 620);
    canvas.lineTo(320, 620);
    canvas.stroke();
    float[] dash2 = {9, 6, 0, 6};
    canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
    canvas.setLineDash(dash2, 0);
    canvas.moveTo(50, 610);
    canvas.lineTo(320, 610);
    canvas.stroke();
    canvas.restoreState();

    PdfTemplate hooks = canvas.createTemplate(300, 120);
    hooks.setLineWidth(8);
    hooks.moveTo(46, 50);
    hooks.lineTo(65, 80);
    hooks.lineTo(84, 50);
    hooks.stroke();
    hooks.moveTo(87, 50);
    hooks.lineTo(105, 80);
    hooks.lineTo(123, 50);
    hooks.stroke();
    hooks.moveTo(128, 50);
    hooks.lineTo(145, 80);
    hooks.lineTo(162, 50);
    hooks.stroke();
    hooks.moveTo(169, 50);
    hooks.lineTo(185, 80);
    hooks.lineTo(201, 50);
    hooks.stroke();
    hooks.moveTo(210, 50);
    hooks.lineTo(225, 80);
    hooks.lineTo(240, 50);
    hooks.stroke();

    canvas.saveState();
    canvas.setMiterLimit(2);
    canvas.addTemplate(hooks, 300, 600);
    canvas.restoreState();

    canvas.saveState();
    canvas.setMiterLimit(2.1f);
    canvas.addTemplate(hooks, 300, 550);
    canvas.restoreState();

    document.close();
  }