简体   繁体   English

iText:如何在不使透明部分为黑色的情况下将透明图像转换为pdf?

[英]iText: how to put transparent images into a pdf without making the transparent parts BLACK?

I'm using iText 5.5.9 inside my Android Studio project to make a PDF file. 我在Android Studio项目中使用iText 5.5.9制作PDF文件。
When I add an image, the transparent parts turn to black inside the PDF. 添加图像时,PDF内的透明部分变为黑色。

How can I avoid that? 我该如何避免呢?

Here is the part of my code which illustrates the problem: 这是我的代码中说明问题的部分:

        // add card

        Resources res=getResources();
        Drawable drawable=res.getDrawable(R.drawable.card);
        Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        byte[] bitMapData = stream.toByteArray();
        Image img=Image.getInstance(bitMapData);

        img.scaleAbsolute(300f,156.3f);
        img.setSmask(false);
        doc.add(img);

在此处输入图片说明

If you want to add a transparent circle image with broader (ie stroke) in iText , you can add a circle with stroke. 如果要在iText中添加具有更宽(即笔触)的透明圆形图像,则可以添加具有笔触的圆形。

Use the code below (and change it to suit your needs) : 使用下面的代码(并更改为适合您的需要):

try {
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("myCirclepdffile.pdf"));
  document.open();

  PdfContentByte cb = writer.getDirectContent();

  cb.circle(250.0f, 500.0f, 200.0f);
  cb.stroke();

} catch (Exception e) {
  System.err.println(e.getMessage());
}

Transparent images aren't supported in PDF (such as PNGs or GIFs with a color that acts as if it were transparent), but they are supported in iText. PDF不支持透明图像(例如PNG或GIF的颜色看起来像是透明的),但iText支持透明图像。

Transparency in images in PDF is achieved by adding the opaque image along with an image mask that mimics the transparency. 通过添加不透明图像以及模仿透明度的图像蒙版,可以实现PDF中图像的透明度。 For every transparent image added to a PDF, iText adds two images: the opaque image and the mask. 对于添加到PDF的每个透明图像,iText都会添加两个图像:不透明图像和蒙版。

This is true for PNG files and GIF files that support transparency. 对于支持透明度的PNG文件和GIF文件,这是正确的。 Unfortunetely, you are using the JPEG format, and JPEG doesn't support transparency. 不幸的是,您使用的是JPEG格式,而JPEG不支持透明性。 See Transparent background in JPEG image 请参见JPEG图像中的透明背景

Use PNG or GIF instead of JPEG. 使用PNG或GIF代替JPEG。

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

相关问题 如何在iText创建的PDF中消除透明图像周围的黑色边框 - How to remove black border around transparent image in PDF created by iText 如何在android中的图像上放置黑色透明 - How to put black transparent on image in android 如何将 Webp 图像放入 PDF 和 itext 中 java? - How to put Webp images in a PDF with itext in java? IText - BarcodeQRCode使背景颜色透明 - IText - BarcodeQRCode making the background color transparent Itext 5.5.13.3 是否支持透明 pdf 背景? - Does Itext 5.5.13.3 support Transparent pdf backgrounds? GIF / PNG图像的透明部分在JLabel java中以黑色显示 - transparent parts of GIF/PNG image shown in black inside a JLabel java 将JPanel转换成Image添加到 iText PDF 背景为白色或透明 - Converting JPanel to Image to be added in iText PDF with white or transparent background 如何在透明窗口上绘制图像? - How to draw images on transparent window? xfa.fillXfaForm之后,透明图像显示为黑框。 XFA填充是否支持透明图像? - Transparent images are showing up as black boxes after xfa.fillXfaForm. Are transparent images supported for the xfa fill? iText 7:无边框或背景(透明)的图像字段(按钮) - iText 7 : image field (button) without border nor background (transparent)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM