Пример #1
0
 protected void copy(JRPrintImage printImage) {
   printImage.setRenderable(getRenderable());
   printImage.setAnchorName(getAnchorName());
   printImage.setHyperlinkReference(getHyperlinkReference());
   printImage.setHyperlinkAnchor(getHyperlinkAnchor());
   printImage.setHyperlinkPage(getHyperlinkPage());
   printImage.setHyperlinkTooltip(getHyperlinkTooltip());
   printImage.setBookmarkLevel(getBookmarkLevel());
   printImage.setHyperlinkParameters(hyperlinkParameters);
   //		transferProperties(printImage);
 }
  public void setImageSource(String imageSource) throws JRException {
    if (isEmbedded) {
      try {
        ByteArrayInputStream bais =
            new ByteArrayInputStream(imageSource.getBytes("UTF-8")); // FIXMENOW other encodings ?
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        Base64Decoder decoder = new Base64Decoder(bais, baos);
        decoder.process();

        printImage.setRenderer(
            JRImageRenderer.getInstance(baos.toByteArray())); // , JRImage.ON_ERROR_TYPE_ERROR));
      } catch (Exception e) {
        throw new JRException("Error decoding embedded image.", e);
      }
    } else {
      printImage.setRenderer(JRImageRenderer.getInstance(imageSource));
    }
  }
  @Override
  public void test() {
    super.test();

    numberOfPagesTest(1);

    try {
      byte[] imageData =
          JRImageLoader.getInstance(DefaultJasperReportsContext.getInstance())
              .loadBytesFromAwtImage(image, ImageTypeEnum.JPEG);
      JRPrintImage jrImage = (JRPrintImage) getElementAt("title.image1", 0);
      Assert.assertTrue(
          "image data",
          Arrays.equals(
              imageData,
              jrImage.getRenderable().getImageData(DefaultJasperReportsContext.getInstance())));
      Assert.assertEquals("scale image", ScaleImageEnum.CLIP, jrImage.getScaleImageValue());
    } catch (JRException e) {
      e.printStackTrace();
      Assert.fail(e.getMessage());
    }
  }
 protected float getYAlignFactor(JRPrintImage image) {
   float yalignFactor = 0f;
   switch (image.getVerticalAlignmentValue()) {
     case BOTTOM:
       {
         yalignFactor = 1f;
         break;
       }
     case MIDDLE:
       {
         yalignFactor = 0.5f;
         break;
       }
     case TOP:
     default:
       {
         yalignFactor = 0f;
         break;
       }
   }
   return yalignFactor;
 }
 protected float getXAlignFactor(JRPrintImage image) {
   float xalignFactor = 0f;
   switch (image.getHorizontalAlignmentValue()) {
     case RIGHT:
       {
         xalignFactor = 1f;
         break;
       }
     case CENTER:
       {
         xalignFactor = 0.5f;
         break;
       }
     case LEFT:
     default:
       {
         xalignFactor = 0f;
         break;
       }
   }
   return xalignFactor;
 }
  protected void exportReportToOasisZip(OutputStream os) throws JRException, IOException {
    OasisZip oasisZip =
        new FileBufferedOasisZip(((JROpenDocumentExporterNature) nature).getOpenDocumentNature());

    ExportZipEntry tempBodyEntry = new FileBufferedZipEntry(null);
    ExportZipEntry tempStyleEntry = new FileBufferedZipEntry(null);

    tempBodyWriter = tempBodyEntry.getWriter();
    tempStyleWriter = tempStyleEntry.getWriter();

    styleCache = new StyleCache(tempStyleWriter, fontMap, getExporterKey());

    Writer stylesWriter = oasisZip.getStylesEntry().getWriter();

    StyleBuilder styleBuilder = new StyleBuilder(jasperPrintList, stylesWriter);
    styleBuilder.build();

    stylesWriter.close();

    for (reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++) {
      setJasperPrint(jasperPrintList.get(reportIndex));

      List<JRPrintPage> pages = jasperPrint.getPages();
      if (pages != null && pages.size() > 0) {
        if (isModeBatch) {
          startPageIndex = 0;
          endPageIndex = pages.size() - 1;
        }

        JRPrintPage page = null;
        for (pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++) {
          if (Thread.interrupted()) {
            throw new JRException("Current thread interrupted.");
          }

          page = pages.get(pageIndex);

          exportPage(page);
        }
      }
    }

    tempBodyWriter.flush();
    tempStyleWriter.flush();

    tempBodyWriter.close();
    tempStyleWriter.close();

    /*   */
    ContentBuilder contentBuilder =
        new ContentBuilder(
            oasisZip.getContentEntry(),
            tempStyleEntry,
            tempBodyEntry,
            styleCache.getFontFaces(),
            ((JROpenDocumentExporterNature) nature).getOpenDocumentNature());
    contentBuilder.build();

    tempStyleEntry.dispose();
    tempBodyEntry.dispose();

    if ((imagesToProcess != null && imagesToProcess.size() > 0)) {
      for (Iterator<JRPrintElementIndex> it = imagesToProcess.iterator(); it.hasNext(); ) {
        JRPrintElementIndex imageIndex = it.next();

        JRPrintImage image = getImage(jasperPrintList, imageIndex);
        JRRenderable renderer = image.getRenderer();
        if (renderer.getType() == JRRenderable.TYPE_SVG) {
          renderer =
              new JRWrappingSvgRenderer(
                  renderer,
                  new Dimension(image.getWidth(), image.getHeight()),
                  ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null);
        }

        oasisZip.addEntry( // FIXMEODT optimize with a different implementation of entry
            new FileBufferedZipEntry(
                "Pictures/" + getImageName(imageIndex), renderer.getImageData()));
      }
    }

    oasisZip.zipEntries(os);

    oasisZip.dispose();
  }
  /**
   * @throws JRException
   * @throws IOException
   */
  public void exportImage(JRPrintImage image) throws JRException, IOException {
    xmlWriter.startElement(JRXmlConstants.ELEMENT_image);
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_scaleImage, image.getOwnScaleImageValue());
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_hAlign, image.getOwnHorizontalAlignmentValue());
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_vAlign, image.getOwnVerticalAlignmentValue());
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_isLazy, image.isLazy(), false);
    xmlWriter.addAttribute(
        JRXmlConstants.ATTRIBUTE_onErrorType, image.getOnErrorTypeValue(), OnErrorTypeEnum.ERROR);

    if (hyperlinkProducerFactory == null) {
      xmlWriter.addEncodedAttribute(
          JRXmlConstants.ATTRIBUTE_hyperlinkType,
          image.getLinkType(),
          HyperlinkTypeEnum.NONE.getName());
      xmlWriter.addEncodedAttribute(
          JRXmlConstants.ATTRIBUTE_hyperlinkReference, image.getHyperlinkReference());
      xmlWriter.addEncodedAttribute(
          JRXmlConstants.ATTRIBUTE_hyperlinkAnchor, image.getHyperlinkAnchor());
      xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_hyperlinkPage, image.getHyperlinkPage());
    } else {
      String reference = hyperlinkProducerFactory.produceHyperlink(image);
      if (reference != null) {
        xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_hyperlinkType, HyperlinkTypeEnum.REFERENCE);
        xmlWriter.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_hyperlinkReference, reference);
      }
    }

    xmlWriter.addAttribute(
        JRXmlConstants.ATTRIBUTE_hyperlinkTarget,
        image.getLinkTarget(),
        HyperlinkTargetEnum.SELF
            .getName()); // FIXMETARGET this exporter is used in the Flash viewer
    xmlWriter.addEncodedAttribute(
        JRXmlConstants.ATTRIBUTE_hyperlinkTooltip, image.getHyperlinkTooltip());
    xmlWriter.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_anchorName, image.getAnchorName());
    xmlWriter.addAttribute(
        JRXmlConstants.ATTRIBUTE_bookmarkLevel, image.getBookmarkLevel(), JRAnchor.NO_BOOKMARK);

    exportReportElement(image);
    exportBox(image.getLineBox());
    exportGraphicElement(image);

    JRRenderable renderer = image.getRenderer();
    if (renderer != null) {
      xmlWriter.startElement(JRXmlConstants.ELEMENT_imageSource);
      xmlWriter.addAttribute(
          JRXmlConstants.ATTRIBUTE_isEmbedded, isEmbeddingImages && !image.isLazy(), false);

      String imageSource = "";

      if (renderer.getType() == JRRenderable.TYPE_SVG) {
        renderer =
            new JRWrappingSvgRenderer(
                renderer,
                new Dimension(image.getWidth(), image.getHeight()),
                ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null);
      }

      if (image.isLazy()) {
        imageSource = ((JRImageRenderer) renderer).getImageLocation();
      } else if (isEmbeddingImages) {
        try {
          ByteArrayInputStream bais = new ByteArrayInputStream(renderer.getImageData());
          ByteArrayOutputStream baos = new ByteArrayOutputStream();

          Base64Encoder encoder = new Base64Encoder(bais, baos);
          encoder.process();

          imageSource = new String(baos.toByteArray(), DEFAULT_XML_ENCODING);
        } catch (IOException e) {
          throw new JRException("Error embedding image into XML.", e);
        }
      } else {
        if (renderer.getType() == JRRenderable.TYPE_IMAGE
            && rendererToImagePathMap.containsKey(renderer)) {
          imageSource = rendererToImagePathMap.get(renderer);
        } else {
          imageSource = IMAGE_PREFIX + getNextImageId();
          imageNameToImageDataMap.put(imageSource, renderer.getImageData());

          imageSource = new File(imagesDir, imageSource).getPath();
          rendererToImagePathMap.put(renderer, imageSource);
        }
      }

      xmlWriter.writeCDATA(imageSource);
      xmlWriter.closeElement();
    }

    if (hyperlinkProducerFactory == null) {
      exportHyperlinkParameters(image);
    }

    xmlWriter.closeElement();
  }
