@Override
    public void onEndPage(PdfWriter writer, Document document) {
      try {
        Rectangle page = document.getPageSize();

        getHeaderFooter(document);
        // Add page header
        header.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        header.writeSelectedRows(
            0, -1, document.leftMargin(), page.getHeight() - 10, writer.getDirectContent());

        // Add page footer
        footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        footer.writeSelectedRows(
            0, -1, document.leftMargin(), document.bottomMargin() - 5, writer.getDirectContent());

        // Add page border

        int bmargin = 8; // border margin
        PdfContentByte cb = writer.getDirectContent();
        cb.rectangle(
            bmargin, bmargin, page.getWidth() - bmargin * 2, page.getHeight() - bmargin * 2);
        cb.setColorStroke(Color.LIGHT_GRAY);
        cb.stroke();

      } catch (JSONException e) {
        Logger.getLogger(ExportProjectReportServlet.class.getName()).log(Level.SEVERE, null, e);
        throw new ExceptionConverter(e);
      }
    }
Exemplo n.º 2
0
 /**
  * Change the textMode() to either SHAPE or MODEL. <br>
  * This resets all renderer settings, and should therefore be called <EM>before</EM> any other
  * commands that set the fill() or the textFont() or anything. Unlike other renderers, use
  * textMode() directly after the size() command.
  */
 public void textMode(int mode) {
   if (textMode != mode) {
     if (mode == SHAPE) {
       g2.dispose();
       g2 = content.createGraphicsShapes(width, height);
     } else if (mode == MODEL) {
       g2.dispose();
       g2 = content.createGraphics(width, height, mapper);
       //        g2 = template.createGraphics(width, height, mapper);
     } else if (mode == SCREEN) {
       throw new RuntimeException("textMode(SCREEN) not supported with PDF");
     } else {
       throw new RuntimeException("That textMode() does not exist");
     }
   }
 }
Exemplo n.º 3
0
  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();
  }
Exemplo n.º 4
0
  /** Call to explicitly go to the next page from within a single draw(). */
  public void nextPage() {
    PStyle savedStyle = getStyle();
    g2.dispose();

    try {
      //    writer.setPageEmpty(false);  // maybe useful later
      document.newPage(); // is this bad if no addl pages are made?
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (textMode == SHAPE) {
      g2 = content.createGraphicsShapes(width, height);
    } else if (textMode == MODEL) {
      g2 = content.createGraphics(width, height, mapper);
    }
    style(savedStyle);

    // should there be a beginDraw/endDraw in here?
  }
Exemplo n.º 5
0
  public void readAll() throws IOException, DocumentException {

    cb.saveState();
    java.awt.Graphics2D g2 = cb.createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
    try {
      PAContext context =
          new PAContext(
              (Graphics2D) g2,
              new Dimension((int) PageSize.A4.width(), (int) PageSize.A4.height()));
      context.draw(new BufferedInputStream(in));
      // ( (Graphics2D) backBuffer.getGraphics()).dispose();
      in.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    } catch (PainterException ex) {
      ex.printStackTrace();
    }

    cb.restoreState();
  }