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();
 }
Beispiel #2
0
 /**
  * Reads the WMF into a template.
  *
  * @param template the template to read to
  * @throws IOException on error
  * @throws DocumentException on error
  */
 public void readWMF(PdfTemplate template) throws IOException, DocumentException {
   setTemplateData(template);
   template.setWidth(getWidth());
   template.setHeight(getHeight());
   InputStream is = null;
   try {
     if (rawData == null) {
       is = url.openStream();
     } else {
       is = new java.io.ByteArrayInputStream(rawData);
     }
     MetaDo meta = new MetaDo(is, template);
     meta.readAll();
   } finally {
     if (is != null) {
       is.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 #4
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;
  }
  /**
   * 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();
  }