Example #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();
  }
  /**
   * 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();
  }
  /**
   * Método que dibuja la cabecera de la página.
   *
   * @param writer Creador de documentos.
   * @param document Documento del informe.
   */
  protected void drawHeader(PdfWriter writer, Document document) {
    try {
      PdfContentByte cb = writer.getDirectContent();
      cb.saveState();

      PdfPTable tableExt = new PdfPTable(1);
      tableExt.setTotalWidth(document.right() - document.left());
      tableExt.setLockedWidth(true);
      tableExt.setHorizontalAlignment(Element.ALIGN_CENTER);
      tableExt.getDefaultCell().setBorder(Rectangle.BOX);
      tableExt.getDefaultCell().setBorderWidth(0.1F);
      tableExt.getDefaultCell().setBorderColor(BORDER_COLOR);
      tableExt.getDefaultCell().setFixedHeight(69);
      tableExt.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
      tableExt.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

      PdfPTable tableInt = new PdfPTable(3);
      tableInt.setTotalWidth(new float[] {108, document.right() - document.left() - 222, 108});
      tableInt.setLockedWidth(true);
      tableInt.setHorizontalAlignment(Element.ALIGN_CENTER);
      tableInt.getDefaultCell().setBorder(Rectangle.TOP | Rectangle.BOTTOM);
      tableInt.getDefaultCell().setBorderWidth(1);
      tableInt.getDefaultCell().setBorderColor(Color.BLUE);
      tableInt.getDefaultCell().setFixedHeight(69);
      tableInt.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
      tableInt.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

      Image logo = null;
      try {
        logo = Image.getInstance(ORGANIZACION_IMAGE_URL);
      } catch (Exception e) {
        logo = ConfiguracionArchivoManager.getInstance().getBlankImage();
      }
      logo.setBorder(Rectangle.BOX);
      logo.setBorderWidth(0.1F);
      logo.setBorderColor(BORDER_COLOR);
      tableInt.addCell(logo);

      PdfPTable tableInfo = new PdfPTable(1);
      tableInfo.setTotalWidth(300);
      tableInfo.setLockedWidth(true);
      tableInfo.setHorizontalAlignment(Element.ALIGN_CENTER);
      tableInfo.getDefaultCell().setBorder(Rectangle.NO_BORDER);
      tableInfo.getDefaultCell().setBorderWidth(0.5f);
      tableInfo.getDefaultCell().setBorderColor(Color.BLUE);
      tableInfo.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
      tableInfo.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

      // tableInfo.addCell(new Phrase(new Chunk(GOBIERNO,
      // CABECERA_H1_FONT)));
      tableInfo.addCell(new Phrase(new Chunk(TITULO, CABECERA_H1_FONT)));

      PdfPCell cell = new PdfPCell(tableInfo.getDefaultCell());
      if (StringUtils.isNotEmpty(SUBTITULO1)) {
        cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM);
        // cell.setPhrase(new Phrase(new Chunk(CONSEJERIA,
        // CABECERA_H2_FONT)));
        cell.setPhrase(new Phrase(new Chunk(SUBTITULO1, CABECERA_H2_FONT)));
        tableInfo.addCell(cell);
      }

      if (StringUtils.isNotEmpty(SUBTITULO2)) {
        cell = new PdfPCell(tableInfo.getDefaultCell());
        cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM);
        // cell.setPhrase(new Phrase(new Chunk(VICECONSEJERIA,
        // CABECERA_H3_FONT)));
        cell.setPhrase(new Phrase(new Chunk(SUBTITULO2, CABECERA_H3_FONT)));
        tableInfo.addCell(cell);
      }

      if (StringUtils.isNotEmpty(SUBTITULO3)) {
        // tableInfo.addCell(new Phrase(new Chunk(DIRECCION,
        // CABECERA_H3_FONT)));
        tableInfo.addCell(new Phrase(new Chunk(SUBTITULO3, CABECERA_H3_FONT)));
      }

      tableInt.addCell(tableInfo);

      // tableInt.addCell(new Phrase(new Chunk(APPLICATION_NAME,
      // APP_NAME_FONT)));
      Image logoArchivo = null;

      try {
        logoArchivo = Image.getInstance(ARCHIVO_IMAGE_URL);
      } catch (Exception e) {
        logoArchivo = ConfiguracionArchivoManager.getInstance().getBlankImage();
      }
      tableInt.addCell(logoArchivo);

      tableExt.addCell(tableInt);
      tableExt.writeSelectedRows(0, -1, document.left(), document.top() + 80, 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();
  }