Пример #1
0
  /**
   * Helperfunction to extract an image from an imagereference. If the image is contained as
   * java.awt.Image object only, the image is recoded into an PNG-Image.
   *
   * @param reference the image reference.
   * @return an image.
   * @throws com.lowagie.text.DocumentException if no PDFImageElement could be created using the
   *     given ImageReference.
   * @throws java.io.IOException if the image could not be read.
   */
  public Image getImage(final ImageContainer reference) throws DocumentException, IOException {
    if (reference == null) {
      throw new NullPointerException();
    }

    Object identity = null;
    java.awt.Image image = null;
    if (reference instanceof URLImageContainer) {
      final URLImageContainer urlImageContainer = (URLImageContainer) reference;
      final ResourceKey url = urlImageContainer.getResourceKey();
      if (url != null && urlImageContainer.isLoadable()) {
        identity = url;
        final Image cached = (Image) cachedImages.get(identity);
        if (cached != null) {
          return cached;
        }

        try {
          final ResourceData resourceData = resourceManager.load(url);
          final byte[] data = resourceData.getResource(resourceManager);
          final Image itextimage = Image.getInstance(data);
          cachedImages.put(identity, itextimage);
          return itextimage;
        } catch (ResourceException re) {
          RTFImageCache.logger.info("Caught illegal Image, will recode to PNG instead", re);
        } catch (BadElementException be) {
          RTFImageCache.logger.info("Caught illegal Image, will recode to PNG instead", be);
        } catch (IOException ioe) {
          RTFImageCache.logger.info(
              "Unable to read the raw-data, will try to recode image-data.", ioe);
        }

        try {
          final Resource resource = resourceManager.create(url, null, Image.class);
          image = (java.awt.Image) resource.getResource();
        } catch (ResourceException re) {
          RTFImageCache.logger.info("Caught illegal Image, will try to find local instance", re);
        }
      }
    }

    if (reference instanceof LocalImageContainer && image == null) {
      final LocalImageContainer localImageContainer = (LocalImageContainer) reference;
      image = localImageContainer.getImage();
      if (image != null) {
        // check, if the content was cached ...
        identity = localImageContainer.getIdentity();
        if (identity != null) {
          final Image cachedImage = (Image) cachedImages.get(identity);
          if (cachedImage != null) {
            return cachedImage;
          }
        }
      }
    }
    if (image == null) {
      return null;
    }

    final WaitingImageObserver obs = new WaitingImageObserver(image);
    obs.waitImageLoaded();

    try {
      final byte[] data = RenderUtility.encodeImage(image);
      final Image itextimage = Image.getInstance(data);
      if (identity != null) {
        cachedImages.put(identity, itextimage);
      }
      return itextimage;
    } catch (UnsupportedEncoderException uee) {
      logger.warn("Assertation-Failure: PNG encoding failed.", uee);
      return null;
    }
  }
  protected void drawReplacedContent(final RenderableReplacedContentBox content) {
    final Graphics2D g2 = getGraphics();
    final Object o = content.getContent().getRawObject();
    if (o instanceof DrawableWrapper) {
      final DrawableWrapper drawableWrapper = (DrawableWrapper) o;
      if (drawDrawable(content, g2, drawableWrapper)) {
        drawImageMap(content);
      }
      return;
    }
    if (o instanceof Image) {
      if (drawImage(content, (Image) o)) {
        drawImageMap(content);
      }
      return;
    }

    if (o instanceof URLImageContainer) {
      final URLImageContainer imageContainer = (URLImageContainer) o;
      if (imageContainer.isLoadable() == false) {
        PdfLogicalPageDrawable.logger.info(
            "URL-image cannot be rendered, as it was declared to be not loadable: "
                + imageContainer.getSourceURLString());
      } else {
        final ResourceKey resource = imageContainer.getResourceKey();
        if (resource == null) {
          PdfLogicalPageDrawable.logger.info(
              "URL-image cannot be rendered, as it did not return a valid URL.");
        } else {
          try {
            final ResourceManager resourceManager = getResourceManager();
            final com.lowagie.text.Image instance;
            final com.lowagie.text.Image maybeImage = imageCache.get(resource);
            if (maybeImage != null) {
              instance = maybeImage;
            } else {
              final ResourceData data = resourceManager.load(resource);
              instance = com.lowagie.text.Image.getInstance(data.getResource(resourceManager));
              imageCache.put(resource, instance);
            }

            final Resource imageWrapped = resourceManager.create(resource, null, Image.class);
            final Image image = (Image) imageWrapped.getResource();

            if (drawImage(content, image, instance)) {
              drawImageMap(content);
            }
            return;
          } catch (InvalidReportStateException re) {
            throw re;
          } catch (Exception e) {
            PdfLogicalPageDrawable.logger.info(
                "URL-image cannot be rendered, as the image was not loadable.", e);
          }
        }
      }
    }

    if (o instanceof LocalImageContainer) {
      final LocalImageContainer imageContainer = (LocalImageContainer) o;
      final Image image = imageContainer.getImage();
      if (drawImage(content, image)) {
        drawImageMap(content);
      }
    } else {
      PdfLogicalPageDrawable.logger.debug("Unable to handle " + o);
    }
  }