Пример #8
0
  public JasperPrint fill(HttpServletRequest req, UserAccount user) throws Exception {
    // JasperPrint
    JasperPrint jasperPrint = new JasperPrint();
    jasperPrint.setName("NoReport");
    jasperPrint.setPageWidth(595);
    jasperPrint.setPageHeight(842);

    // Fonts
    JRBaseStyle normalStyle = new JRBaseStyle("Arial_Normal");
    normalStyle.setFontName("Arial");
    normalStyle.setFontSize(8);
    normalStyle.setPdfFontName("Helvetica");
    normalStyle.setPdfEncoding("Cp1252");
    normalStyle.setPdfEmbedded(false);
    jasperPrint.addStyle(normalStyle);

    JRBaseStyle boldStyle = new JRBaseStyle("Arial_Bold");
    boldStyle.setFontName("Arial");
    boldStyle.setFontSize(8);
    boldStyle.setBold(true);
    boldStyle.setPdfFontName("Helvetica-Bold");
    boldStyle.setPdfEncoding("Cp1252");
    boldStyle.setPdfEmbedded(false);
    jasperPrint.addStyle(boldStyle);

    JRBaseStyle italicStyle = new JRBaseStyle("Arial_Italic");
    italicStyle.setFontName("Arial");
    italicStyle.setFontSize(8);
    italicStyle.setItalic(true);
    italicStyle.setPdfFontName("Helvetica-Oblique");
    italicStyle.setPdfEncoding("Cp1252");
    italicStyle.setPdfEmbedded(false);
    jasperPrint.addStyle(italicStyle);

    JRPrintPage page = new JRBasePrintPage();

    JRPrintLine line = new JRBasePrintLine(jasperPrint.getDefaultStyleProvider());
    line.setX(40);
    line.setY(50);
    line.setWidth(515);
    line.setHeight(0);
    page.addElement(line);

    JRPrintImage image = new JRBasePrintImage(jasperPrint.getDefaultStyleProvider());
    image.setX(45);
    image.setY(55);
    image.setWidth(165);
    image.setHeight(40);
    image.setScaleImage(JRImage.SCALE_IMAGE_CLIP);
    image.setRenderer(
        JRImageRenderer.getInstance(
            JRLoader.loadBytes(this.getClass().getResourceAsStream("jasperreports.gif"))));
    page.addElement(image);

    JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setX(210);
    text.setY(55);
    text.setWidth(345);
    text.setHeight(30);
    text.setTextHeight(text.getHeight());
    text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
    text.setLineSpacingFactor(1.3133681f);
    text.setLeadingOffset(-4.955078f);
    text.setStyle(boldStyle);
    text.setFontSize(18);
    text.setText("JasperReports Project Description");
    page.addElement(text);

    text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setX(210);
    text.setY(85);
    text.setWidth(325);
    text.setHeight(15);
    text.setTextHeight(text.getHeight());
    text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
    text.setLineSpacingFactor(1.329241f);
    text.setLeadingOffset(-4.076172f);
    text.setStyle(italicStyle);
    text.setFontSize(12);
    text.setText((new SimpleDateFormat("EEE, MMM d, yyyy")).format(new Date()));
    page.addElement(text);

    text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setX(40);
    text.setY(150);
    text.setWidth(515);
    text.setHeight(200);
    text.setTextHeight(text.getHeight());
    text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED);
    text.setLineSpacingFactor(1.329241f);
    text.setLeadingOffset(-4.076172f);
    text.setStyle(normalStyle);
    text.setFontSize(14);
    text.setText(
        "JasperReports is a powerful report-generating tool that has the ability to deliver rich content onto the screen, to the printer or into PDF, HTML, XLS, CSV or XML files.\n\n"
            + "It is entirely written in Java and can be used in a variety of Java enabled applications, including J2EE or Web applications, to generate dynamic content.\n\n"
            + "Its main purpose is to help creating page oriented, ready to print documents in a simple and flexible manner.");
    page.addElement(text);

    jasperPrint.addPage(page);

    return jasperPrint;
  }