简体   繁体   English

直接将java.awt.Image添加到itext

[英]Add java.awt.Image to itext directly

I was wondering if you could add a java.awt.Image to itext directly; 我想知道你是否可以直接向itext添加java.awt.Image; all the responses to this question I've seen so far suggest to write the image to disk 到目前为止我看到的所有对这个问题的回答都建议将图像写入磁盘

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

Then fetch it using the com.itextpdf.text.Image method Image.getInstance() 然后使用com.itextpdf.text.Image方法Image.getInstance()获取它

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

This solution works but is not quite elegant. 这个解决方案有效但不太优雅。 Is there any way to do this better? 有没有办法做得更好?

If you look at the iText API documentation for the Image class , then you will see that there are many other ways than a filename to use an image: 如果您查看Image类的iText API文档 ,那么您将看到除文件名之外还有许多其他方法可以使用图像:

  • static Image getInstance(byte[] imgb)
  • static Image getInstance(byte[] imgb, boolean recoverFromImageError) gets an instance of an Image static Image getInstance(byte[] imgb, boolean recoverFromImageError)获取Image的实例
  • static Image getInstance(Image image) gets an instance of an Image static Image getInstance(Image image)得到一个实例Image
  • static Image getInstance(Image image, Color color) Gets an instance of an Image from a java.awt.Image . static Image getInstance(Image image, Color color)java.awt.Image获取Image的实例。
  • static Image getInstance(Image image, Color color, boolean forceBW) Gets an instance of an Image from a java.awt.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) Creates an Image with CCITT G3 or G4 compression. 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) Creates an Image with CCITT G3 or G4 compression. 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) Creates a JBIG2 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) Gets an instance of an Image in raw mode. 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) Gets an instance of an Image in raw mode. 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) Gets an instance of a Image from a java.awt.Image . static Image getInstance(PdfContentByte cb, Image awtImage, float quality)java.awt.Image获取Image的实例。
  • static Image getInstance(PdfTemplate template) gets an instance of an Image static Image getInstance(PdfTemplate template)获取Image的实例
  • static Image getInstance(PdfWriter writer, Image awtImage, float quality) Gets an instance of a Image from a java.awt.Image . static Image getInstance(PdfWriter writer, Image awtImage, float quality)java.awt.Image获取Image的实例。
  • static Image getInstance(PRIndirectReference ref) Reuses an existing image. static Image getInstance(PRIndirectReference ref)现有图像。
  • static Image getInstance(String filename) Gets an instance of an Image 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) Gets an instance of an Image . static Image getInstance(URL url, boolean recoverFromImageError)获取Image的实例。

You can find an example on how to use a java.awt.Image on the official web site. 您可以在官方网站上找到有关如何使用java.awt.Image的示例。 See the ImageTypes example: 请参阅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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM