Ejemplo n.º 1
0
  /**
   * This closes the pdf file created by createPDF, after you have written things to g2D.
   *
   * @param oar the object[] returned from createPdf
   * @throwsException if trouble
   */
  public static void closePdf(Object oar[]) throws Exception {
    Graphics2D g2D = (Graphics2D) oar[0];
    Document document = (Document) oar[1];
    PdfContentByte pdfContentByte = (PdfContentByte) oar[2];
    PdfTemplate pdfTemplate = (PdfTemplate) oar[3];

    g2D.dispose();

    // center it
    if (verbose)
      String2.log(
          "SgtUtil.closePdf"
              + " left="
              + document.left()
              + " right="
              + document.right()
              + " bottom="
              + document.bottom()
              + " top="
              + document.top()
              + " template.width="
              + pdfTemplate.getWidth()
              + " template.height="
              + pdfTemplate.getHeight());
    // x device = ax user + by user + e
    // y device = cx user + dy user + f
    pdfContentByte.addTemplate(
        pdfTemplate, // a,b,c,d,e,f      //x,y location in points
        0.5f,
        0,
        0,
        0.5f,
        document.left() + (document.right() - document.left() - pdfTemplate.getWidth() / 2) / 2,
        document.bottom() + (document.top() - document.bottom() - pdfTemplate.getHeight() / 2) / 2);

    /*
    //if boundingBox is small, center it
    //if boundingBox is large, shrink and center it
    //document.left/right/top/bottom include 1/2" margins
    float xScale = (document.right() - document.left())   / pdfTemplate.getWidth();
    float yScale = (document.top()   - document.bottom()) / pdfTemplate.getHeight();
    float scale = Math.min(Math.min(xScale, yScale), 1);
    float xSize = pdfTemplate.getWidth()  / scale;
    float ySize = pdfTemplate.getHeight() / scale;
    //x device = ax user + by user + e
    //y device = cx user + dy user + f
    pdfContentByte.addTemplate(pdfTemplate, //a,b,c,d,e,f
        scale, 0, 0, scale,
        document.left()   + (document.right() - document.left()   - xSize) / 2,
        document.bottom() + (document.top()   - document.bottom() - ySize) / 2);
    */

    document.close();
  }
Ejemplo n.º 2
0
  /**
   * Método que dibuja el número de página.
   *
   * @param writer Creador de documentos.
   * @param document Documento del informe.
   */
  public void drawPageNumber(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();

    // Número de página
    String text =
        new StringBuffer()
            .append("Página ")
            .append(writer.getPageNumber())
            .append(" de ")
            .toString();

    float textSize = HELVETICA.getWidthPoint(text, 8);
    float textBase = document.bottom() - 20;

    cb.beginText();
    cb.setColorFill(FOOTER_COLOR);
    cb.setFontAndSize(HELVETICA, 8);
    cb.setTextMatrix(document.right() - textSize - 20, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - 20, textBase);

    cb.restoreState();
  }
Ejemplo n.º 3
0
  /**
   * Método que dibuja los bordes de la página.
   *
   * @param writer Creador de documentos.
   * @param document Documento del informe.
   */
  protected void drawBorders(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();

    // Dibujar el borde de la página
    cb.setColorStroke(BORDER_COLOR);
    cb.setLineWidth(0.5f);
    cb.rectangle(
        document.left(),
        document.bottom(),
        document.right() - document.left(),
        document.top() - document.bottom());
    cb.stroke();

    cb.restoreState();
  }
