【问题标题】:Add java.awt.Image to itext directly直接将 java.awt.Image 添加到 itext
【发布时间】:2016-05-21 12:33:22
【问题描述】:

我想知道你是否可以直接将 java.awt.Image 添加到 itext;到目前为止我看到的所有对这个问题的回答都建议将图像写入磁盘

   ImageIO.write(img, "png", "output.png);

然后使用 com.itextpdf.text.Image 方法Image.getInstance()获取它

   Image iTextImage = Image.getInstance("output.png");

此解决方案有效,但不是很优雅。有什么办法可以做得更好吗?

【问题讨论】:

  • 长答案如下。简短的回答是:您是否尝试过 IDE 为您提供的自动完成建议?可能不会,因为那时你不必问。

标签: java image itext


【解决方案1】:

如果您查看 iText API documentation for the Image class,那么您会发现除了文件名之外还有许多其他方式可以使用图像:

  • static Image getInstance(byte[] imgb)
  • static Image getInstance(byte[] imgb, boolean recoverFromImageError) 获取 Image 的实例
  • static Image getInstance(Image image) 获取 Image 的实例
  • static Image getInstance(Image image, Color color)java.awt.Image 获取 Image 的实例。
  • static Image getInstance(Image image, Color color, boolean forceBW)java.awt.Image 获取 Image 的实例。
  • static Image getInstance(int width, int height, boolean reverseBits, int typeCCITT, int parameters, byte[] data) 使用 CCITT G3 或 G4 压缩创建 Image
  • static Image getInstance(int width, int height, boolean reverseBits, int typeCCITT, int parameters, byte[] data, int[] transparency) 使用 CCITT G3 或 G4 压缩创建 Image
  • static Image getInstance(int width, int height, byte[] data, byte[] globals) 创建一个 JBIG2 Image
  • static Image getInstance(int width, int height, int components, int bpc, byte[] data) 在原始模式下获取Image 的实例。
  • static Image getInstance(int width, int height, int components, int bpc, byte[] data, int[] transparency) 在原始模式下获取 Image 的实例。
  • static Image getInstance(PdfContentByte cb, Image awtImage, float quality)java.awt.Image 中获取Image 的实例。
  • static Image getInstance(PdfTemplate template) 获取 Image 的实例
  • static Image getInstance(PdfWriter writer, Image awtImage, float quality)java.awt.Image 获取Image 的实例。
  • static Image getInstance(PRIndirectReference ref) 重复使用现有图像。
  • static Image getInstance(String filename) 获取 Image 的实例
  • static Image getInstance(String filename, boolean recoverFromImageError)
  • static Image getInstance(URL url)
  • static Image getInstance(URL url, boolean recoverFromImageError) 获取Image 的实例。

您可以在官方网站上找到有关如何使用java.awt.Image 的示例。请参阅ImageTypes 示例:

// Adding a java.awt.Image
java.awt.Image awtImage =
    Toolkit.getDefaultToolkit().createImage(RESOURCE);
img = com.itextpdf.text.Image.getInstance(awtImage, null);
document.add(new Paragraph(
    String.format("%s is an image of type %s",
    "java.awt.Image", img.getClass().getName())));
document.add(img);

【讨论】:

  • 感谢布鲁诺清理我的答案。 (我猜):D
  • 我经常遇到同样的问题:如果需要复制链接和代码 sn-ps,电话接听并不容易。
猜你喜欢
  • 2018-03-31
  • 1970-01-01
  • 2022-07-31
  • 1970-01-01
  • 2015-06-27
  • 2011-09-14
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多