Ejemplo n.º 4
0
  /**
   * Método que dibuja el pie de la página.
   *
   * @param writer Creador de documentos.
   * @param document Documento del informe.
   */
  private void drawFooter(PdfWriter writer, Document document) {
    try {
      PdfContentByte cb = writer.getDirectContent();
      cb.saveState();

      PdfPTable table = new PdfPTable(2);
      table.setTotalWidth(new float[] {70, document.right() - document.left() - 70});
      table.setLockedWidth(true);
      table.setHorizontalAlignment(Element.ALIGN_CENTER);
      table.getDefaultCell().setBorder(Rectangle.BOX);
      table.getDefaultCell().setBorderWidth(0.1F);
      table.getDefaultCell().setBorderColor(BORDER_COLOR);
      table.getDefaultCell().setBackgroundColor(BACKGROUND_COLOR);
      table.getDefaultCell().setFixedHeight(45);
      table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
      table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

      /*
       * Image logo = Image.getInstance(FOOTER_IMAGE_URL);
       * logo.setBorder(Rectangle.BOX); logo.setBorderWidth(0.1F);
       * logo.setBorderColor(BORDER_COLOR); table.addCell(logo);
       */
      table.addCell("");

      PdfPTable tableInfo = new PdfPTable(1);
      tableInfo.setTotalWidth(document.right() - document.left() - 75);
      tableInfo.setLockedWidth(true);
      tableInfo.setHorizontalAlignment(Element.ALIGN_CENTER);
      tableInfo.getDefaultCell().setBorder(Rectangle.NO_BORDER);
      tableInfo.getDefaultCell().setFixedHeight(20);
      tableInfo.getDefaultCell().setPadding(5);
      tableInfo.addCell(
          new Phrase(new Chunk(DateUtils.EXTENDED_DATE_FORMATTER.format(new Date()), FOOTER_FONT)));

      PdfPCell tableInfoCell = new PdfPCell(tableInfo.getDefaultCell());
      tableInfoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
      tableInfoCell.setVerticalAlignment(Element.ALIGN_BOTTOM);
      tableInfoCell.setPhrase(new Phrase(new Chunk(DIRECCION, FOOTER_ITALIC_FONT)));
      tableInfo.addCell(tableInfoCell);
      table.addCell(tableInfo);

      table.writeSelectedRows(0, -1, document.left(), document.bottom() - 5, cb);

      cb.restoreState();
    } catch (Exception e) {
      throw new ExceptionConverter(e);
    }
  }
  public void onEndPage(PdfWriter pdfWriter, Document document) {
    pageNum++; // 页面计数
    if (needNotFootAndHead && pageNum >= needNotFootAndHeadPageNum) {
      return;
    }

    PdfContentByte pdfContByte = pdfWriter.getDirectContent();
    pdfContByte.saveState();
    pdfContByte.setGState(pdfGState);
    pdfContByte.setFontAndSize(baseFont, 48);
    pdfContByte.endText();

    Image barCodeImge = code39.createImageWithBarcode(pdfContByte, null, null);

    try {
      pdfContByte.addImage(
          headerImage,
          headerImage.getWidth() / 2,
          0,
          0,
          headerImage.getHeight() / 2,
          document.left() + 65,
          document.top() + 10);
      pdfContByte.addImage(
          barCodeImge,
          barCodeImge.getWidth(),
          0,
          0,
          barCodeImge.getHeight(),
          document.left() + 35,
          document.bottom() - 26);
    } catch (Exception ex) {
      throw new ExceptionConverter(ex);
    }
    pdfContByte.restoreState();
    pdfContByte.saveState();

    String headFootText = "保险合同号:" + this.contNo;
    float headTextPos = document.top() + 10;
    pdfContByte.beginText();
    pdfContByte.setFontAndSize(baseFont, 10);
    pdfContByte.setTextMatrix(document.right() - 175, headTextPos);
    pdfContByte.showText(headFootText);
    pdfContByte.endText();
    pdfContByte.saveState();

    //		cell.setBorder(Rectangle.BOTTOM);

    //		String metLife = "中美大都会人寿保险有限公司";
    //		float footTextPos = document.bottom()-12;
    //		pdfContByte.beginText();
    //		pdfContByte.setFontAndSize(baseFont, 7);
    //		pdfContByte.setTextMatrix(document.left()+50, footTextPos);
    //		pdfContByte.showText(metLife);
    //		pdfContByte.endText();
    //		pdfContByte.saveState();

    String shanghaimetLife = "中美联泰大都会人寿保险有限公司";
    float shanghaifootText = document.bottom();
    pdfContByte.beginText();
    pdfContByte.setFontAndSize(baseFont, 7);
    pdfContByte.setTextMatrix(document.left() + 35, shanghaifootText);
    pdfContByte.showText(shanghaimetLife);
    pdfContByte.endText();
    pdfContByte.saveState();

    String shanghaimetLifeAddress = "                  联系地址:上海市黄浦区黄陂北路227号中区广场11楼";
    float shanghaifootTextAddPos = document.bottom();
    pdfContByte.beginText();
    pdfContByte.setFontAndSize(baseFont, 7);
    pdfContByte.setTextMatrix(document.right() - 190, shanghaifootTextAddPos);
    pdfContByte.showText(shanghaimetLifeAddress);
    pdfContByte.endText();
    pdfContByte.saveState();

    String metLifeAddress = "北京东城区东长安街一号东方广场东方经贸城东二办公楼12层";
    float footTextAddPos = document.bottom() - 8;
    pdfContByte.beginText();
    pdfContByte.setFontAndSize(baseFont, 7);
    pdfContByte.setTextMatrix(document.right() - 190, footTextAddPos);
    pdfContByte.showText(metLifeAddress);
    pdfContByte.endText();
    pdfContByte.saveState();

    String metLifeCustSer = "客户服务热线:400 818 8168";
    float footTextCSPos = document.bottom() - 16;
    pdfContByte.beginText();
    pdfContByte.setFontAndSize(baseFont, 7);
    pdfContByte.setTextMatrix(document.right() - 85, footTextCSPos);
    pdfContByte.showText(metLifeCustSer);
    pdfContByte.endText();
    pdfContByte.saveState();

    String metLifeSite = "http://www.metlife.com.cn/";
    float footTextSTPos = document.bottom() - 24;
    pdfContByte.beginText();
    pdfContByte.setFontAndSize(baseFont, 7);
    pdfContByte.setTextMatrix(document.right() - 75, footTextSTPos);
    pdfContByte.showText(metLifeSite);
    pdfContByte.endText();
    pdfContByte.saveState();

    //		String pageNumText = "第 " + pdfWriter.getPageNumber() + " 页";
    String pageNumText = String.valueOf(pdfWriter.getPageNumber());
    float textBase = document.bottom() - 2;
    pdfContByte.beginText();
    pdfContByte.setFontAndSize(baseFont, 9);
    pdfContByte.setTextMatrix(document.left() + 285, textBase);
    pdfContByte.showText(pageNumText);
    pdfContByte.endText();
    pdfContByte.saveState();
  }